默认分类

docker_compose举例

1、拉取镜像:

例:可选

拉取mysql 8.0.27镜像

docker pull mysql:8.0.27

2、安装docker

sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

启动docker

sudo systemctl start docker

sudo docker run hello-world

systemctl enable docker

修改源为国内地址,提升拉取镜像速度

vim /etc/docker/daemon.json

{
 "registry-mirrors" : [
   "https://mirror.baidubce.com",
   "https://mirror.ccs.tencentyun.com",
   "http://registry.docker-cn.com",
   "http://docker.mirrors.ustc.edu.cn",
   "http://hub-mirror.c.163.com"
 ],
 "insecure-registries" : [
   "registry.docker-cn.com",
   "docker.mirrors.ustc.edu.cn"
 ],
 "debug" : true,
 "experimental" : true
}

3、打包运行容器

执行docker-compose.yaml之前先打包app镜像

在app目录下执行, latest可以修改为对应版本号,打包命令

docker build -t app:latest .

Dockerfile文件内容

FROM openjdk:8
WORKDIR /app
MAINTAINER krsc
VOLUME /opt/docker/app
ADD road-admin-1.0-SNAPSHOT.jar /app/road-admin-1.0-SNAPSHOT.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/opt/docker/app/road-admin-1.0-SNAPSHOT.jar"]    

4、安装docker compose

下载docker compose,选择的是最新版2.12.2

Release v2.12.2 · docker/compose (github.com)

下载docker-compose-linux-x86_64文件,放到/usr/local/bin目录下,chmod+x添加运行权限

执行docker-compose -v检查版本是否正常

编写yaml文件

docker-compose.yaml

version: "3.9"
services:
# 使用mysql8,初始化并创建kerp_db、zentao_db库,赋予用户kccd权限
   mysql:
    image: mysql:8.0.27
    container_name: krsc-mysql
    restart: always
    ports:
     - "3306:3306"
    volumes:
     - /opt/mysql8/data:/var/lib/mysql
     - /opt/mysql8/my.cnf:/etc/mysql
     - /opt/mysql8/log:/logs
    environment:
      MYSQL_ROOT_PASSWORD: mysqlpassword
   #   MYSQL_DATABASE: zentao_db
      MYSQL_DATABASE: kerp_db
      MYSQL_USER: kccd
      MYSQL_PASSWORD: kccdpassword
    entrypoint:
      sh -c "
        echo 'CREATE DATABASE IF NOT EXISTS zento_db; GRANT SELECT ON zento_db.* TO kccd@`%`;' > /docker-entrypoint-initdb.d/init.sql;
        /usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
      "
    networks:
     - db
   #mongo:
   # image: mongo:4.4.3
   # container_name: mongo
   # restart: always
   # ports:
   #  - "27017:27017"
   # volumes:
   #  - /opt/mongo/conf:/data/conf
   #  - /opt/mongo/data:/data/db
   #  - /opt/mongo/log:/data/log
   # networks:
   #  - db
 #创建redis,并使用指定配置文件启动
   redis:
    image: redis:6.2.7
    container_name: krsc-redis
    command: redis-server /usr/local/etc/redis/redis.conf
    restart: always
    ports:
     - "6379:6379"
    volumes:
     - /opt/redis/redis.conf:/usr/local/etc/redis
     - /opt/redis/data:/data
    networks:
     - db
 #创建minio,并开放9001访问端口
   minio:
    image: minio/minio
    container_name: krsc-minio
    restart: always
    ports:
     - "9000:9000"
     - "9001:9001"
    command: server --console-address ':9001' /data
    volumes:
     - /opt/minio/data:/data
     - /opt/minio/config:/root/.minio
    environment:
     - TZ= "Asia/Shanghai"
     - MINIO_ACCESS_KEY=minioadmin
     - MINIO_SECRET_KEY=KcCd202211161058
    networks:
     - db
   app:
     image: app:latest
     container_name: krsc-app
     restart: always
     ports:
      - "9999:9999"
      - "8888:8888"
     volumes:
      - /opt/docker/app/road-admin-1.0-SNAPSHOT.jar:/road-admin-1.0-SNAPSHOT.jar
      - /opt/docker/app/logs:/logs
     environment:
      - TZ= "Asia/Shanghai"
     networks:
      - db
      - app
     entrypoint: java -Xms1024m -Xmx2048m -jar /road-admin-1.0-SNAPSHOT.jar >/logs/road.log --spring.profiles.active=krscdev --system.mode=test &

     
networks:
  db:
   ipam:
    driver: default
    config:
     - subnet: 192.168.128.0/24
       gateway: 192.168.128.1
  app:
   ipam:
    driver: default
    config:
     - subnet: 192.168.129.0/24
       gateway: 192.168.129.1

    
    
    
  web:  # nginx服务名称,不同于容器名称
    image: mynginximages  # nginx镜像名称
    container_name: nginx  # 生成的容器名称
    ports: # 宿主机端口:容器端口
      - "80:80"
    volumes: # 宿主机路径:容器内部文件路径,按自己机器实际情况修改宿主机路径
      - /e/project/docker/nginx/vhost:/usr/local/nginx/conf/vhost # nginx 多域名配置目录 
      - /e/project/docker/nginx/logs:/data/logs/nginx # nginx 日志目录
      - /e/project/docker/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf # nginx 主配置文件
    networks: # 指定加入网络
      - default
    entrypoint: # docker run 要运行的文件
      - /data/entrypoint.sh
  php: # php 容器服务,不同于容器名
    image: myphpimages # php 镜像
    container_name: php # php 容器名称
    volumes: # 宿主机路径:容器内部文件路径,按自己机器实际情况修改宿主机路径
      - /e/project/api:/data/www/.api # 项目目录
      - /e/project/docker/php/php-fpm.conf:/usr/local/php/etc/php-fpm.conf # php-fpm配置文件路径
    env_file: # 环境变量文件
      - ./mylocal.env # 内容可根据实际补充
    networks: # 指定加入网络
      - default
    entrypoint: # docker run 要运行的文件
      - /data/entrypoint.sh
    expose:
      - 9000 # 暴露容器端口,这里只需要让nginx能访问就可以,故无需映射主机端口
networks:  # 下面注意缩进
   default:   # 网络服务名称
     external:   # 外部网络
       name: devnet  # 网络名称

5、文件结构目录

/opt
  -docker
    -docker-compose.yaml
    -app
      -Dockerfile
      -road-admin-1.0-SNAPSHOT.jar

6、启动项目及效果

项目启动命令,/opt/docker目录下

docker-compose up -d

或使用-f命令指定yaml文件

docker-compose -f /opt/docker/docker-compose.yaml up -d

两个命令不通用,混用会导致删掉并重建新的容器,导致服务中断,建议一直使用其中一个

效果如图

2022-12-19T01:49:28.png

7、各种配置文件

mysql

[mysqld]

pid-file = /var/run/mysqld/mysqld.pid

socket = /var/run/mysqld/mysqld.sock

datadir = /var/lib/mysql

secure-file-priv= NULL

character_set_server = utf8mb4

collation_server = utf8mb4_bin
bind-address = 0.0.0.0
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections = 500
max_allowed_packet = 128M
wait_timeout=2880000 
interactive_timeout = 2880000

redis

mongo

8、变更影响

容器内安装ping命令,用于测试docker网络

apt-get update && apt-get install -y iputils-ping

通过配置文件对docker网络做了一定隔离,mysql、minio、redis、mongo使用db网络,app访问app、db网络

容器间通过容器名作为host地址进行互联访问

无法在容器中使用localhost直接访问非本容器中的服务
部署时需把对应密码更替为自己需求模块的强密码

2022-12-19T01:50:04.png

This is just a placeholder img.