用docker-compose部署WordPress服务
1、下载安装docker compose
[root@localhost ~]# curl -L "https://get.daocloud.io/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 423 100 423 0 0 208 0 0:00:02 0:00:02 --:--:-- 208
100 11.6M 100 11.6M 0 0 1788k 0 0:00:06 0:00:06 --:--:-- 2966k
2、给权限
[root@localhost ~]# chmod 755 /usr/local/bin/docker-compose
3、编辑docker-compose文件
这是WordPress的服务docker compose文件配置,其他服务的可以去网上找
[root@localhost ~]# cat docker-compose.yml
version: '2' ## compose的版本
services: ## 服务
xd_db: ##指数据库服务
image: mariadb:latest ## 数据库的镜像
restart: always ## 服务开机自起
volumes: ## 设置卷的路径
- db_data:/var/lib/mysql ## 宿主机路径:容器路径
environment: ## 设置环境变量
MYSQL_ROOT_PASSWORD: wp_password
xd_wp: ## 指WordPress服务
image: wordpress:latest ## 指定镜像
restart: always ## 开机自启
depends_on: ## 指定服务依赖
- xd_db
ports: ## 映射端口
- "8888:80"
environment: ## 设置环境变量
WORDPRESS_DB_HOST: xd_db:3306
WORDPRESS_DB_PASSWORD: wp_password
volumes: ## 设置卷的路径
db_data:
4、配置镜像仓库,在之前的基础上加上"http://hub.c.163.com",之后要重启
[root@localhost ~]# vi /etc/docker/daemon.json
{
"registry-mirrors":["https://dhq9bx4f.mirror.aliyuncs.com","http://hub.c.163.com"],"insecure-registries":["192.168.200.70:5000"]
}
~
~
~
~
~
~
~
~
~
~
"/etc/docker/daemon.json" 3L, 133C written
[root@localhost ~]# systemctl restart docker
5、启动容器
[root@localhost ~]# docker-compose up -d
可以用docker-compose ps -a 查看服务是否启动
[root@localhost ~]# docker-compose ps -a
Name Command State Ports
----------------------------------------------------------------------------
root_xd_db_1 docker-entrypoint.sh mysqld Up 3306/tcp
root_xd_wp_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8888->80/tcp
使用浏览器登录WordPress
关于docker-compose的命令
通过–help命令可以查看关于docker-compose的一些命令,它和docker的使用差不多
[root@localhost ~]# docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [--] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
-c, --context NAME Specify a context name
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--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 specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert keys
in v3 files to their non-Swarm equivalent (DEPRECATED)
--env-file PATH Specify an alternate environment file
Commands:
build Build or rebuild services
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
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 services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show version information and quit
例如:1、查看服务信息
[root@localhost ~]# docker-compose ps -a
Name Command State Ports
-------------------------------------------------------------
root_xd_db_1 docker- Up 3306/tcp
entrypoint.sh
mysqld
root_xd_wp_1 docker- Up 0.0.0.0:8888->80/
entrypoint.sh tcp
apach ...
2、查看所有镜像的列表
[root@localhost ~]# docker-compose images
Container Repository Tag Image Id Size
------------------------------------------------------------
root_xd_db_1 mariadb latest 3a348a04a815 406.5 MB
root_xd_wp_1 wordpress latest bc5f6567b763 549.8 MB
3、关闭服务并移除,并且查看状态
[root@localhost ~]# docker-compose down
Stopping root_xd_wp_1 ... done
Stopping root_xd_db_1 ... done
Removing root_xd_wp_1 ... done
Removing root_xd_db_1 ... done
Removing network root_default
[root@localhost ~]# docker-compose ps -a
Name Command State Ports
------------------------------
4、开启容器,并查看状态
-d : 在后台运行
[root@localhost ~]# docker-compose up -d
Creating network "root_default" with the default driver
Creating root_xd_db_1 ... done
Creating root_xd_wp_1 ... done
[root@localhost ~]# docker-compose ps -a
Name Command State Ports
-------------------------------------------------------------
root_xd_db_1 docker- Up 3306/tcp
entrypoint.sh
mysqld
root_xd_wp_1 docker- Up 0.0.0.0:8888->80/
entrypoint.sh tcp
apach ...
5、关闭服务,并查看服务状态
[root@localhost ~]# docker-compose stop
Stopping root_xd_wp_1 ... done
Stopping root_xd_db_1 ... done
[root@localhost ~]# docker-compose ps -a
Name Command State Ports
-------------------------------------------------------------
root_xd_db_1 docker-entrypoint.sh mysqld Exit 0
root_xd_wp_1 docker-entrypoint.sh apach Exit 0
...
6、开启服务,并查看状态
[root@localhost ~]# docker-compose start
Starting xd_db ... done
Starting xd_wp ... done
[root@localhost ~]# docker-compose ps -a
Name Command State Ports
-------------------------------------------------------------
root_xd_db_1 docker- Up 3306/tcp
entrypoint.sh
mysqld
root_xd_wp_1 docker- Up 0.0.0.0:8888->80/
entrypoint.sh tcp
apach ...
7、删除所有容器,在查看状态(删除后可继续用 up 启动)
[root@localhost ~]# docker-compose rm -s
Stopping root_xd_wp_1 ... done
Stopping root_xd_db_1 ... done
Going to remove root_xd_wp_1, root_xd_db_1
Are you sure? [yN] y
Removing root_xd_wp_1 ... done
Removing root_xd_db_1 ... done
[root@localhost ~]# docker-compose ps -a
Name Command State Ports
------------------------------