Before we begin using Docker Compose, you will need to have:
- Access to a Linux virtual machine. For the following examples, we are using Ubuntu 18.04 LTS.
- Docker-CE and Docker-Compose installed.
- Some basic knowledge of Docker commands.
- A reasonable level of comfort working with the Linux command line.
It is suggested (but not required) that you use Microsoft Visual Studio Code when working with the code examples. There are also extensions that will aid in building docker-compose.yml files and working directly with Docker.
Creating a Linux container is not difficult and is straightforward, especially if you create a Compose file containing all the services, network, and volume configurations required to run your application.
To save some time, you will want to issue the following commands, assuming you have Docker already installed:
docker pull mysql:5.7
docker pull wordpress:latest
These will take a few moments to download. These images will be used later in this guide.
Now, to get started creating a custom container, you will need to create an empty directory inside your home directory. To do so, open a terminal and type the following:
You can verify the folder created by typing ‘ls’ which will return all the contents of the current directory. Next, create an empty file in the ~/WordPress directory that we just created using the ‘touch’ command:
There is another way to create this file using the following examples, based on what editor you have installed. The Nano editor comes preinstalled with Ubuntu:
VSCode, if installed, works in the same way:
Something to keep in mind when creating the Compose file this way is that you must save the file before it will appear in the file system. When using the ‘touch’ command, the file is created and then saved from the start.
From here, you can continue to use Nano if you so choose, but you will have to be mindful of whitespace and tab usage. It is a great text editor, but it is not an Integrated Development Environment (IDE). Visual Studio Code offers linting and code highlighting with the Docker extension. Using this extension will make it easier to enter the keys that will compose a container, which will run a WordPress instance. This is why VScode will be used for the rest of this guide.
This is what a docker-compose.yml file looks like. Let’s walk through a few of these keys to explain what each is doing. The configuration keys documentation can be found here.
In short, you have to define the services and volumes that will be available inside the container. In this instance, starting at line 6, we are going to have a database (db) service, and that service will use a Docker image we currently have on our localhost called mysql:5.7, which means we will be using MySQL version 5.7 as part of our application. We then assign it a volume to store the database, and configure our database service to always restart. All that is left is to configure a few environment variables for the database. These should look familiar if you have ever had to manually install and configure a MySQL database before.
On line 18, we define our WordPress service, which will not work unless we have an existing database service; hence, we create a dependency here. Similar to our database service defined earlier, we are going to use the latest WordPress image available from Docker. Next, we have to map an external port available to our localhost to the internal port that the service will be using as part of the container networking. Again, we configure environment variables in much the same way you would configure WordPress if installing and configuring the application manually.
Finally, starting on line 29, we configure a persistent volume so we can preserve data when the container is shut down. Be sure to save the file before moving on.
Now let’s run ‘docker-compose up –d’ while inside the ‘~/WordPress’ directory. This command will build the container using the YML file. When you press enter, you should see output similar to what appears in the screenshot below:
You can now open a browser and navigate to ‘localhost:8000’. With what you have configured, you should now see a WordPress initial configuration webpage as shown below:
Congratulations! You have deployed a preconfigured Linux container hosting a WordPress web app.
For more information on using Docker Compose, visit the documentation pages. You can also watch a demonstration video of the topics covered in this guide here: https://www.youtube.com/watch?v=Zc4xOUfxSrs