docker-compose安装wordpress
搭建环境:
1.阿里云服务ECS
2.Centos_7_03
部署工具:
1.Docker
2.Docker-compose
3.Wordpress 和 MySql5.7
Docker:
根据官方文档,Docker分为Community Edition (CE)和Enterprise Edition (EE)两个版本,我们作为学习和个人使用,当然选择的是Community Edition (CE),安装步骤如下:
方法1:
// 步骤1 - 为了确保没有安装过老的Docker版本,先将已经安装的Docker从宿主机上删除(如果是在使用中的正式服务器,请谨慎执行此步):
$ sudo yum remove docker \
docker-common \
docker-selinux \
docker-engine
// 步骤2 - 安装Docker所需的包:
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
// 步骤3 - 配置到稳定的Docker CE安装库:
$ sudo yum-config-manager \
--add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
// 步骤4 - 安装Docker CE:
$ sudo yum install docker-ce
// 步骤5 - 启动Docker服务:
$ sudo systemctl start docker
// 步骤6 - 测试是否安装成功:// 可以通过查看版本的形式确认安装是否成功:
$ docker --version // 也可以通过直接运行hello-world容器来确认安装是否成功:
$ docker run hello-world
方法2:
//步骤一:yum更新(root权限)
[root@iz2ze819j38l9ba9luaudkz ~]# yum update
//步骤二:安装docker
[root@iz2ze819j38l9ba9luaudkz ~]#yum -y install docker-io
//步骤三:开启docker后台服务
[root@iz2ze819j38l9ba9luaudkz ~]#service docker start
//步骤四:运行 run hello-world验证安装是否成功
[root@iz2ze819j38l9ba9luaudkz ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
rying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for docker.io/hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
Docker Compose:
为了便于使用,我们需要安装Docker Compose来管理和运行Docker容器,Docker Compose的安装步骤如下:
方法1:
// 步骤1 - 下载安装文件:
$ sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose
// 步骤2 - 给已下载的安装文件添加执行权限:
$ sudo chmod +x /usr/bin/docker-compose
// 步骤3 - 测试是否安装成功:
$ docker-compose --version
方法2:
//步骤一:验证有没有安装Python-pip包
[root@iz2ze819j38l9ba9luaudkz ~]#python-V
//若没有Python-pip包,则执行: yum -y install epel-release
//执行成功后,在执行:yum -y install python-pip
//步骤二:更新pip到最新[root@iz2ze819j38l9ba9luaudkz ~]#pip install --upgrade pip//步骤三:安装docker-compose[root@iz2ze819j38l9ba9luaudkz ~]#pip install docker-compose//步骤四:检查安装是否成功[root@iz2ze819j38l9ba9luaudkz ~]#docker-compose -version
部署WordPress和MySql容器:
先创建一个工作目录,并创建名为docker-compose.yml的文件:
$ cd /usr/
$ sudo mkdir myblog && cd myblog
$ sudo vim docker-compose.yml
将如下内容保存在docker-compose.yml文件中:
version: '3.3'services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: your-mysql-root-password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wp_site:/var/www/html
ports:
- "80:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpressvolumes:
db_data:
wp_site:
关于Composer所使用的的yml文件的语法,请参考官方文档,这里就不一一解释了(因为我自己也是刚入门,只会几个基本的用法)。
和官方给的Demo不同,上述yml文件中,我在volumes中添加了wp_site的卷,并将其挂在到wordpress容器中,这样,当容器被停止或者删除后,重新安装并启动wordpress容器时,已安装的plugins也可以直接继续使用,而不是重新安装。
此时,我们直接使用docker-compose命令启动容器:
$ sudo docker-compose up -d
启动之后,我们就可以通过 http://ip:80 (因为我们绑定的是宿主机的80端口) 来访问WordPress(若不能访问,请在阿里云控制台-云服务器ECS-网络和安全-安全组-配置规则,添加并开启指定的端口的公网访问权限)
如果需要关闭服务,则执行如下命令:
$ sudo docker-compose down
网友评论