- Linux:Powerful Server Administration
- Uday R. Sawant Oliver Pelz Jonathan Hobson William Leemans
- 711字
- 2021-07-09 18:17:04
Deploying WordPress using a Docker network
In this recipe, we will learn to use a Docker network to set up a WordPress server. We will create two containers, one for MySQL and the other for WordPress. Additionally, we will set up a private network for both MySQL and WordPress.
How to do it…
Let's start by creating a separate network for WordPress and the MySQL containers:
- A new network can be created with the following command:
$ docker network create wpnet
- Check whether the network has been created successfully with
docker network ls
:$ docker network ls
- You can get details of the new network with the
docker network inspect
command:$ docker network inspect wpnet
- Next, start a new MySQL container and set it to use
wpnet
:$ docker run --name mysql -d \ -e MYSQL_ROOT_PASSWORD=password \ --net wpnet mysql
- Now, create a container for WordPress. Make sure the
WORDPRESS_DB_HOST
argument matches the name given to the MySQL container:$ docker run --name wordpress -d -p 80:80 \ --net wpnet\ -e WORDPRESS_DB_HOST=mysql\ -e WORDPRESS_DB_PASSWORD=password wordpress
- Inspect
wpnet
again. This time, it should list two containers:
Now, you can access the WordPress installation at your host domain name or IP address.
How it works…
Docker introduced the container networking model (CNM) with Docker version 1.9. CNM enables users to create small, private networks for a group of containers. Now, you can set up a new software-assisted network with a simple docker network create
command. The Docker network supports bridge and overlay drivers for networks out of the box. You can use plugins to add other network drivers. The bridge network is a default driver used by a Docker network. It provides a network similar to the default Docker network, whereas an overlay network enables multihost networking for Docker clusters.
This recipe covers the use of a bridge network for wordpress containers. We have created a simple, isolated bridge network using the docker network
command. Once the network has been created, you can set containers to use this network with the --net
flag to docker run
command. If your containers are already running, you can add a new network interface to them with the docker network connect
command, as follows:
$ # docker network connect network_name container_name $ docker network connect wpnet mysql
Similarly, you can use docker network disconnect
to disconnect or remove a container from a specific network. Additionally, this network provides an inbuilt discovery feature. With discovery enabled, we can communicate with other containers using their names. We used this feature while connecting the MySQL container to the wordpress container. For the WORDPRESS_DB_HOST
parameter, we used the container name rather than the IP address or FQDN.
If you've noticed, we have not mentioned any port mapping for the mysql
container. With this new wpnet network, we need not create any port mapping on the MySQL container. The default MySQL port is exposed by the mysql
container and the service is accessible only to containers running on the wpnet network. The only port available to the outside world is port 80
from the wordpress container. We can easily hide the WordPress service behind a load balancer and use multiple wordpress containers with just the load balancer exposed to the outside world.
There's more…
Docker also supports links to create secure communication links between two or more containers. You can set up a WordPress site using linked containers as follows:
- First, create a
mysql
container:$ docker run --name mysql -d \ -e MYSQL_ROOT_PASSWORD=password mysql
- Now, create a
wordpress
container and link it with themysql
container:$ docker run --name wordpress -d -p 80:80 --link mysql:mysql
And you are done. All arguments for
wordpress
, such asDB_HOST
andROOT_PASSWORD
, will be taken from the linkedmysql
container.
The other option to set up WordPress is to set up both WordPress and MySQL in a single container. This needs process management tools such as supervisord to run two or more processes in a single container. Docker allows only one process per container by default.
See also
You can find the respective Dockerfiles for MySQL and WordPress containers at the following addresses:
- Docker Hub WordPress: https://hub.docker.com/_/wordpress/
- Docker Hub MySQL: https://hub.docker.com/_/mysql/
- Docker networking: https://blog.docker.com/2015/11/docker-multi-host-networking-ga/
- Networking for containers using libnetwork: https://github.com/docker/libnetwork
- 嵌入式系統應用
- 三菱FX3U/5U PLC從入門到精通
- Matplotlib 3.0 Cookbook
- 數據挖掘方法及天體光譜挖掘技術
- ESP8266 Home Automation Projects
- Machine Learning with Apache Spark Quick Start Guide
- Word 2007,Excel 2007辦公應用融會貫通
- PLC與變頻技術應用
- Photoshop CS4數碼攝影處理50例
- Mastering Predictive Analytics with scikit:learn and TensorFlow
- Microsoft Dynamics CRM 2013 Marketing Automation
- 與人共融機器人的關節力矩測量技術
- ADuC系列ARM器件應用技術
- Embedded Linux Development using Yocto Projects(Second Edition)
- 網站規劃與網頁設計