【容器】docker-compose一键启动mysql和nginx

帅彦
2023-12-01

docker安装

yum更新

yum -y update

安装依赖项

yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

yum install -y yum-utils device-mapper-persistent-data lvm2

设置yum源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安装稳定版本

yum install -y docker-ce

启动并加入开机启动

sudo systemctl start docker
sudo systemctl enable docker

安装 docker-compose

下载 docker-compose 包,并加入到path里


curl -L https://github.com/docker/compose/releases/download/1.26.0/docker-compose-`uname -s `-`uname -m` > /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose -v

基础镜像

本文是基于arm 64 架构

docker pull mysql:8.0.30
docker pull redis:7.0.4
docker pull nacos/nacos-server:v2.2.0-slim
docker pull zookeeper:3.8
docker pull postgres:14.5

具体的 docker-compose.yaml 文件如下

version: '3'
services:

  mysql:
    image: mysql:8.0.30
    container_name: mysql
    ports:
      - "3306:3306" # 绑定容器的3306端口到主机的3306端口 前面:主机端口,后面:容器端口
    networks:
      - back
    volumes:
      - ./mysql/data:/var/lib/mysql  # 挂载主机上的./mysql/data目录到容器里的/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    privileged: true
    environment: # 环境变量
      - TZ="Asia/Shanghai"
      - MYSQL_ROOT_PASSWORD=root
    command: --default-authentication-plugin=mysql_native_password
    restart: always

  redis:
    image: redis:7.0.4
    container_name: redis
    ports:
      - "6379:6379"
    networks:
      - back
    volumes:
      - ./redis/data:/data
      - ./redis/conf/redis.conf:/etc/redis/redis.conf
    privileged: true
    environment:
      - TZ="Asia/Shanghai"
    # command: redis-server --requirepass ${pwd} --notify-keyspace-events Eglx
    command: redis-server /etc/redis/redis.conf --appendonly yes
    restart: always

  nacos:
    image: nacos/nacos-server:v2.2.0-slim
    container_name: nacos-server
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9849:9849"
    networks:
      - back
    volumes:
      - ./nacos/conf:/home/nacos/conf
      - ./nacos/logs:/home/nacos/logs
      - ./nacos/data:/home/nacos/data
    privileged: true
    environment:
      - TIME_ZONE="Asia/Shanghai"
      - MODE=standalone
      - SPRING_DATASOURCE_PLATFORM=mysql
      - MYSQL_SERVICE_HOST=mysql
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=root
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_DB_NUM=1

    restart: always

  pgsql:
    image: postgres:14.5
    container_name: pgsql
    privileged: true
    environment:
      POSTGRES_PASSWORD: root
      POSTGRES_USER:  postgres
      POSTGRES_DB: postgres
      TZ: Asia/Shanghai
    ports:
      - "5432:5432"
    networks:
      - back
    volumes:
      - ./pgsql/data:/var/lib/postgresql/data
    restart: always

  nginx:
    image: nginx:1.23.1
    container_name: nginx
    ports:
      - "80:80"
    networks:
      - back
    volumes:
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
    privileged: true
    environment:
      - TZ="Asia/Shanghai"
    command: /bin/bash -c "exec nginx -g 'daemon off;'"
    restart: always

  zk-1:
    image: zookeeper:3.8
    restart: always
    hostname: zk-1
    container_name: zk-1
    ports:
      - "2181:2181"
    networks:
      - back
    volumes:
      - ./zk-1/data:/data
      - ./zk-1/datalog:/datalog
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zk-2:2888:3888 server.3=zk-3:2888:3888

  zk-2:
    image: zookeeper:3.8
    restart: always
    hostname: zk-2
    container_name: zk-2
    ports:
      - "2281:2181"
    networks:
      - back
    volumes:
      - ./zk-2/data:/data
      - ./zk-2/datalog:/datalog
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zk-3:2888:3888

  zk-3:
    image: zookeeper:3.8
    restart: always
    hostname: zk-3
    container_name: zk-3
    ports:
      - "2381:2181"
    networks:
      - back
    volumes:
      - ./zk-3/data:/data
      - ./zk-3/datalog:/datalog
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 server.3=0.0.0.0:2888:3888

networks:
  back:
    driver: bridge

详细解释如下:

  • 指定服务使用的镜像:image
  • 指定服务镜像启动命令:command
  • 指定运行服务的容器名称:container_name
  • 指定服务依赖关系:depends_on
  • 设置环境变量:environment
  • 设置容器互联:links
  • 指定端口暴露:expose
  • 设置服务容器的端口映射:ports
 类似资料: