docker-compose

1、安装docker

2、下载docker-compose

  sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /mnt/docker-compose

3、给文件赋权

  chmod +x /mnt/docker-compose

4、上传springboot项目jar包到workspace

5、新建Dockerfile文件

FROM java:8
VOLUME /tmp
ADD demo-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 9000
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

6、新建docker-compose.yml文件

version: '2'
services:
  nginx:
    image: nginx:1.16.1
    ports:
      - 80:80
      - 443:443
    privileged: true
    restart: always
    volumes:
      - /mnt/tools/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /mnt/tools/nginx/conf/keys:/etc/nginx/keys
      - /mnt/tools/nginx/conf/conf.d:/etc/nginx/conf.d
      - /mnt/tools/nginx/www:/usr/share/nginx/html
      - /mnt/tools/nginx/logs:/var/log/nginx
  redis:
    image: redis:5.0
    privileged: true
    restart: always
    ports:
      - 6379:6379
    volumes:
      - /mnt/tools/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /mnt/tools/redis/data:/data
    command:
      # 以配置文件的方式启动 redis.conf
      redis-server /usr/local/etc/redis/redis.conf
  demo:
    build: .
    restart: always
    ports:
      - 8080:8080
    depends_on:
      - nginx
      - redis

7、在宿主机/mnt/tools下创建相应的nginx、redis目录

8、编辑nginx.conf和redis.conf并上传指定宿主机路径

9、拉取并执行docker-compose

  /mnt/docker/docker-compose up -d  # -d表示后台启动

10、当修改代码重新上传后,需要刷新容器

  ./docker-compose up --force-recreate --build -d

说明:当项目跟redis、mysql等在一台宿主机上时,可以用宿主机的内网ip作为访问地址,当不在一台宿主机上时,需要用宿主机的外网ip作为访问地址,尤其需要注意redis的访问权限bind,和mysql的访问权限。以防无法访问和安全问题。

上一篇:C#学习笔记 类型转换


下一篇:用Jetpack Compose完美复刻Flappy Bird!