Docker is a container tool that provides a virtual environment. Many believe it has changed the way we think about software.

This article, through the construction of a blog example, to introduce how to use Docker to achieve micro services.

From Docker’s perspective, software is a combination of containers: business logic container, database container, storage container and queue container…… Docker allows software to be broken down into standardized containers and assembled like building blocks.

This is the idea of Microservices: software outsources tasks to a variety of external services, with the software itself serving as a dispatching center and assembly layer for the underlying services.

Microservices are well suited to Docker containers, each hosting a service. A single computer running multiple containers at the same time makes it easy to simulate complex microservices architectures.

The previous tutorial introduced Docker concepts and basic usage, and this article continues to show you how to implement multiple services on a single computer and make them work together to form an application.

My example software of choice is WordPress. It is a popular software with tens of millions of users worldwide. At the same time, it is very simple, just two containers (business container + database container), perfect for teaching. Moreover, this “business + database” container architecture is versatile and can be reused by many applications.

To deepen your understanding, this article demonstrates how to set up a WordPress site using three methods.

  • Method A: Build your own WordPress container

  • Method B: Use the official WordPress container

  • Method C: Use the Docker Compose tool


This tutorial needs to download the image file from the warehouse, but it is very slow to access the official Docker warehouse in China and it is often disconnected, so we need to change the website of the warehouse to the domestic mirror station. The official image is recommended at registry.docker-cn.com. The following is my Debian system default repository modification method, other system modification method refer to the official documentation.

Open the /etc/default/docker file (sudo permission required) and add a line to the bottom of the file.

DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com"Copy the code

Then, restart the Docker service.

$ sudo service docker restartCopy the code

The image file will now be automatically downloaded from the image repository.


As mentioned earlier, this article demonstrates the installation of WordPress in three ways. The first is to build your own WordPress container.

2.1 Official PHP image

First, create a working directory and go to it.

$ mkdir docker-demo && cd docker-demoCopy the code

Then, execute the following command.

$ docker container run \
  --rm \
  --name wordpress \
  --volume "$PWD/":/var/www/html \
  php:5.6-apacheCopy the code

The above command creates a new container based on PHP’s image file and runs it. The PHP TAB is 5.6-apache, indicating that PHP 5.6 is installed, and the Apache server has been installed. The meanings of the three parameters in this command are as follows:

  • –rm: The container file will be automatically deleted after the container is stopped.

  • –name wordpress: The container name is wordpress.

  • –volume “$PWD/”:/var/www/html: maps the current directory ($PWD) to /var/www/html (Apache’s default directory). Therefore, any changes to the current directory are reflected in the container and accessed externally.

After running the above command, if all is well, the command line will prompt for the container’s external IP address. Please note this address, we will use it to access the container. The IP address assigned to me is 172.17.0.2.

Open your browser to 172.17.0.2 and you will see the following prompt.

Forbidden
You don't have permission to access / on this server.Copy the code

This is because the container’s /var/www/html directory (that is, the native Docker-demo directory) has nothing under it to provide accessible content.

Add the simplest PHP file, index. PHP, under the docker-demo directory on your machine.

<? php phpinfo(); ? >Copy the code

Once saved, the browser refreshes 172.17.0.2 and you should see the familiar PHPInfo page.

2.2 Copying the WordPress installation Package

The docker-demo directory can be mapped to the container. If you copy the WordPress installation package to the docker-Demo directory, you can access the installation interface of WordPress through the container.

First, in the Docker-demo directory, run the following command to grab and unpack the WordPress installation package.

$wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz $tar XVF - wordpress - 4.9.4 - zh_CN. Tar. GzCopy the code

After decompression, the WordPress installation file will be in the docker-demo/ WordPress directory.

When the browser to http://172.17.0.2/wordpress, you can see the WordPress installation tips.

2.3 Official MySQL container

WordPress must have a database to install, so you must create a MySQL container.

Open a new command line window and execute the following command.

$ docker container run \ -d \ --rm \ --name wordpressdb \ --env MYSQL_ROOT_PASSWORD=123456 \ --env MYSQL_DATABASE = \ mysql: wordpress 5.7Copy the code

The above command creates a new container based on MySQL’s image file (version 5.7). The meanings of the five command line parameters are as follows.

  • -d: After the container is started, it runs in the background.

  • –rm: After the container stops running, the container file is automatically deleted.

  • –name WordPressdb: The name of the container is WordPressdb

  • –env MYSQL_ROOT_PASSWORD=123456: Pass an environment variable MYSQL_ROOT_PASSWORD to the container process, which will be used as the root password for MySQL.

  • –env MYSQL_DATABASE=wordpress: Pass an environment variable MYSQL_DATABASE to the container process, and the container MySQL will create a database with the same name (in this case, wordpress).

After running the above command, the command line normally displays a string, the container ID, indicating that it has been created successfully.

At this point, use the following command to view the running containers, and you should see both wordpress and WordPressdb running.

$ docker container lsCopy the code

WordPressdb is run in the background and the foreground cannot see its output. You must run the following command to view the output.

$ docker container logs wordpressdbCopy the code

2.4 Customizing PHP containers

You already have WordPress containers and MySQL containers. Next, connect the WordPress container to the MySQL container. However, PHP’s official image does not come with mysql extensions, and you must create your own image file.

First, stop the WordPress container.

$ docker container stop wordpressCopy the code

When stopped, the container file is automatically deleted due to the –rm argument.

Then, in the docker-demo directory, create a new Dockerfile and write the following.

FROM PHP :5.6-apache RUN docker-php-ext-install mysqli CMD apache2-foregroundCopy the code

Install the mysqli extension on top of the original PHP image. Then, start Apache.

Based on this Dockerfile, create a new image file called phpWithmysql.

$ docker build -t phpwithmysql .Copy the code

2.5 WordPress Containers connect to MySQL

Now create a new WordPress container based on PHPWithmysql Image.

$ docker container run \
  --rm \
  --name wordpress \
  --volume "$PWD/":/var/www/html \
  --link wordpressdb:mysql \
  phpwithmysqlCopy the code

Link WordPressdb :mysql, which means that the WordPress container is connected to the WordPressdb container, and the colon indicates that the alias of the container is mysql.

You also need to change the wordpress directory permissions so that the container can write configuration information to this directory (the /var/www/html directory written inside the container will map to this directory).

$ chmod -R 777 wordpressCopy the code

Then, back to the browser’s http://172.17.0.2/wordpress page, click on “start now!” Button to start installation.

WordPress prompts for database parameters. Enter the following parameters.

  • Database name: wordpress

  • User name: root

  • Password: 123456

  • Database host: mysql

  • Table prefix: wp_ (unchanged)

Click the “Next” button, if WordPress successfully connected to the database, the following page will appear, indicating that you are ready to install.

At this point in the demo of your own WordPress containers, you can close the two containers that are running (the container files will be deleted automatically).

$ docker container stop wordpress wordpressdbCopy the code


The custom WordPress container in the previous section was a bit of a hassle. In fact, there is no need to go to so much trouble. Docker already provides an official WordPress image, so you can use that directly. With the foundation of the previous section, the following operations are easy to understand.

3.1 Basic Usage

First, create and start the MySQL container.

$ docker container run \ -d \ --rm \ --name wordpressdb \ --env MYSQL_ROOT_PASSWORD=123456 \ --env MYSQL_DATABASE = \ mysql: wordpress 5.7Copy the code

Then, create and start the WordPress container based on the official WordPress image.

$ docker container run \
  -d \
  --rm \
  --name wordpress \
  --env WORDPRESS_DB_PASSWORD=123456 \
  --link wordpressdb:mysql \
  wordpressCopy the code

The meanings of each parameter in the preceding command have been explained. The environment variable WORDPRESS_DB_PASSWORD is the root password of the MySQL container.

The command above specifies that the wordpress container is running in the background, so the foreground cannot see the output. Use the following command to find the IP address of the wordpress container.

$ docker container inspect wordpressCopy the code

After the above command is executed, it will output a lot of content, just find the IPAddress field. The IP address returned by my machine is 172.17.0.3.

When you access 172.17.0.3, you will be prompted to install WordPress.

3.2 WordPress container customization

At the previous step, the installation of the official WordPress container was successful. However, this approach has two major inconveniences.

  • Each time a new container is created, the returned IP address cannot be the same. As a result, you need to change the IP address to access WordPress.

  • WordPress is installed in a container and cannot be modified locally.

These two problems can be solved easily by adding two command-line arguments when creating a new container.

Stop the WordPress container that you just started (the container file will be deleted automatically).

$ docker container stop wordpressCopy the code

Then, use the following command to create and start the WordPress container.

$docker container run -d -p 127.0.0.2:8080:80 --rm --name wordpress --env WORDPRESS_DB_PASSWORD=123456 \ --link wordpressdb:mysql \ --volume "$PWD/wordpress":/var/www/html \ wordpressCopy the code

The above command has only two more command-line arguments than the previous one.

  • -p 127.0.0.2:8080:80: maps port 80 of the container to port 8080 of 127.0.0.2.

  • –volume “$PWD/wordpress”:/var/www/html: maps the /var/www/html directory of the container to the wordpress subdirectory of the current directory.

You will be prompted to install WordPress using your browser at 127.0.0.2:8080:80. Also, every change you make in a wordpress subdirectory is reflected in the container.

Finally, terminate both containers (the container files are deleted automatically).

$ docker container stop wordpress wordpressdbCopy the code


Method B above is simple enough, but you must start the two containers separately and provide the connection information between the containers on the command line when you start them. Therefore, Docker provides an easier way to manage the linkage of multiple containers.

4.1 Docker Compose Introduction

Compose is a Docker tool for managing multiple Docker containers into an application. You need to define a YAML configuration file docker-comemage. yml to write the call relationship between several containers. These containers can then be started/closed at the same time with a single command.

$ docker-compose up
$ docker-compose stopCopy the code

4.2 Docker Compose installation

Docker Compose will be installed for Both Mac and Windows docker servers. For details about installation in Linux, see the official documentation.

When the installation is complete, run the following command.

$ docker-compose --versionCopy the code

4.3 WordPress sample

In the docker-demo directory, create a docker-comemess. yml file and write the following.

Mysql: image: mysql:5.7 environment: -mysql_root_password =123456 -mysql_database =wordpress web: image: WordPress links: -mysql environment: -wordpress_db_password =123456 ports: - "127.0.0.3:8080:80" working_dir: /var/www/html volumes: - wordpress:/var/www/htmlCopy the code

In the code above, the two top tags indicate that there are two containers mysql and Web. The specific Settings of each container have been explained previously and are fairly easy to understand.

Start both containers.

$ docker-compose upCopy the code

Visit http://127.0.0.3:8080 and you should see the WordPress installation screen.

Now close both containers.

$ docker-compose stopCopy the code

When closed, the two container files still exist and the data written in them will not be lost. You can reuse it the next time you start up. The following command can remove the two container files (the container must have stopped running).

$ docker-compose rmCopy the code

Reprint statement: this article is reproduced, the original address: http://www.ruanyifeng.com/blog/2018/02/docker-wordpress-tutorial.html

– MORE | – MORE excellent articles

  • When programming languages become girls

  • Transactions in Java — JDBC transactions and JTA transactions

  • Why do I recommend using enumerations to implement singletons?

  • The most complete Docker introduction and tutorial, a complete master.

If you saw this, you enjoyed this article.

So please long press the QR code to follow Hollis

Forwarding moments is the biggest support for me.