Running the Docker run Command to Run Containers

Preparing Images

WordPress and MySQL images are general-purpose images and can be obtained from the container registry.

You can run the docker pull command on the device where the container engine is installed to download images.

docker pull mysql:5.7
docker pull wordpress

After the images are downloaded, run the docker images command to view the images. Two images exist on the local host, as shown in the following figure.

Running Containers

You can use the container engine to directly run the WordPress and MySQL containers, and use the --link parameter to connect the two containers. In this way, the WordPress container can access the MySQL container without code changes.

Run the following command to run the MySQL container:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=******** -d mysql:5.7

The parameters are described as follows:

  • --name: specifies the container name as some-mysql.
  • -e: specifies the environment variable of the container. In this example, the value of MYSQL_ROOT_PASSWORD is set to ******** (replace ******** with an actual password).
  • -d: indicates that the container runs in the backend.

Run the following command to run the WordPress container:

docker run --name some-wordpress --link some-mysql:mysql -p 8080:80 -e WORDPRESS_DB_PASSWORD=******** -d wordpress

The following provides the parameter description.

  • --name: specifies the container name as some-wordpress.
  • --link: connects the some-wordpress container to the some-mysql container and changes the name of the some-mysql container to mysql. --link is an easy method for connecting two containers. Alternatively, you can configure the environment variable WORDPRESS_DB_HOST of the some-wordpress container to access the IP address and port of the mysql container.
  • -p: specifies ports for mapping. In this example, port 80 of the container is mapped to port 8080 of the host.
  • -e: specifies the environment variable of the container. In this example, the value of WORDPRESS_DB_PASSWORD is set to ******** (replace ******** with an actual password). The value of WORDPRESS_DB_PASSWORD must be the same as that of MYSQL_ROOT_PASSWORD. This is because the WordPress requires a password to access the MySQL database.
  • -d: indicates that the container runs in the backend.

After the WordPress runs, you can access WordPress blogs through http://127.0.0.1:8080, as shown in the following figure.