Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Let us demonstrate the power of compose by setting a WordPress blog website in a matter of minutes.
Example - A WordPress blog
Start by creating a new project directory for your WordPress application.
Now we need to create a docker-compose.yml
file inside our project directory to define the different services that are required for our setup. A service exist of a container with its specific settings to run. Basically we define what images are required for our multi-container application. To run a Wordpress blog, we need both a mysql database server and a Wordpress image. Using the depends_on
key it is possible to indicate that the Wordpress container requires a database container, this will result in first starting the database container, and then starting the Wordpress container to prevent problems.
Environment variables need to be used to configure both wordpress and mysql accounts.
Do note that the above username and passwords are used for demonstrational purposes only. A real production site should be authenticated using secure passwords.
Build the project
Build the project by running docker-compose up -d
inside your project directory.
This pulls the required Docker images, and starts the specified containers, as shown in the example below. You can verify that the containers are up an running by executing the docker ps
command.
WordPress configuration
The WordPress site is not immediately available on port 8000
because the containers are still being initialized and may take a couple of minutes before the first load.
Now we should be good to go.
You can stop the multi-container project by issuing the command docker-compose down
. This command removes the containers and default network, but preserves your WordPress database.
Last updated