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

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑起来贼好看

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值