How to Setup WordPress with Docker in Ubuntu 16.04

本文介绍如何使用Docker Compose快速安装WordPress,包括安装MySQL及PHPMyAdmin,并演示主题安装及WordPress版本更新。

This article covers how to setup wordpress with docker using docker-compose quickly. WordPress requires LAMP or LEMP stack but the official wordpress docker image contains PHP and Apache, leaving us to install only MySQL docker image. But for managing MySQL through web interface, we will also install PHPMyAdmin docker image. Docker compose makes it easy to deal with the co-ordination of processes inside containers. Therefore before starting make sure you have the latest version of docker compose installed in your host machine.

1. Prerequisites for wordpress with docker

You must have both docker and docker-compose available in your host machine. We will use docker-compose version '2' therefore you may need to update docker compose in the host machine. You can update the docker compose from the official github page. Once you have updated the compose, find the version of both docker and compose in the host machine.

root@demohost:~# docker -v
Docker version 1.12.3, build 6b644ec
root@demohost:~# docker-compose -version
docker-compose version 1.10.0, build 4bd6f1a

2. Install MySQL

We will first define a project directory where all the resources will be knitted by compose to build our image. Therefore to begin with, we will also create docker-compose.yml inside our project directory.

root@demohost:~# mkdir docker_wp
root@demohost:~# cd docker_wp

Create/Edit docker-compose.yml to create a MySQL instance with a volume mount for data persistence. We have also specified our top level networks with key 'back'. Service level ( WordPress, PHPMyAdmin) networks will specify this 'Key' to connect to top level networks.

root@demohost:~/docker_wp# vi docker-compose.yml

version: '2'
services:
database:
image: mysql:5.7.17
restart: always
volumes:
- database_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- back
networks:
back:
volumes:
database_data:

4624570-a0f58accc268fafa.png
Docker compose for MySQL

Now start the container with docker-compose

root@demohost:~/docker_wp# docker-compose up -d
Creating network "dockerwp_back" with the default driver
Creating volume "dockerwp_db_data" with default driver
Pulling database (mysql:5.7.17)...
5.7.17: Pulling from library/mysql
--------------------------
--------------------------
a1b9627588c7: Pull complete
Digest: sha256:5e2ec5964847dd78c83410f228325a462a3bfd796b6133b2bdd590b71721fea6
Status: Downloaded newer image for mysql:5.7.17
Creating dockerwp_database_1

List the container

root@demohost:~/docker_wp# docker ps -a

3. Install PHPMyAdmin

To install PHPMyAdmin in the container, we will append the following code snippets in docker-compose.yml under services section.

phpmyadmin:
depends_on:
- database
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: database
MYSQL_ROOT_PASSWORD: root
networks:
- back

The phpmyadmin service depends on our 'database' service that we have defined earlier in docker compose file. The docker compose will grab official phpmyadmin and port no 8080 of docker host will be mapped to port 80 of the container. The environment variable PMA_HOST defines the address/host name of the MySQL server and MYSQL_ROOT_PASSWORD specify the root password for MySQL server and it must be same with MYSQL_ROOT_PASSWORD value that was set for under 'database' service.

Start-up the application group again.

root@demohost:~/docker_wp# docker-compose up -d
Pulling phpmyadmin (phpmyadmin/phpmyadmin:latest)...
latest: Pulling from phpmyadmin/phpmyadmin
0a8490d0dfd3: Pull complete
-----------------------------
98ff89f652db: Pull complete
Digest: sha256:942030a8ef4e2ab49ae5d9a60a27c94eed4e9003cd2bd01a62c501b92fdabfda
Status: Downloaded newer image for phpmyadmin/phpmyadmin:latest
dockerwp_database_1 is up-to-date
Creating dockerwp_phpmyadmin_1

List the containers

root@demohost:~/docker_wp# docker ps -a

Open your favourite browser and point it to http://Docker_Host_IP:8080, you will be greeted by phpmyadmin login page. Login with user root and password as root. If you have set MYSQL_ROOT_PASSWORD environment value to something else, then login with that password.

4624570-5b0d4cd36af25e65.png
Wordpress login page

4. Install WordPress with docker

Finally we will setup wordpress docker container by prepending following code snippets under services section.

wordpress:
depends_on:

  • database
    image: wordpress:4.6
    restart: always
    volumes:
  • ./wp-content:/var/www/html/wp-content
    environment:
    WORDPRESS_DB_HOST: database:3306
    WORDPRESS_DB_PASSWORD: root
    ports:
  • 80:80
  • 443:443
    networks:
  • back

The wordpress service depends on 'database' service and docker compose will pull wordpress with docker version 4.6 We have chosen the version 4.6 deliberately so that we can update the wordpress in the final step. The environment variable WORDPRESS_DB_HOST is set to MySQL hostname followed by port no and WORDPRESS_DB_PASSWORD is set to 'root' which we have assigned in MYSQL_ROOT_PASSWORD under 'database' section. The port no 80 and 443 of docker host is mapped to containers port no 80 and 443. The wordpress docker container is also connected to top-level networks 'back'.

Our final docker-compose.yml looks like below.

4624570-87c33e11fe866e42.png
Wordpress with docker compose

Next, start-up all the three applications together.

root@demohost:~/docker_wp# docker-compose up -d
Pulling wordpress (wordpress:4.7.2)...
4.7.2: Pulling from library/wordpress
5040bd298390: Already exists
568dce68541a: Pull complete
-----------------------------
Digest: sha256:a0ce5d2666bff9fe17fd8d5c6b5ba3094b7109e97af8c69a0c31057a77e24899
Status: Downloaded newer image for wordpress:4.7.2
dockerwp_database_1 is up-to-date
dockerwp_phpmyadmin_1 is up-to-date
Creating dockerwp_wordpress_1

List the containers.

root@demohost:~/docker_wp# docker ps -a

Access wordpress site by pointing your browser at http://Docker_Host_IP You will be prompted to choose a language.

4624570-a76c0dcf7d26f868.png
Wordpress with docker installation

Provide site title, user name, password, email and click "Install WordPress"

4624570-4d8544518babb4d2.png
Wordpress site settings

Login to wordpress site by typing username/email address and password that you have provided in the last step.

4624570-b38bbcc7784c0e75.png
Wordpress login

You will be redirected to wordpress dashboard.

4624570-de06ee9379f08b4c.png
Wordpress Dashboard

Click plugins from left side menu bar. You will find two plugins by the name "Akismet" and "Hello Dolly"

4624570-ef46412d15b5d2bc.png
Wordpress plugins

Remove the "Hello Dolly" plugin by changing to wp-content/plugins directory of wordpress project

root@demohost:~/docker_wp# cd wp-content/plugins/
root@demohost:~/docker_wp/wp-content/plugins# ls
akismet hello.php index.php
root@demohost:~/docker_wp/wp-content/plugins# rm hello.php

Refresh the page, Hello Dolly will not be listed now. Click 'Themes' under 'Appearance' from left side bar. The default themes will be listed in this page.

4624570-d526168db3bf677c.png
Wordpress themes

Since we want to place a theme of our choice, navigate to wp-content/themes directory and remove the themes.

root@demohost:~/docker_wp# cd wp-content/themes/
root@demohost:~/docker_wp/wp-content/themes# ls
index.php twentyfifteen twentyseventeen twentysixteen
root@demohost:~/docker_wp/wp-content/themes# rm -rf twenty*

Download a theme of your choice. We will download the theme https://wordpress.org/themes/nisarg/ Unzip the theme and remove the zip file.

root@demohost:~/docker_wp/wp-content/themes# wget https://downloads.wordpress.org/theme/nisarg.1.2.6.zip
root@demohost:~/docker_wp/wp-content/themes# unzip nisarg.1.2.6.zip
root@demohost:~/docker_wp/wp-content/themes# rm -rf nisarg.1.2.6.zip

Now refresh the themes page, the downloaded theme will be listed there. Activate the theme by clicking "Activate"

4624570-701ba0637539e92a.png
Wordpress activate nisarg theme

Since we have installed wordpress version 4.6, you will be prompted to update wordpress. Don't click "Please update now" at this stage. We need to change the version of wordpress in docker-compose.yml. Edit docker-compose and change the wordpress version from 4.6 to 4.7.2

wordpress:
depends_on:
- database
image: wordpress:4.7.2
------------------------
------------------------

Stop/Restart the wordpress container

root@demohost:~# docker stop dockerwp_wordpress_1
root@demohost:~# docker rm dockerwp_wordpress_1
root@demohost:~# docker-compose up -d

Now click "Please update now"

4624570-998a923634e0cdf9.png
Wordpress update

Click "Update Now"

4624570-7c75ef67ce6ae0a6.png
Wordpress update to newer version

WordPress update process will be visible to you.

4624570-8952d5b601637188.png
Wordpress update process

To check the version of wordpress, click "Updates". The wordpress version will be listed in this page.

4624570-1b47b6d92701c3aa.png
Wordpress check version

Conclusions:

Thats all for wordpress with docker in Ubuntu 16. In this article, we have covered installation of wordpress using docker compose, installing a new theme and updating wordpress version. You should have a working wordpress site by now from where you can start modifying themes, installing new plugins etc. by editing files inside wp-content folder of the wordpress with docker project.

<think> 首先,用户的问题是:“docker import ubuntu16.04.tar执行成功的前提是本地有ubuntu16.04.tar文件吗” 我需要回答这个问题,并用中文。问题核心是关于 Docker 的 `import` 命令是否需要指定的 tar 文件存在于本地文件中。 回想一下 Docker 的 `docker import` 命令: - 这个命令用于从一个 tarball 创建 Docker 镜像。 - 语法是 `docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]` - 输入可以是: - 一个本地文件路径(如 `ubuntu16.04.tar`) - 一个 URL - 或者 `-` 表示从标准输入读取。 所以,对于命令 `docker import ubuntu16.04.tar`: - 这里的 `ubuntu16.04.tar` 被视为一个本地文件路径。 - 因此,**前提是**这个文件 `ubuntu16.04.tar` 必须存在于当前目录或指定的路径中。 - 如果文件不存在,命令会失败。 关键点: - `docker import` 需要输入源存在。 - 对于本地文件路径,文件必须存在。 - 其他选项如 URL 或 STDIN 不需要本地文件,但在这个特定命令中,指定的是文件名。 因此,答案是:是的,执行 `docker import ubuntu16.04.tar` 成功的前提是本地有 `ubuntu16.04.tar` 文件。 现在,我需要构建回答: - 回答必须用中文。 - 回答完问题后,必须提出 3-5 个相关问题。 - 相关问题必须在末尾,以 "§§相关问题§§" 开头,禁止换行分散。 - 格式参考示例:先回答问题,然后代码块(如果适用),然后相关问题。 问题不涉及代码,所以我可以直接解释。 回答结构: 1. 简要解释 `docker import` 命令的作用。 2. 说明输入源可以是本地文件、URL 或 STDIN。 3. 针对这个命令,强调指定本地文件时,文件必须存在。 4. 给出结论。 然后,提出相关问题: - 基于问题和回答,提出 3-5 个相关问题。 - 问题应该相关,比如关于 Docker import、export、镜像创建等。 - 每个问题以数字开头加点(如 "1. ...")。 相关问题建议: 1. `docker import` 和 `docker load` 有什么区别? 2. 如何从 URL 导入 Docker 镜像? 3. 创建 Docker 镜像的其他方法有哪些? 4. 如果本地文件不存在,命令会有什么错误消息? 5. 如何检查 Docker 镜像是否成功导入? 确保相关问题数量在 3-5 个。 完整回答格式: - 回答问题段落。 - 然后 "§§相关问题§§" - 紧接着相关问题列表:1. ... 2. ... 3. ... 现在,用中文撰写回答。 </think> ### `docker import ubuntu16.04.tar` 执行前提分析 执行 `docker import ubuntu16.04.tar` 命令成功的前提是**本地必须存在 `ubuntu16.04.tar` 文件**。原因如下: #### 关键解析 1. **命令原理**: - `docker import` 是 Docker 用于从一个 tarball(压缩包)创建镜像的命令。 - 在命令行中指定 `ubuntu16.04.tar` 时,Docker 会将其解释为**本地文件路径**。 - 如果文件不存在,命令会直接失败,并提示错误如 `open ubuntu16.04.tar: no such file or directory`。 2. **输入源要求**: - 支持三种输入类型: - **本地文件**:如 `ubuntu16.04.tar`,必须存在于当前工作目录或绝对路径中。 - **URL**:如 `http://example.com/image.tar`,文件可远程获取。 - **标准输入(STDIN)**:使用 `-` 符号,如 `cat image.tar | docker import -`。 - 本例中未使用 URL 或 STDIN,因此文件必须在本地。 3. **完整执行流程**: - 步骤: ```bash # 前提:确保文件存在 ls ubuntu16.04.tar # 验证文件 docker import ubuntu16.04.tar my-ubuntu:16.04 # 执行导入 docker images # 检查新镜像 ``` - 若文件缺失,需先通过下载或创建获得: ```bash wget https://example.com/ubuntu16.04.tar # 远程下载 # 或手动创建 tarball ``` #### 注意事项 - **文件内容要求**:tarball 必须是有效的 Docker 镜像导出格式(如通过 `docker export` 生成)。 - **替代命令**:若需从远程源导入,应改用 URL 语法,如 `docker import http://example.com/ubuntu16.04.tar`。 - **错误处理**:文件不存在时会返回错误代码 `1`,可通过 `echo $?` 验证。 > ✅ 总结:本地文件存在是此命令成功的绝对前提,否则无法读取数据创建镜像。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值