How to Install and Use Docker Compose in Ubuntu
To Install and Use Docker Compose in Ubuntu
Docker Compose is a tool used to run the complex applications with Docker. It allows the users to manage the multi-container application with all of its dependencies in a single file. In this article you can learn about the installation and configuration of Docker compose in Ubuntu.
Installation of Docker Compose
First download and install the required script by using the following command.
root@linuxhelp1:~# wget -qO- https://get.docker.com/ | sh
apparmor is enabled in the kernel and apparmor utils were already installed
+ sh -c apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Executing: /tmp/tmp.sn4mlAPnxe/gpg.1.sh --keyserver
hkp://ha.pool.sks-keyservers.net:80
--recv-keys
58118E89F3A912897C070ADBF76221572C52609D
gpg: requesting key 2C52609D from hkp server ha.pool.sks-keyservers.net
gpg: key 2C52609D: public key " Docker Release Tool (releasedocker) < docker@docker.com> " imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
+ break
+ sh -c apt-key adv -k 58118E89F3A912897C070ADBF76221572C52609D > /dev/null
+ sh -c mkdir -p /etc/apt/sources.list.d
+ dpkg --print-architecture
+ sh -c echo deb [arch=amd64] https://apt.dockerproject.org/repo ubuntu-xenial main > /etc/apt/sources.list.d/docker.list
+ sh -c sleep 3 apt-get update apt-get install -y -q docker-engine
Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease
Get:4 https://apt.dockerproject.org/repo ubuntu-xenial InRelease [30.2 kB]
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:6 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages [2,553 B]
Fetched 32.8 kB in 1s (19.5 kB/s)
Reading package lists... Done
Reading package lists...
Building dependency tree...
Reading state information...
.
.
.
Client:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 22:01:48 2016
OS/Arch: linux/amd64
Server:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 22:01:48 2016
OS/Arch: linux/amd64
If you would like to use Docker as a non-root user, you should now consider
adding your user to the " docker" group with something like:
sudo usermod -aG docker your-user
Remember that you will have to log out and back in for this to take effect!
Then add your user to the docker group.
root@linuxhelp1:~# sudo usermod -aG docker $(whoami)
Here you need to install python-pip as prerequisite.
root@linuxhelp1:~# apt-get -y install python-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libexpat1 libexpat1-dev libpython-all-dev libpython-dev libpython2.7 libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib
python-all python-all-dev python-dev python-pip-whl python-pkg-resources python-setuptools python-wheel python2.7 python2.7-dev
python2.7-minimal
Suggested packages:
python-setuptools-doc python2.7-doc binfmt-support
The following NEW packages will be installed:
libexpat1-dev libpython-all-dev libpython-dev libpython2.7-dev python-all python-all-dev python-dev python-pip python-pip-whl
python-pkg-resources python-setuptools python-wheel python2.7-dev
The following packages will be upgraded:
libexpat1 libpython2.7 libpython2.7-minimal libpython2.7-stdlib python2.7 python2.7-minimal
6 upgraded, 13 newly installed, 0 to remove and 410 not upgraded.
Need to get 34.6 MB/34.7 MB of archives.
After this operation, 45.4 MB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 python2.7 amd64 2.7.12-1~16.04 [224 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython2.7 amd64 2.7.12-1~16.04 [1,070 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython2.7-stdlib amd64 2.7.12-1~16.04 [1,877 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 python2.7-minimal amd64 2.7.12-1~16.04 [1,294 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython2.7-minimal amd64 2.7.12-1~16.04 [339 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libexpat1-dev amd64 2.1.0-7ubuntu0.16.04.2 [115 kB]
.
.
.
Setting up python-pip-whl (8.1.1-2ubuntu0.2) ...
Setting up python-pip (8.1.1-2ubuntu0.2) ...
Setting up python-pkg-resources (20.7.0-1) ...
Setting up python-setuptools (20.7.0-1) ...
Setting up python-wheel (0.29.0-1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Once the python-pip get installed, run the following command to install the Docker Compose.
root@linuxhelp1:~# pip install docker-compose
The directory ' /home/user1/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo' s -H flag.
The directory ' /home/user1/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo' s -H flag.
Collecting docker-compose
Downloading docker-compose-1.8.1.tar.gz (151kB)
100% |????????????????????????????????| 153kB 528kB/s
Collecting cached-property< 2,> =1.2.0 (from docker-compose)
Downloading cached_property-1.3.0-py2.py3-none-any.whl
Collecting docopt< 0.7,> =0.6.1 (from docker-compose)
Downloading docopt-0.6.2.tar.gz
Collecting PyYAML< 4,> =3.10 (from docker-compose)
Downloading PyYAML-3.12.tar.gz (253kB)
100% |????????????????????????????????| 256kB 188kB/s
Collecting requests< 2.8,> =2.6.1 (from docker-compose)
Downloading requests-2.7.0-py2.py3-none-any.whl (470kB)
100% |????????????????????????????????| 471kB 219kB/s
.
.
.
Running setup.py install for backports.ssl-match-hostname ... done
Running setup.py install for dockerpty ... done
Running setup.py install for functools32 ... done
Running setup.py install for docker-compose ... done
Successfully installed PyYAML-3.12 backports.ssl-match-hostname-3.5.0.1 cached-property-1.3.0 docker-compose-1.8.1 docker-py-1.10.6 docker-pycreds-0.2.1 dockerpty-0.4.1 docopt-0.6.2 enum34-1.1.6 functools32-3.2.3.post2 ipaddress-1.0.17 jsonschema-2.5.1 requests-2.7.0 six-1.10.0 texttable-0.8.6 websocket-client-0.37.0
To run a container with Docker Compose
Follow the below steps to run a container with docker compose. Create a directory for YAML file.
root@linuxhelp1:~# mkdir hello-world
The public Docker registry, Docker Hub, includes a simple Hello World image.
Then change into the directory.
root@linuxhelp1:~# cd hello-world/
Create the YAML file using any text editor.
root@linuxhelp1:~/hello-world# nano docker-compose.yml
Enter the following contents into the file. Then save and exit the text editor.
my-test: image: hello-world
First line will be used as part of the container name. The second line specifies the image used to create the container. The image will be downloaded from the official Docker Hub repository.
Next execute the following command to create the container.
root@linuxhelp1:~/hello-world# docker-compose up
Pulling my-test (hello-world:latest)...
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
Creating helloworld_my-test_1
Attaching to helloworld_my-test_1
my-test_1 |
my-test_1 | Hello from Docker!
my-test_1 | This message shows that your installation appears to be working correctly.
my-test_1 |
my-test_1 | To generate this message, Docker took the following steps:
my-test_1 | 1. The Docker client contacted the Docker daemon.
.
.
.
my-test_1 | Share images, automate workflows, and more with a free Docker Hub account:
my-test_1 | https://hub.docker.com
my-test_1 |
my-test_1 | For more examples and ideas, visit:
my-test_1 | https://docs.docker.com/engine/userguide/
my-test_1 |
helloworld_my-test_1 exited with code 0
Now start the hello world as shown below,
root@linuxhelp1:~/hello-world# docker-compose up -d
Starting helloworld_my-test_1
Utilize the following command to show the group of Docker containers.
root@linuxhelp1:~/hello-world# docker-compose ps
Name Command State Ports
-----------------------------------------------
helloworld_my-test_1 /hello Exit 0
If you want to stop all running Docker containers for an application group, use the following command in the same directory.
root@linuxhelp1:~/hello-world# docker-compose stop
The below mentioned command allows you to delete all the containers that make up your container group.
root@linuxhelp1:~# docker-compose rm
ERROR:
Can' t find a suitable configuration file in this directory or any
parent. Are you in the right directory?
Supported filenames: docker-compose.yml, docker-compose.yaml
To avoid error, run the above command in the directory that contains a Docker container.
Move to the respective directory to remove the containers.
root@linuxhelp1:~/hello-world# docker-compose rm
Going to remove helloworld_my-test_1
Are you sure? [yN] y
Removing helloworld_my-test_1 ... do
To access the Docker container file system
For accessing the docker container file system, create a new directory for it and move into it.
root@linuxhelp1:~# mkdir ~/nginx & & cd $_
Then create a " docker-compose.yml" file in newly created directory.
root@linuxhelp1:~/nginx# nano docker-compose.yml
Enter the following lines in the editor as shown below.
nginx: image: nginx
Save the file and exit from the editor.
To start Nginx container
Run the following command to start the Nginx container as a background process.
root@linuxhelp1:~/nginx# docker-compose up -d
Pulling nginx (nginx:latest)...
latest: Pulling from library/nginx
43c265008fae: Pull complete
e4c030a565b1: Pull complete
685b7631c1ce: Pull complete
Digest: sha256:dedbce721065b2bcfae35d2b0690857bb6c3b4b7dd48bfe7fc7b53693731beff
Status: Downloaded newer image for nginx:latest
Creating nginx_nginx_1
The Nginx image will be downloaded and the container will be started in the background.
To obtain the CONTAINER ID for the container, list all the containers with the following command.
root@linuxhelp1:~/nginx# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06db33419f46 nginx " nginx -g ' daemon off" 7 minutes ago Up 7 minutes 80/tcp, 443/tcp nginx_nginx_1
Execute the " docker exec" command to start a shell inside the container.
root@linuxhelp1:~/nginx# docker exec -it 06db33419f46 /bin/bash
Comments ( 1 )