docker-compose文件的简介及使用
一、docker-compose 文件整合多个项目配置
version: '3.8'
services:
# MySQL 数据库服务
mysql:
image: mysql:5.7 # 使用官方的 MySQL 镜像,版本为 5.7
container_name: my_mysql
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: root_password # 设置 root 用户的密码
MYSQL_DATABASE: mydb # 创建一个名为 mydb 的数据库
MYSQL_USER: myuser # 创建一个名为 myuser 的用户
MYSQL_PASSWORD: mypassword # 设置 myuser 用户的密码
volumes:
- "./mysql/data:/var/lib/mysql"
- "./mysql/conf:/etc/mysql/confi.d"
- "./mysql/init:/docker-entrypoint-initdb.d"
ports:
- "3306:3306" # 将宿主机的 3306 端口映射到容器的 3306 端口
networks:
- my_net
aipro:
build: # 根据Dockerfile文件构建镜像
context: .
dockerfile: Dockerfile
container_name: aipro
ports:
- "8080:8080" # 将宿主机的 8080 端口映射到容器的 8080 端口(根据你的后端应用配置调整)
networks:
- my_net
depends_on:
- my_mysql # 确保 MySQL 服务先启动
# Nginx 服务,用于反向代理前端和后端
nginx:
image: nginx:latest # 使用官方的 Nginx 镜像,版本为 latest
container_name: my_nginx
volumes:
- "./nginx/nginx.conf:/etc/nginx/nginx.conf" # 使用自定义的 Nginx 配置文件
- "./nginx/frontend/dist:/usr/share/nginx/html" # 将前端应用的静态文件映射到 Nginx 的默认根目录
ports:
- "80:80" # 将宿主机的 80 端口映射到容器的 80 端口
depends_on:
- aipro # 确保后端服务先启动(如果 Nginx 需要代理到后端)
networks:
- my_net
networks:
my_net:
name: aipro
MacBook-Pro:dockercompose jd$ docker compose --help
Usage: docker compose [OPTIONS] COMMAND
Define and run multi-container applications with Docker
Options:
--all-resources Include all resources, even those not used by services
--ansi string Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto")
--compatibility Run compose in backward compatibility mode
--dry-run Execute command in dry run mode
--env-file stringArray Specify an alternate environment file
-f, --file stringArray Compose configuration files(指定compose文件的路径和名称)
--parallel int Control max parallelism, -1 for unlimited (default -1)
--profile stringArray Specify a profile to enable
--progress string Set type of progress output (auto, tty, plain, json, quiet) (default "auto")
--project-directory string Specify an alternate working directory
(default: the path of the, first specified, Compose file)
-p, --project-name string Project name
Commands:
attach Attach local standard input, output, and error streams to a service's running container
build Build or rebuild services
config Parse, resolve and render compose file in canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service
down Stop and remove containers, networks(停止并移除所有所有容器)
events Receive real time events from containers
exec Execute a command in a running container(在一个运行的容器中执行命令)
images List images used by the created containers
kill Force stop service containers
logs View output from containers (查看指定容器的日志)
ls List running compose projects
pause Pause services
port Print the public port for a port binding
ps List containers(列出所有的容器)
pull Pull service images
push Push service images
restart Restart service containers
rm Removes stopped service containers
run Run a one-off command on a service
scale Scale services
start Start services
stats Display a live stream of container(s) resource usage statistics
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers(创建并启动所有容器)
version Show the Docker Compose version information
wait Block until containers of all (or specified) services stop.
watch Watch build context for service and rebuild/refresh containers when files are updated
Run 'docker compose COMMAND --help' for more information on a command.
- 实战操作
docker compose up -d
docker compose ps