docker compose使用
前置
初始化文件夹,网络。
init_dir.sh
#!/bin/env bash
# 生成默认配置
HOME=$(pwd)
# docker network free
init_network(){
NETWORK_NAME="free"
CREATED=$(docker network ls -f name=${NETWORK_NAME} | wc -l)
if [ '2' -ne ${CREATED} ]
then
docker network create ${NETWORK_NAME}
echo created docker network ${NETWORK_NAME}
fi
}
# nginx
init_nginx(){
APP_NAME="nginx"
if [ -e ${APP_NAME} ];then return;fi
cd ${HOME}
mkdir ${APP_NAME} && cd ${APP_NAME}
if [ ! -e etc_nginx ];then mkdir etc_nginx;fi
# 创建软连接,方便静态资源管理
# ln -s target_dir link_name
docker run --name tmp-container-nginx -d nginx:stable-alpine
docker cp tmp-container-nginx:/etc/nginx/nginx.conf $(pwd)/etc_nginx/nginx.conf
docker cp tmp-container-nginx:/etc/nginx/conf.d $(pwd)/etc_nginx/conf.d/
docker rm -f tmp-container-nginx
}
# mysql
init_mysql(){
APP_NAME="mysql"
if [ -e ${APP_NAME} ];then return;fi
cd ${HOME}
mkdir ${APP_NAME} && cd ${APP_NAME}
if [ ! -e etc ];then mkdir etc;fi
if [ ! -e var_lib_mysql ];then mkdir var_lib_mysql;fi
docker run --rm --name tmp-container-mysql -d -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0
docker cp tmp-container-mysql:/etc/my.cnf $(pwd)/etc/my.cnf
sed -i "s@/var/run/mysqld/mysqld.sock@/var/lib/mysql/mysql.sock@g" etc/my.cnf
docker rm -f tmp-container-mysql
}
init_nginx
init_mysql
init_network
启动脚本
常用容器 docker-compose.yml
services:
nginx:
container_name: nginx
image: nginx:alpine
ports:
- "443:443/tcp"
- "443:443/udp"
# - "442:442"
environment:
TZ: Asia/Shanghai
networks:
- free
volumes:
- ./nginx/etc_nginx/conf.d/:/etc/nginx/conf.d/
- ./nginx/etc_nginx/nginx.conf:/etc/nginx/nginx.conf:rw
- ./nginx/js:/js
mem_limit: 32m
mysql:
container_name: mysql
image: mysql:8.0
ports:
- "3306:3306"
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 123456
networks:
- free
volumes:
- ./mysql/var_lib_mysql:/var/lib/mysql
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
mem_limit: 1024m
mysql2:
container_name: mysql2
image: mysql:8.0
ports:
- "3307:3306"
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 123456
networks:
- free
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
mem_limit: 1024m
redis:
container_name: redis
image: redis:7.0-alpine
# redis-stack-server支持Json等格式
ports:
- "6379:6379"
environment:
TZ: Asia/Shanghai
networks:
- free
volumes:
- ./redis/data:/data
command: --appendonly yes --requirepass "123456"
mem_limit: 64m
# redis-stack-server:
# container_name: redis-stack-server
# image: redis/redis-stack-server
# ports:
# - "6380:6379"
# environment:
# TZ: Asia/Shanghai
# REDIS_ARGS: " --appendonly yes --requirepass 123456"
# networks:
# - free
# volumes:
# - ./redis-stack-server/data:/data
# mem_limit: 64m
mongo:
container_name: mongo
image: mongo:7.0
ports:
- "27017:27017"
environment:
TZ: Asia/Shanghai
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: 123456
networks:
- free
volumes:
- ./mongo/data_db:/data/db
- ./mongo/data_configdb:/data/configdb
mem_limit: 512m
command: --auth
# web ui
# mongo-express:
# container_name: mongo-express
# image: mongo-express
# ports:
# - 27018:8081
# environment:
# ME_CONFIG_BASICAUTH_USERNAME: mongo
# ME_CONFIG_BASICAUTH_PASSWORD: 123456
# ME_CONFIG_MONGODB_URL: mongodb://admin:123456@mongo:27017/
# networks:
# - free
nacos:
container_name: nacos
image: nacos/nacos-server:v2.3.2-slim
ports:
- "8848:8848"
- "9848:9848"
environment:
MODE: standalone
NACOS_AUTH_ENABLE: true
NACOS_AUTH_IDENTITY_KEY: serverIdentity
NACOS_AUTH_IDENTITY_VALUE: security
NACOS_AUTH_TOKEN: SecretKEY01234567890123456789012345678901234567890123456789012345678
LOGGING_LEVEL_ROOT: OFF
networks:
- free
volumes:
- ./nacos/home_nacos_data:/home/nacos/data
mem_limit: 1024m
seata:
container_name: seata
image: seataio/seata-server:2.0.0-slim
ports:
- "7091:7091"
- "8091:8091"
environment:
# 可指定SEATA_IP环境变量,指定seata-server的ip地址,避免注册为容器内部IP
# SEATA_IP:
SEATA_REGISTRY_TYPE: nacos
SEATA_REGISTRY_PREFERRED_NETWORKS: 192.168.*
SEATA_REGISTRY_NACOS_APPLICATION: seata-server
SEATA_REGISTRY_NACOS_SERVER_ADDR: nacos:8848
SEATA_REGISTRY_NACOS_GROUP: SEATA_GROUP
SEATA_REGISTRY_NACOS_USERNAME: nacos
SEATA_REGISTRY_NACOS_PASSWORD: nacos
networks:
- free
mem_limit: 1024m
depends_on:
- nacos
rabbitmq:
container_name: rabbitmq
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
environment:
RABBITMQ_DEFAULT_USER: rabbitmq
RABBITMQ_DEFAULT_PASS: 123456
RABBITMQ_ERLANG_COOKIE: "rabbitmq_cookie"
RABBITMQ_NODENAME: "rabbit@rabbitmq"
networks:
- free
mem_limit: 1024m
volumes:
- ./rabbitmq/var_lib_rabbitmq:/var/lib/rabbitmq
# 加入集群
# docker exec rabbitmq2 rabbitmqctl stop_app
# docker exec rabbitmq2 rabbitmqctl join_cluster --ram rabbit@rabbitmq
# docker exec rabbitmq2 rabbitmqctl start_app
# docker exec rabbitmq rabbitmqctl cluster_status
# postgres:
# container_name: postgres
# # image: postgres:16
# image: postgis/postgis:16-master
# ports:
# - '5432:5432'
# environment:
# - POSTGRES_PASSWORD=123456
# - TZ=Asia/Shanghai
# networks:
# - free
# volumes:
# - ./postgres/var_lib_postgresql_data:/var/lib/postgresql/data
# mem_limit: 1024m
# mssql:
# container_name: mssql
# image: mcr.microsoft.com/mssql/server:2022-latest
# ports:
# - "1433:1433"
# environment:
# TZ: "Asia/Shanghai"
# ACCEPT_EULA: "Y"
# MSSQL_SA_PASSWORD: "Pwd123456"
# networks:
# - free
# mem_limit: 2048m
# mqnamesrv:
# container_name: mqnamesrv
# image: apache/rocketmq:5.1.3
# ports:
# - "9876:9876"
# environment:
# - TZ=Asia/Shanghai
# - JAVA_OPT_EXT=-server -Xms256m -Xmx256m -Xmn256m
# networks:
# - free
# volumes:
# - ./rocketmq/namesrv_root_logs:/root/logs \
# - ./rocketmq/namesrv_root_store:/root/store \
# mem_limit: 256m
# command: ./mqnamesrv
# # WSL下需要设置../conf/broker.conf中追加 brokerIP1=127.0.0.1 ,其他则设为外网IP
# # docker exec mqbroker echo "brokerIP1=127.0.0.1" >> /home/rocketmq/rocketmq-5.1.3/conf/broker.conf
# # docker restart mqbroker
# mqbroker:
# container_name: mqbroker
# image: apache/rocketmq:5.1.3
# ports:
# - "10909:10909"
# - "10911:10911"
# - "10912:10912"
# environment:
# - TZ=Asia/Shanghai
# - JAVA_OPT_EXT=-server -Xms512m -Xmx512m -Xmn256m
# networks:
# - free
# volumes:
# - ./rocketmq/broker_root_logs:/root/logs \
# - ./rocketmq/broker_root_store:/root/store \
# mem_limit: 512m
# command: ./mqbroker -c ../conf/broker.conf -n mqnamesrv:9876
# depends_on:
# - mqnamesrv
# mq-dashboard:
# container_name: mq-dashboard
# image: apacherocketmq/rocketmq-dashboard
# ports:
# - "9877:8080"
# environment:
# - TZ=Asia/Shanghai
# - JAVA_OPTS=-Drocketmq.namesrv.addr=mqnamesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false
# networks:
# - free
# mem_limit: 1024m
# depends_on:
# - mqnamesrv
# - mqbroker
# minio:
# container_name: minio
# image: minio/minio
# ports:
# - "9000:9000"
# - "9001:9001"
# environment:
# TZ: Asia/Shanghai
# MINIO_ROOT_USER: minio
# MINIO_ROOT_PASSWORD: 123123123
# networks:
# - free
# volumes:
# - ./minio/data:/data
# - ./minio/root_minio:/root/.minio
# command: server "/data" --console-address ":9001"
# mem_limit: 1024m
portainer:
image: portainer/portainer-ce:alpine-sts
restart: always
container_name: portainer
ports:
- "9443:9443"
environment:
PUID: 1000
PGID: 1000
AGENT_SECRET: "portainer-agent"
networks:
- free
volumes:
- /etc/localtime:/etc/localtime
- /var/run/docker.sock:/var/run/docker.sock
- ./portainer/data:/data
mem_limit: 64m
# portainer-agent:
# container_name: portainer-agent
# image: portainer/agent
# restart: always
# ports:
# - 9001:9001
# environment:
# - AGENT_SECRET=portainer-agent
# volumes:
# - /var/run/docker.sock:/var/run/docker.sock
# - /var/lib/docker/volumes:/var/lib/docker/volumes
# mem_limit: 64m
# frpc:
# container_name: frpc
# image: ghcr.io/natfrp/frpc
# restart: always
# environment:
# NATFRP_TARGET: "5330626"
# NATFRP_TOKEN: "126b245fe032d4d0"
# networks:
# - free
# mem_limit: 32m
# syncthing:
# container_name: syncthing
# image: syncthing/syncthing
# restart: unless-stopped
# environment:
# TZ: Asia/Shanghai
# PUID: 1000
# PGID: 1000
# volumes:
# - ./syncthing/var_syncthing:/var/syncthing
# network_mode: host
# mem_limit: 128m
# showdoc:
# image: star7th/showdoc
# restart: always
# container_name: showdoc
# environment:
# TZ: Asia/Shanghai
# ports:
# - "4999:80"
# networks:
# - free
# volumes:
# - ./showdoc/var_www_html:/var/www/html
# mem_limit: 256m
# firefox:
# container_name: firefox
# image: jlesage/firefox
# ports:
# - "5800:5800"
# environment:
# VNC_PASSWORD: "123456"
# LANG: zh_CN.UTF-8
# TZ: Asia/Shanghai
# KEEP_APP_RUNNING: 1
# volumes:
# - ./firefox/config:/config
# - ./firefox/msyh:/usr/share/fonts/msyh
# mem_limit: 256m
# networks:
# - free
# java-app:
# container_name: java-app
# # restart: always
# image: ibm-semeru-runtimes:open-17-jdk
# deploy:
# resources:
# limits:
# # cpus: '0.50'
# memory: 2048M
# extra_hosts:
# - local.example.com:192.168.1.100
# volumes:
# - ./app.jar:/app.jar
# - /etc/localtime:/etc/localtime
# ports:
# - "8080:8080"
# environment:
# - TZ=Asia/Shanghai
# entrypoint: java -server -Dfile.encoding=UTF-8 -jar app.jar
networks:
free:
name: free
# 使用已有网络
external: true
其他
更新镜像
docker compose pull
启动
docker compose up -d
停止
docker compose stop
如果配置文件有更新,不能直接docker compose restart
需要先stop
再up
。
nacos经常有破坏性更新,需要更新配置文件才能正常启动,建议固定好版本号。
其他几个经常保持最新版,未发现问题。
如有错漏,欢迎指正!谢绝转载。