Docker Compose是一个用于定义和运行多个Docker容器的工具。它使用YAML文件来配置应用程序的服务、网络和卷等方面,使得在单个主机上进行部署更加简单。通过定义一个Compose文件,你可以一次性启动、停止和管理整个应用程序的多个容器。
Compose文件包含了应用程序的各种服务的配置选项,如镜像、端口映射、环境变量、卷挂载等。你只需在Compose文件中定义所需的服务和其配置,然后使用docker-compose up命令即可启动整个应用程序。此外,你还可以使用docker-compose down命令来停止和删除所有相关的容器。
1. docker-compose部署
[root@localhost bin]# pwd
/usr/local/bin
[root@localhost bin]# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
100 12.1M 100 12.1M 0 0 5094 0 0:41:40 0:41:40 --:--:-- 5509[root@localhost bin]# ll
total 12440
-rw-r--r--. 1 root root 12737304 Mar 10 02:25 docker-compose
[root@localhost bin]# chmod 777 docker-compose[root@localhost bin]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c
查看帮助
[root@localhost ~]# docker-compose --help
Define and run multi-container applications with Docker.Usage:docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]docker-compose -h|--helpOptions:-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)-p, --project-name NAME Specify an alternate project name (default: directory name)--verbose Show more output--no-ansi Do not print ANSI control characters-v, --version Print version and exit-H, --host HOST Daemon socket to connect to--tls Use TLS; implied by --tlsverify--tlscacert CA_PATH Trust certs signed only by this CA--tlscert CLIENT_CERT_PATH Path to TLS certificate file--tlskey TLS_KEY_PATH Path to TLS key file--tlsverify Use TLS and verify the remote--skip-hostname-check Don't check the daemon's hostname against the name specifiedin the client certificate (for example if your docker hostis an IP address)--project-directory PATH Specify an alternate working directory(default: the path of the Compose file)Commands:build Build or rebuild servicesbundle Generate a Docker bundle from the Compose fileconfig Validate and view the Compose filecreate Create servicesdown Stop and remove containers, networks, images, and volumesevents Receive real time events from containersexec Execute a command in a running containerhelp Get help on a commandimages List imageskill Kill containerslogs View output from containerspause Pause servicesport Print the public port for a port bindingps List containerspull Pull service imagespush Push service imagesrestart Restart servicesrm Remove stopped containersrun Run a one-off commandscale Set number of containers for a servicestart Start servicesstop Stop servicestop Display the running processesunpause Unpause servicesup Create and start containersversion Show the Docker-Compose version information
2. docker-compose.yml模板
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
包含了两个服务:web 和 db。web 服务使用 Nginx 镜像并将容器的 80 端口映射到宿主机的 8080 端口;db 服务使用 MySQL 镜像,并设置了一些环境变量用于配置 MySQL 实例。解析:
version: 指定了 Docker Compose 文件的版本。
services: 定义了各个服务。
web 和 db 是服务的名称,可以根据实际情况自行命名。
image: 指定了服务所使用的镜像。
ports: 定义了端口映射关系,格式为 "宿主机端口:容器端口"。
environment: 设置了该服务运行时需要的环境变量,这里设置了 MySQL 的 root 密码、数据库名、用户名和密码。
3. 使用compose搭建WordPress
[root@localhost ~]# cd /home/
[root@localhost home]# ll
total 0# 创建项目目录
[root@localhost home]# mkdir wordpress
[root@localhost home]# vi docker-compose.yml
[root@localhost home]# cat docker-compose.yml
version: "3"
services:db:image: mysql:8.0command:- --default_authentication_plugin=mysql_native_password- --character-set-server=utf8mb4- --collation-server=utf8mb4_unicode_civolumes:- db_data:/var/lib/mysqlrestart: alwaysenvironment:MYSQL_ROOT_PASSWORD: 123456MYSQL_DATABASE: wordpressMYSQL_USER: wordpressMYSQL_PASSWORD: wordpresswordpress:depends_on:- dbimage: wordpress:latestports:- "8000:80"restart: alwaysenvironment:WORDPRESS_DB_HOST: db:3306WORDPRESS_DB_USER: wordpressWORDPRESS_DB_PASSWORD: wordpress
volumes:db_data:
启动项目
[root@localhost home]# docker-compose up[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
edeae04f9543 wordpress:latest "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:8000->80/tcp, :::8000->80/tcp home_wordpress_1
ebaa335a1d47 mysql:8.0 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 3306/tcp, 33060/tcp home_db_1#必须在项目目录中才能使用这个命令
[root@localhost home]# docker-compose psName Command State Ports
------------------------------------------------------------------------------------------------
home_db_1 docker-entrypoint.sh --def ... Up 3306/tcp, 33060/tcp
home_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8000->80/tcp,:::8000->80/tcp
关闭防火墙,通过浏览器进行访问
[root@localhost home]# systemctl stop firewalld
[root@localhost home]# setenforce 0