How to use the docker-compose command


Find out how to get started working with the docker-compose command.

How to use the docker-compose command
Find out how to get started working with the docker-compose command.

Managing and deploying Docker containers can be done in many ways. You can use the straight up docker command, you can work via one of the many GUI tools (both web-based and desktop client oriented), or you opt to go the docker-compose route.

What is Docker Compose? Simple: Docker Compose is used for the creation of containers and the links between them. However, the docker-compose command is actually quite a bit more useful than that. With this command you can: Build images, scale containers, heal containers, view output from containers, list the public port of a container, and much more. 

But how do you use docker-compose? Let’s find out how.

SEE: Serverless computing: A guide for IT leaders (TechRepublic Premium)

How to install Docker Compose

Even if you’ve installed Docker on your server, chances are Docker Compose wasn’t installed along with it. In order to install Docker Compose, issue the following commands:

sudo curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Verify the installation with the command:

docker-compose version

You should see the version numbers for a few applications (Figure A).

Figure A


Docker Compose installed and ready.

The Docker file

In order to deploy a container, Docker Compose depends on a docker-compose.yml file, which is a file used to deploy a Docker container to your specifics. Let’s say you want to deploy a WordPress container. First create a new directory with the command:

mkdir ~/wordpressbuild

Change into that new directory with the command:

cd ~/wordpressbuild

Create the new Docker Compose file with the command:

nano docker-compose.yml

Paste the following contents (taken from the official Docker Compose documentation) into the file:

version: '3.3'

     image: mysql:5.7
       - db_data:/var/lib/mysql
     restart: always
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

       - db
     image: wordpress:latest
       - "8000:80"
     restart: always
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
    db_data: {}

Save and close that file.

Now we build the project and deploy the container in detached mode with the command:

docker-compose up -d

The command will download all the necessary images (in this case MySQL and WordPress) and then deploy the service on port 8000. You can point a web browser to http://SERVER_IP:8000 (where SERVER_IP is the IP address of the hosting server) to see the WordPress installation page (Figure B).

Figure B


How to check in on your deployment

Let’s say you want to check the logs from your deployment. To do this, issue the command:

docker-compose logs

You should see plenty of information from your last deployment (Figure C).

Figure C


Viewing the logs from the docker-compose deployment of WordPress.

That command will spit out a lot of information (especially if you’ve deployed numerous containers). Instead, you can specify a service for which to view log files. How to know which service name to use? Check the docker-compose.yml file. In our example, we have two services:

  • db – the database
  • wordpress – the WordPress container

So if you want to view only the logs for the wordpress service, that command would be:

docker-compose logs wordpress

You could also follow the output of the logs (in the same manner you use the tail command) like so:

docker-compose logs -f wordpress

Any time new information is logged for the wordpress service, it will appear in your terminal window (Figure D).

Figure D


Following the wordpress service logs.

What if you forgot which ports you used in your deployment? You could check the docker-compose.yml file, or you could use the port option with the docker-compose command. You do have to know the internal command of a service. For example, WordPress defaults to port 80, so we know that’s the internal port. But what did we assign as the network-facing port? Find out with the command:

docker-compose port wordpress 80

The output of this command will show we mapped internal port 80 to external port 8000 (Figure E).

Figure E


Our port mapping for WordPress.

If you can’t remember what containers you’ve deployed, you can issue the command:

docker-compose ps

The output will list out every container you’ve deployed (Figure F).

Figure F


Just getting started

And that should get you started with the power to be found in Docker Compose. When next we visit this topic, we’ll break down the docker-compose.yml file and find out how to build your own.

Also see


Image: Docker

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button