1.安装docker-compose
pip install docker-compose
2.编写docker-compose.yml
包含了四个服务
- db
- server
- web
- mq
version: '3'
services:
db:
image: docker.io/mysql:5.7
container_name: mysql
privileged: true
env_file:
- "./FasterRunner/.env"
environment:
# 设置默认数据库和root默认密码,如果宿主机中/var/lib/mysql已经存在,这两个设置都不会生效
- MYSQL_DATABASE="${FASTER_DB_NAME}"
- MYSQL_USER="${FASTER_DB_USERNAME}"
- MYSQL_ROOT_PASSWORD="${FASTER_DB_ROOT_PASSWORD}"
volumes:
- "/var/lib/mysql:/var/lib/mysql" # 挂载宿主机的mysql数据到docker中
- "./FasterRunner/docker_mysql.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf" # 把宿主机文件挂在到容器的mysql配置文件
# 端口映射,格式 宿主机端口:容器端口
ports:
- "3306:3306"
restart: always
# 容器开机启动命令
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
networks:
- fasterrunner
server:
image: fasterrunner:latest
container_name: fasterrunner
privileged: true
volumes:
- ./FasterRunner:/opt/workspace/FasterRunner
- ./FasterRunner/nginx.conf:/etc/nginx/sites-enabled/nginx.conf
ports:
- "8000:8000"
networks:
- fasterrunner
web:
image: fasterweb:latest
container_name: fastweb
privileged: true
volumes:
- ./FasterWeb/dist:/usr/share/nginx/html
- ./FasterWeb/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:8080"
networks:
- fasterrunner
mq:
image: rabbitmq:management-alpine
container_name: rabbitmq
env_file:
- "./FasterRunner/.env"
environment:
- RABBITMQ_DEFAULT_USER=${FASTER_MQ_USER}
- RABBITMQ_DEFAULT_PASS=${FASTER_MQ_PASSWORD}
restart: always
ports:
- "15672:15672"
- "5672:5672"
networks:
- fasterrunner
networks: # 指定了内网,可以在配置文件中直接使用容器名字,但是需要先在宿主机创建网络
fasterrunner:
external: true
3.编写环境变量.env文件(在FasterRunner项目的根据路径)
# .env文件
# MQ
FASTER_MQ_USER=my_mq_username # rabbitmq容器创建的默认用户名
FASTER_MQ_PASSWORD=my_mq_password # rabbitmq容器创建的默认密码
# MySQL
FASTER_HOST=10.0.3.67 # 服务器本机ip地址
FASTER_DB_NAME=my_db # mysql容器创建的默认数据库名
FASTER_DB_USERNAME=my_db_user # mysql容器创建的默认用户名
FASTER_DB_PASSWORD=my_db_password # mysql容器创建的默认用户密码
FASTER_DB_ROOT_PASSWORD=my_db_root_password # mysql容器创建的默认root密码
4.构建镜像
在docker-compose中使用了4个镜像,下面两个是属于自定义的,所以需要预先构建.剩下的两个会自动在docker官方拉取
image: fasterrunner:latest
image: fasterweb:latest
# 进入FasterRunner项目根目录
(fasterenv) [faster@faster_3_57 FasterRunner]$ cd /home/faster/FasterRunner
(fasterenv) [faster@faster_3_57 FasterRunner]$ pwd
/home/faster/FasterRunner
# 构建fasterrunner:latest镜像
docker build -t fasterrunner:latest .
# 进入FasterWeb项目根目录
(fasterenv) [faster@faster_3_57 FasterRunner]$ cd /home/faster/FasterWeb/
(fasterenv) [faster@faster_3_57 FasterWeb]$ pwd
/home/faster/FasterWeb
# 构建 fasterweb:latest镜像()
docker build -t fasterweb:latest .
5.文件结构如下
(fasterenv) [faster@faster_3_57 ~]$ ls -l
-rw-rw-r-- 1 faster faster 2010 Dec 31 15:03 docker-compose.yml
drwxrwxr-x 9 faster faster 4096 Jan 6 09:43 FasterRunner
drwxrwxr-x 8 faster faster 4096 Jan 6 15:18 FasterWeb
6.启动所有服务
# 在docker-compose.yml所在的目录
docker-compose up # 首次启动会提示需要重新network
# 后台运行模式
docker-compose up -d
7.docker-compose常用命令
# 停止并清理容器
docker-compose down
# 查看所有容器端口映射
docker-compose ps
# 查看所有容器的进程
docker-compose top
# 查看所有容器的配置(环境变量)
docker-compose config
# 查看容器的日志
docker-compose logs -f web
# 进入容器
docker exec -it fasterrunner /bin/sh
网友评论