基础命令
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
解决文章来源:https://www.toymoban.com/news/detail-613450.html
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模板网!