How to Install and Use Docker Compose on Ubuntu 16.04 /18.04

This post will guide you how to install and use Docker compose on Ubuntu Linux 16.04 or 18.04. How do I install the latest version of Docker Compose tool on your Ubuntu system. How to Launch multiple containers with Docker Compose on Ubuntu system. How to uninstall Docker Compose from your Ubuntu system.

What is Docker Compose?


Docker compose is a tool for defining and running multiple container Docker applications in your Ubuntu system. And you can define a YAML file to configure your application’s service, netowrks and volumes, so that you can use a docker compse command to create and start all services from your configuration. So you can use Docker Compose to orchestrate the processes of Docker containers, including starting up, or shutting down Docker Containers.

docker-compose installation ubuntu1

Installing Docker Compose with Default APT


The Docker Compose package is already available in the default official Ubuntu repositories. So you can directly to install it with apt command on your Ubuntu 16.04 or 18.04 system. You need to know that the installed version of Docker Compose may be not the latest version. Type the following command to install Docker Compose:

$ sudo apt install docker-compose

The outputs should be similar to the below:

devops@devops-osetc:~$ sudo apt install docker-compose
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
golang-docker-credential-helpers python-backports.ssl-match-hostname python-cached-property
python-certifi python-chardet python-docker python-dockerpty python-dockerpycreds
python-docopt python-funcsigs python-functools32 python-jsonschema python-mock
python-openssl python-pbr python-requests python-texttable python-urllib3 python-websocket
Suggested packages:
python-funcsigs-doc python-mock-doc python-openssl-doc python-openssl-dbg python-socks
python-ntlm
Recommended packages:
docker.io
The following NEW packages will be installed:
docker-compose golang-docker-credential-helpers python-backports.ssl-match-hostname
python-cached-property python-certifi python-chardet python-docker python-dockerpty
python-dockerpycreds python-docopt python-funcsigs python-functools32 python-jsonschema
python-mock python-openssl python-pbr python-requests python-texttable python-urllib3
python-websocket
0 upgraded, 20 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,249 kB of archives.
After this operation, 5,422 kB of additional disk space will be used.
Do you want to continue? [Y/n]
......
Get:19 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 docker-compose all 1.17.1-2 [76.3 kB]
Get:20 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-openssl all 17.5.0-1ubuntu1 [41.3 kB]
Fetched 1,249 kB in 25s (50.8 kB/s)
......
Setting up python-docopt (0.6.2-1build1) ...
Setting up python-cached-property (1.3.1-1) ...
Setting up python-dockerpycreds (0.2.1-1) ...
Setting up python-docker (2.5.1-1) ...
Setting up python-mock (2.0.0-3) ...
Setting up python-jsonschema (2.6.0-2) ...
update-alternatives: using /usr/bin/python2-jsonschema to provide /usr/bin/jsonschema (jsonschema) in auto mode
Setting up docker-compose (1.17.1-2) ...

Then you can try to test if it is installed successfully on your Ubuntu system, just check it’s version, type:

$ docker-compose -v

Outputs:

devops@devops-osetc:~$ docker-compose -v
docker-compose version 1.17.1, build unknown

From the above outputs, you can see that the installed version of Docker Compose from the official Ubuntu repositories is 1.17.1.

Installing Docker Compose from Docker’s GitHub Repository


You can install the Docker Compose from the official Ubuntu repositories, but it is not the latest version. So you can install the latest version of Docker Compose from the Docker’s GitHub repository. The current version of Docker Compose in GitHub is 1.24.0. To install the latest version of Docker Compose on Ubuntu 16.04 or 18.04, just executing the following curl command:

$ curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Outputs:

root@devops-osetc:/home/devops# sudo curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 453 0 --:--:-- 0:00:01 --:--:-- 453
100 15.4M 100 15.4M 0 0 319k 0 0:00:49 0:00:49 --:--:-- 346k

Then you can verify if the installation is completed successfully by checking the version, type:

$ docker-compose --version

Outputs:

devops@devops-osetc:~$ docker-compose --version
docker-compose version 1.24.0, build 0aa59064

You can see that the current installed version of Docker Compose is 1.24.0.

Launching Multiple Containers with Docker Compose


After installed Docker Compose on your Ubuntu system, you can start to setup multiple containers with Docker Compose. Assuming that you want to create two services, db and myUbuntu. And each service runs one image in a Docker Container. You can do the following steps to launch a multi-container application:

#1 create your own project directory, and change the current directory to it.

$ mkdir mytest
$ cd mytest

#2 create a YAML file called docker-compose.yml under your project directory with vi/vim test editor.

$ vim docker-compose.yml

Or

$ nano docker-compose.yml

Then put the following contents into the file, save and close the file.

version: "3"
services:

    db:
       image: mysql:5.7
       container_name: 'mysql'
       restart: 'always'
    volumes:
       - db-data:/var/lib/mysql
    environment:
       MYSQL_ROOT_PASSWORD: 123456
       MYSQL_DATABASE: mydata
       MYSQL_USER: test
       MYSQL_PASSWORD: test

     myUbuntu:
       image: ubuntu

volumes: 
    db_data:

#3 run the following command to start up those two Docker Containers, type:

$ docker-compose up

Outputs:

devops@devops-osetc:~/mytest$ docker-compose up
Creating network "mytest_default" with the default driver
Creating volume "mytest_db-data" with default driver
Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
27833a3ba0a5: Pull complete
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
8f856c14f5af: Pull complete
9c4f38c23b6f: Pull complete
1b810e1751b3: Pull complete
5479aaef3d30: Pull complete
1d924ec3d520: Pull complete
1ab7ae63ac60: Pull complete
08aa5f3680e9: Pull complete
a832d0a0972a: Pull complete
Digest: sha256:dba5fed182e64064b688ccd22b2f9cad4ee88608c82f8cff21e17bab8da72b81
Status: Downloaded newer image for mysql:5.7
Creating mysql ... done
Creating mytest_myUbuntu_1 ... done
Attaching to mytest_myUbuntu_1, mysql

Note: the docker-compose command will look for image files by the name you specified, mysql and myUbuntu from your local disk. If it do not find image from local, Docker Compose will pull image from the Docker Hub publick repository. Then it will create two containers and run the corresponding application.

Now you can try to check the local image file with the docker image command, type:

$ docker image

Outputs:

devops@devops-osetc:~/mytest$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 98455b9624a9 13 days ago 372MB
ubuntu latest 47b19964fb50 2 months ago 88.1MB

#4 you need to check the container information by docker ps command with -a option to show all existing containers, type:

$ docker ps -a

#5 start all detached mode of containers by docker-compose up command with -d option, type:

$ docker-compose up -d

Outputs:

devops@devops-osetc:~/mytest$ docker-compose up -d
Starting mytest_myUbuntu_1 ... done
Starting mysql ... done

If you want to stop all containers or services, you can use the following command:

$ docker-compose stop

Uninstalling Docker Compose


If you want to uninstall Docker Compose from your ubuntu system, just delete the docker-compose binary file with the following command:

$ sudo rm /usr/local/bin/docker-compose
$ sudo rm /usr/bin/docker-compose

If you install the docker compose with the default ubuntu repository, you just execute the following command to uninstall it, type:

$ sudo apt remove docker-compose

Conclusion


You should know that how to use and install Docker Compose tool on Ubuntu Linux 16.04 or 18.04. And you also should know that how to create multiple containers with Docker-compose tool on Ubuntu system. If you want to see more information about Docker Compose, you can go to the official web page of Docker Compose directly.

See Also:

 

You might also like:

Sidebar



back to top