docker compose.yml学习

这篇具有很好参考价值的文章主要介绍了docker compose.yml学习。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

基础命令

docker-compose up 启动文件
docker-compose up -d 后台启动文件
docker-compose -f up -d -f` 指定文件启动 启动文件

docker-compose -f compose.yml up -d

docker安装

docker安装

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

shell 命令创建redis mysql容器

docker-compose.yml 文件

version: '3'
services:
  mysql1:
    image: hub.c.163.com/library/mysql:5.7
    container_name: mysql_1
    ports:
      - '12001:3306'
    command: --lower_case_table_names=1
    volumes:
        - /root/mysql_1/config:/etc/mysql/conf.d
        - /root/mysql_1/data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=kdzt123456.
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.2
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m  
  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
  jpom-server:
    image: jpomdocker/jpom
    container_name: jpom-server
    ports:
      - '2122:2122'    
    volumes:
      - /home/jpom-server/logs:/usr/local/jpom-server/logs \
	  - /home/jpom-server/data:/usr/local/jpom-server/data \
	  - /home/jpom-server/conf:/usr/local/jpom-server/conf \
    environment:
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.3
    privileged: true
    restart: always	  
networks:
  default:
    external: true
    name: mynet



shell命令


echo -e "\e[1;31m 安装docker开始====> \e[0m"

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



sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -y \

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y \

echo -e "\e[1;31m 启动docker服务====> \e[0m"

sudo systemctl start docker \

systemctl enable docker \

echo -e "\e[1;31m 安装docker-compose========> \e[0m" \

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 \

echo -e "\e[1;31m 设置docker-compose 软连接===========> \e[0m" \

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


echo -e "\e[1;31m 开始创建容器======》 \e[0m"



echo -e "\e[1;31m 创建redis.conf=====> \e[0m"

sudo tee /root/redis/conf/redis.conf <<-'EOF'
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile ""
databases 12
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
requirepass kdzt123456.
EOF

echo -e "\e[1;31m 开始创建mysql、redis容器 \e[0m"

docker-compose -f /root/docker/docker-compose.yml up -d

遇到的问题

Error response from daemon: Get "https://index.docker.io/v1/search?q=jpomdocker%2Fjpom&n=25": dial tcp: lookup index.docker.io on 192.168.10.1:53: read udp 192.168.10.8:53929->192.168.10.1:53: read: connection refused
修改 /etc/resolv.conf

vim /etc/resolv.conf

添加
namespace  8.8.8.8
namespace  8.8.8.4
namespace  114.114.114.114

Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded

解决

vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com"]
}

Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)文章来源地址https://www.toymoban.com/news/detail-613450.html

命令

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
  redis:
    image: redis:6.0.10
    container_name: redis
    ports:
      - '6379:6379'    
	command: ["redis-server","/usr/local/etc/redis/redis.conf"]
    volumes:
       - /root/redis/conf:/usr/local/etc/redis
    environment:
      - TZ=Asia/Shanghai
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=abc123456
    networks:
      default:
        ipv4_address: 172.18.0.9
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 200m
  mongo:
    image: docker.io/mongo
    container_name: mongo
    ports:
      - '27017:27017'    
	command: ["mongod", "--config", "/etc/mongo/mongod.conf"]
    volumes:
       - /root/mongo:/etc/mongo \
       - /root/mongo/data/db:/data/db \
    environment:
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.8
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m
  ss:
    image: jdk
    container_name: ss
    ports:
      - '3307:3307'    
    volumes:
       - /root/ss:/root/ss
    environment:
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.7
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 800m
  minio:
    image: bitnami/minio:latest
    container_name: minio
    ports:
      - '9000:9000'     
      - '9001:9001'     
    volumes:
       - /root/minio/data:/data
    environment:
      - TZ=Asia/Shanghai
      - MINIO_ROOT_USER=root
      - MINIO_ROOT_PASSWORD=abc123456
      - MINIO_SKIP_CLIENT=yes
    networks:
      default:
        ipv4_address: 172.18.0.10
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m            
   mq:
    image: rabbitmq
    container_name: mq
    ports:
      - '5672:5672'
    volumes:
       - /root/minio/data:/data
    environment:
      - TZ=Asia/Shanghai
    networks:
      default:
        ipv4_address: 172.18.0.11
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m
  nacos:
    image: nacos/nacos-server
    container_name: nacos
    ports:
      - '8848:8848'
    environment:
      - TZ=Asia/Shanghai
      - MODE=standalone
    networks:
      default:
        ipv4_address: 172.18.0.12
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 400m     
  sentinel:
    image: bladex/sentinel-dashboard
    container_name: sentinel
    ports:
      - '8858:8858'
      - '8719:8719'
    environment:
      - TZ=Asia/Shanghai
      - MODE=standalone
    networks:
      default:
        ipv4_address: 172.18.0.13
    privileged: true
    restart: always
    deploy:
      resources:
        limits:
          memory: 600m            
    
networks:
  default:
    external: true
    name: mynet


到了这里,关于docker compose.yml学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Docker Compose Yaml(Yml)

    Docker Hub mysql 镜像 Mongo镜像 mongo默认是没有用户的,可以进行配置 首先创建宿主机挂载目录(选择性进行配置) 1、docker启动  1)、使用docker启动 2)、使用docker compose启动  2、创建用户和库 首先了解mongo角色权限 role 作用 read 允许用户读取指定数据库 readWrite 允许用户读写

    2024年02月09日
    浏览(41)
  • 【docker】docker-compose.yml 语法详解

    前面讲过Docker Compose的概念,如何安装、以及常用指令等。 这里重点介绍Docker Compose的语法,如何写一个正确的docker-compose.yml文件。 我们知道 docker-compose 指令(比如 docker-compose up 等)是运行它的脚本文件docker-compose.yml,那么docker-compose.yml内部到底有哪些语法要求呢! 【doc

    2024年01月21日
    浏览(53)
  • Docker Compose的常用命令与docker-compose.yml脚本属性配置

    注意:需要在docker-compose配置文件同目录下执行命令 不然提示如下: 下面命令可以输出 web服务 5000 端口所绑定的公共端口 会优雅地停止服务,即发送 SIGTERM 信号给容器,等待一段时间(默认为 10 秒)让容器执行清理操作,然后再发送 SIGKILL 信号强制关闭容器。这样可以确保

    2024年02月15日
    浏览(44)
  • 【Docker】Docker Compose,yml 配置指令参考的详细讲解

    作者简介: 辭七七,目前大二,正在学习C/C++,Java,Python等 作者主页: 七七的个人主页 文章收录专栏: 七七的闲谈 欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖💖 在现代软件开发中,应用程序的部署和管理是一个复杂而关键的任务。Docker Compose是一个强大的工具,可以简化多

    2024年02月05日
    浏览(34)
  • docker-compose.yml报错问题

    ERROR: The Compose file \\\'./docker-compose.yml\\\' is invalid because: Invalid top-level property \\\"True\\\". Valid top-level sections for this Compose file are: version, services, networks, volumes, secrets, configs, and extensions starting with \\\"x-\\\". You might be seeing this error because you\\\'re using the wrong Compose file version. Either specify a supported vers

    2024年02月14日
    浏览(45)
  • docker-compose.yml配置文件参数

    1. image 在 services 标签下的第二级标签是 web,这个名字是用户自己自定义,它就是服务名称。 image 则是 指定服务的镜像名称或镜像 ID 。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。 2. environment 环境变量 ,这个标签的作用是 设置镜像变量 ,它可以保存变量到镜像

    2024年02月10日
    浏览(36)
  • 如何使用docker-compose.yml构建镜像?

    使用 docker-compose.yml 文件来构建镜像,你需要在该文件中对你的服务进行定义,然后使用 docker-compose build 命令。 假设你有一个 docker-compose.yml 文件,它定义了一个名为 “web” 的服务,并指定了构建该服务所需的 Dockerfile 的路径,如下所示: 在这个例子中, build: . 指出 Dock

    2024年02月12日
    浏览(79)
  • 对象存储, 开源MinIO docker-compose.yml 文件

    https://min.io/docs/minio/linux/developers/python/API.html 经过验证,可以启动。 访问http://127.0.0.1:9001/ 登录一下(用户名和密码在 docker-compose 文件中已经配置) 配置 bucket 再配置 bucket 的accessKey, secretKey, 配置完后之后,可以下载这些配置文件为 json 文件到本地这个文件很重要, 在业

    2024年01月19日
    浏览(49)
  • ERROR: Version in “./docker-compose.yml“ is unsupported

    错误原因:------              docker-compose版本过低 解决方式: 直接改成3.3  或者你保存信息中提到的版本号 首先要查看docker-compose的路径 根据上一步的 whereis 找到了docker-compose的路径为: /usr/bin/docker-compose ,所以下面的脚本,路径也替换为这个就可以了。 下载 授权 版本号

    2024年02月16日
    浏览(41)
  • docker-composeERROR:ERROR: Version in “./docker-compose.yml“ is unsupported

    常见问题 docker compose出现ERROR:ERROR: Version in \\\"./docker-compose.yml\\\" is unsupported. You might be seeing this error because you\\\'re using the wrong Compose file version. Either specify a supported version (\\\"2.0\\\", \\\"2.1\\\", \\\"3.0\\\") and place your service definitions under the `services` key, 原因:docker-compose版本不对,需要升级版本 下载地

    2024年02月11日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包