docker compose.yml学习

docker compose

安装docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose


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


 ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

docker-compose version

容器创建

创建网络
设置固定ip

docker network create --subnet=172.19.0.0/18 mynetwork

docker-compose安装 mysql

遇到的问题
changing ownership of '/var/lib/mysql/': Permission denied
设置 privileged: true 属性

privileged: true  容器设置root属性
command: --lower_case_table_names=1   忽略大小写

 networks:
      default:
        ipv4_address: 172.19.0.2  绑定ip
version: '3'
services:
  mysql:
    image: mysql:8.0.23
    container_name: mysql_demo1
    ports:
      - '13001:3306'
	command: --lower_case_table_names=1
    volumes:
      - /data/mysql_demo1/config:/etc/mysql/conf.d
      - /data/mysql_demo1/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=abc123456
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.19.0.2
	privileged: true		

networks:
  default:
    external:
      name: mynetwork

启动命令

docker-compose up -d

指定文件启动
docker-compose -f docker-compose.yaml up

version : '3'                                      #compose文件版本支持特定的Docker版本
services:                                          #本工程的服务配置列表
  mysql_demo1:                                           #服务名,自定义
    container_name: docker-compose-mysql-demo      #容器名
    image: mysql:8.0.23                              #指定基于mysql:5.7镜像为基础镜像来构建镜像。ports:
      - "13001:3306"
    command: [                                           #使用command可以覆盖容器启动后默认执行的命令
            '--character-set-server=utf8mb4',            #设置数据库表的数据集
            '--collation-server=utf8mb4_unicode_ci',     #设置数据库表的数据集
            '--default-time-zone=+8:00'                  #设置mysql数据库的时区问题
    ]
    environment:           
      MYSQL_ROOT_PASSWORD: abc123456                         #设置root连接密码
      MYSQL_ROOT_HOST: '%'
    volumes:
      - /root/docker-compose/mysql/data:/var/lib/mysql
      - /root/docker-compose/mysql/config:/etc/mysql/conf.d
    restart: always
    networks:
      default:
        ipv4_address: 172.19.0.2
    privileged: true     
networks:
  default:
    external:
      name: mynetwork                                         #自定义的网络 

安装备份

version: '3'
services:
  mysql1:
    image: mysql:8.0.23
    container_name: mysql_1
    ports:
      - '12001:3306'
    command: --lower_case_table_names=1
    volumes:
        - /data/mysql_1/config:/etc/mysql/conf.d
        - /data/mysql_1/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=abc123456
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.2
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m
  mysql2:
    image: mysql:8.0.23
    container_name: mysql_2
    ports:
      - '12002:3306'
    command: --lower_case_table_names=1
    volumes:
      - /data/mysql_2/config:/etc/mysql/conf.d
      - /data/mysql_2/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=abc123456
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.3
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m
  mysql3:
    image: mysql:8.0.23
    container_name: mysql_3
    ports:
      - '12003:3306'
    command: --lower_case_table_names=1
    volumes:
      - /data/mysql_3/config:/etc/mysql/conf.d
      - /data/mysql_3/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=abc123456
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.4
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m
  mysql4:
    image: mysql:8.0.23
    container_name: mysql_4
    ports:
      - '12004:3306'
    command: --lower_case_table_names=1
    volumes:
      - /data/mysql_4/config:/etc/mysql/conf.d
      - /data/mysql_4/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=abc123456
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.5
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m
  mysql5:
    image: mysql:8.0.23
    container_name: mysql_5
    ports:
      - '12005:3306'
    command: --lower_case_table_names=1
    volumes:
      - /data/mysql_5/config:/etc/mysql/conf.d
      - /data/mysql_5/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=abc123456
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.6
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m
networks:
  default:
    external: true
    name: mynet


redis

version: '3'
services:
  redis:
    image: redis:6.0.10
    container_name: redis
    ports:
      - '16379:6379'    
	command: ["redis-server","/usr/local/etc/redis/redis.conf"]
    volumes:
       - /root/redis/conf:/usr/local/etc/redis
    environment:
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.9
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 200m  
networks:
  default:
    external: true
    name: mynet

你可能感兴趣的:(docker,学习,容器)