Running a WordPress container

Almost everyone at some point will have installed, used, or read about WordPress, so for our next example, we will be using the official WordPress container from the Docker Hub. You can find details on the container at https://hub.docker.com/_/wordpress/.

Note

WordPress is web software that you can use to create a beautiful website, blog, or app. We like to say that WordPress is both free and priceless at the same time. For more information, check out https://wordpress.org/.

To launch WordPress, you will need to download and run two containers, the first of which is the database container, for this I recommend using the official MySQL container which you can find at https://hub.docker.com/_/mysql/.

To download the latest MySQL container image run the following command on your Mac, Windows or Linux machine:

docker image pull mysql

Now that you have the pulled a copy of the image you can launch MySQL by running the following command:

docker container run -d 
    --name mysql 
    -e MYSQL_ROOT_PASSWORD=wordpress 
    -e MYSQL_DATABASE=wordpress 
    mysql

The command above launches (docker container run) the MySQL in a detached state (using -d), meaning that it is running in the background, we are calling the container mysql (--namewordpress) and we are using two different environment variables (using -e) to set the MySQL root password to wordpress (-e MYSQL_ROOT_PASSWORD=wordpress) and to create a database called wordpress (-e MYSQL_DATABASE=wordpress).

Once launched, you should receive the container ID. You can check the container is running as expected by using the following command:

docker container ps

Now, at this point, although the container is running that doesn't really mean that MySQL is ready. If you were to launch your WordPress container now, you might find that it runs for a short while and then stops.

Don't worry, this is expected. As there is no MySQL data within the container it takes a little while to get itself into a state where it is available to accept incoming connections.

To check the status of your MySQL container you can run the following command:

docker container logs mysql
Running a WordPress container

Once you see the message mysqld: ready for connections, you are good to launch your WordPress container; you may find yourself having to check the logs a few times.

Next up, we to down the WordPress container image; to do this, run the following command:

docker image pull wordpress

Once downloaded run the following command to launch the WordPress container:

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

Again, we are launching the container in the background (using -d), calling the container the wordpress (with --name wordpress). This is where things differ slightly between the MySQL and WordPress containers, we need to let the WordPress container know where our MySQL container is accessible, to do this are using the link flag (in our case by running --link mysql:mysql) this will create an alias within the WordPress container pointing it at the IP address of the MySQL container.

Next up we are opening port 8080 on our machine and mapping it to port 80 on the container (using -p 8080:80) and then letting WordPress know what the password is (with -e WORDPRESS_DB_PASSWORD=wordpress).

Check the running containers using the following command:

docker container ps

Should show you that you now have two running containers, MySQL and WordPress:

Running a WordPress container

Unlike the MySQL container, there isn't much the WordPress container needs to do before it is accessible, you can check the logs by running the following command:

docker container logs wordpress
Running a WordPress container

If you open your browser and go to http://localhost:8080/ you should see your WordPress installation sitting at an installation prompt like the following:

Running a WordPress container

If you like, you can work through the installation and get WordPress up and running by clicking on Continue and following the onscreen prompts; however, the next set of commands we will be running will destroy our two containers.

To remove everything we have just launched ahead of the next exercise, run the following commands:

docker container stop wordpressmysql
docker container rmwordpressmysql
docker image rm wordpress mysql

So far we have used the Docker client to easily launch, stop, start, pause, unpause and remove containers as well as downloading and removing container images from the Docker Hub, while this is great to quickly launch a few containers it can get complicated to manage once you have more than a few containers running at once, this is where the next tool we are going to look at comes in.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset