How to use 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:

Verify the installation with the command:

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

Figure A

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:

Change into that new directory with the command:

Create the new Docker Compose file with the command:

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

Save and close that file.

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

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:

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

Figure C

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:

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

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

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

Figure D

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:

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

Figure E

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

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