How to Install Docker Compose on Ubuntu 18.04

how to install docker compose on ubuntu 18 04

Today we have a tutorial for you, on how to install Docker Compose on Ubuntu 18.04 VPS.

installing docker compose on ubuntu 18 04

Docker Compose is used to run multi-container Docker applications. This means that each container will run a standalone application which can also communicate with the other containers present in the same host. Docker Compose uses YAML files to configure all of your Docker containers and configurations. This makes Docker excellent for deploying and testing quickly and easily. Let’s get started with the installation od Docker Compose on Ubuntu 18.04.

 

1. Requirements

  • For the purposes of this tutorial, we will use an Ubuntu 18.04 VPS.
  • Full SSH root access or a user with sudo privileges is also required.

2. Connect via SSH

Connect to your server via SSH as the root user using the following command:

ssh root@IP_ADDRESS -p PORT_NUMBER

and replace “IP_ADDRESS” and “PORT_NUMBER” with your actual server IP address and SSH port number.

Before starting with the installation you will need to update your system packages to their latest version.

You can do this by running the following command:

apt-get update 
apt-get upgrade

Once the upgrade is completed we can move on, to the installation step.

2. Install Docker Compose on Ubuntu 18.04

install docker compose on ubuntu 18 04To get the latest version of Docker Compose, we will install it from the official Docker’s GitHub repository. To install Docker Compose on Ubuntu 18.04, please follow these steps:

First, we will download the Docker Compose with the following command:

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Once the download is completed, we will set the permissions for the Compose binary:

sudo chmod +x /usr/local/bin/docker-compose

We will then verify the installation by checking the Compose version:

docker-compose --version

The output should be similar to this:

docker-compose version 1.25.0, build c16347m

Docker Compose has been installed successfully. Now in the next steps, we will show you some useful Docker Compose commands.

3. Using the Docker Compose Command

In this section, we will show you how to use Docker Compose command to create a container with Docker Compose.

If you want to see the options available to a specific command execute the following command:

docker-compose docker-subcommand --help

If we execute the following command:

docker-compose up  --help

The output should be similar to this:

Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options:
-d, --detach                                  Detached mode: Run containers in the background, print new container names. Incompatible with
                                              --abort-on-container-exit.
--no-color                                    Produce monochrome output.
--quiet-pull                                  Pull without printing progress information
--no-deps                                     Don't start linked services.
--force-recreate                              Recreate containers even if their configuration
                                              and image haven't changed.
--always-recreate-deps                        Recreate dependent containers.
                                              Incompatible with --no-recreate.
--no-recreate                                 If containers already exist, don't recreate
                                              them. Incompatible with --force-recreate and -V.
--no-build                                    Don't build an image, even if it's missing.
--no-start                                    Don't start the services after creating them.
--build                                       Build images before starting containers.
--abort-on-container-exit                     Stops all containers if any container was
                                              stopped. Incompatible with -d.
-t, --timeout TIMEOUT                         Use this timeout in seconds for container
                                              shutdown when attached or when containers are
                                              already running. (default: 10)
-V, --renew-anon-volumes                      Recreate anonymous volumes instead of retrieving
                                              data from the previous containers.
--remove-orphans                              Remove containers for services not defined
                                              in the Compose file.
--exit-code-from SERVICE                      Return the exit code of the selected service
                                              container. Implies --abort-on-container-exit.
--scale                                       SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
                                              `scale` setting in the Compose file if present.

4. Running a Container with Docker Compose

Docker Hub is a public Docker registry in which Docker users can create, test, store, and distribute container images.  In this tutorial, we will use a Hello World image for testing purposes.

First, we need to create a directory for the YAML file with the following commands:

mkdir hello_world
cd hello_world

Now we need to create the YAML file:

nano docker-compose.yml

We need to put the following content into the file, then save and close it.

test:
   image: hello-world

The first line is showing the container name and the second line specifies which image to use.

If you need to look manually at images on your system you can use the following command:

docker images

If there are no local images it will show only the headings.

REPOSITORY               TAG               IMAGE                ID               CREATED SIZE

Now we will run the Hello World image by executing the following command:

docker-compose up

If there is no local image for Hello World, Docker Compose will pull it from the Docker Hub repository.

Pulling test (hello-world:)...
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Creating hello_test_1_428dc80bd6e3 ... done
Attaching to hello_test_1_cf248cd9e35e

After downloading the image, Docker Compose creates a container and runs the hello-world program.

If you want to see the container information, you can use the following command:

docker ps -a

The output should be similar to this:

CONTAINER ID IMAGE        COMMAND CREATED            STATUS                        PORTS      NAMES
451001d43d47 hello-world "/hello" About a minute ago Exited (0)About a minute ago             hello_test_1_cf248cd9e35e

If you need to remove the container for some reason you can run the following command:

docker rm 451001d43d47

Once the container has been removed, we can then remove the image:

docker rmi hello-world

In this tutorial, we learned how to install Docker Compose on Ubuntu 18.04, as well as the basics of how to use it.


Of course, you don’t have to install Docker Compose on Ubuntu 18.04 if you use one of our Managed Ubuntu Hosting services, in which case you can simply ask our expert system administrators to install Docker Compose on Ubuntu 18.04 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Docker Compose on Ubuntu 18.04, please share it with your friends on the social networks by using the share shortcuts below, or simply leave a comment in the comments section. Thanks.

Leave a Comment

To prove you are human please solve the following *