We already have a blog for the docker compose installation procedures which you can refer from the here. In this particular blog, we would be discussing the use of docker compose and would also be looking into its demonstration by running a simple nginx container.
For this demo, we would be using an AWS ec2 instance, though you can use it on your local machine as well.
Note: Please install Docker and docker compose before you start.
Let’s get started,
Clone a sample nginx repository from the devopsage Github public repository.
ubuntu@devopsage:~$ git clone https://github.com/devopsage/docker-compose-nginx.git ubuntu@devopsage ls docker-compose-nginx ubuntu@devopsage:~$ cd docker-compose-nginx/ ubuntu@devopsage:~/docker-compose-nginx$ ls -lrth total 28K -rw-rw-r-- 1 ubuntu ubuntu 102 Jun 1 16:47 web2.Dockerfile drwxrwxr-x 2 ubuntu ubuntu 4.0K Jun 1 16:47 web2 -rw-rw-r-- 1 ubuntu ubuntu 95 Jun 1 16:47 web1.Dockerfile -rw-rw-r-- 1 ubuntu ubuntu 30 Jun 1 16:47 index.html -rw-rw-r-- 1 ubuntu ubuntu 137 Jun 1 16:47 docker-compose.yml -rw-rw-r-- 1 ubuntu ubuntu 286 Jun 1 16:47 default.conf -rw-rw-r-- 1 ubuntu ubuntu 78 Jun 1 16:47 README.md ubuntu@devopsage:~/docker-compose-nginx$
So here, We will be using 2 different containers for nginx, hosting 2 different sample pages.
Build the web1.Dockerfile
ubuntu@devopsage:~/docker-compose-nginx$ docker build -t nginx:1.0 -f web1.Dockerfile .
The output of the above command:
Similarly, build the web2.Dockerfile using the below command.
ubuntu@devopsage:~/docker-compose-nginx$ docker build -t nginx:1.2 -f web2.Dockerfile .
Output will be as below,
Now, We will have 2 docker images, nginx:1.0 (web1) and nginx:1.2 (web2) which we will be using in the docker compose and will be exposing the port to 80 and 90 respectively.
Just have a look at the docker compose file.
version: '3' services: web1: image: nginx:1.0 ports: - 80:80 web2: image: nginx:1.2 ports: - 90:80
Here we are using 2 services web1 and web2, each of the services has its image and the port to which we need to expose. Now, let’s run the docker compose file.
ubuntu@devopsage:~/docker-compose-nginx$ docker-compose up -d
Output:
Check for the running container by using # docker ps
Now, Try to access the running application,
http://Public_IP –> For web1
http://Public_IP:90 –> For web2
Use Local address or localhost when using locally.
Now, remove the environment using the below docker compose command. This will remove both the containers, networks, etc. In general, the things created by docker compose will be completely removed.
ubuntu@devopsage:~/docker-compose-nginx$ docker-compose down
Optional: If you are running above command not from the present working directory then you can define -f option for the docker compose file.
ubuntu@devopsage:~$ docker-compose -f docker-compose-nginx/docker-compose.yml up -d
Output:
You will no longer be able to see running containers.
Important Docker Compose Commands.
To Run the compose file from the current directory with the container output.
# docker-compose up
To run the Compose file from the current directory as a daemon to run in the background (Detached Mode). ( -d option)
# docker-compose up -d
To Run specifying the docker compose file full path. ( -f option )
# docker-compose -f docker-compose-nginx/docker-compose.yml up -d
To Specify an alternate project name use -p option, by default it uses directory name.
# docker-compose -f docker-compose-nginx/docker-compose.yml -p my_webapp up -d
To Remove the container for the Service not defined in the docker compose file. for example, if you have run the docker compose file and later updated the docker file with few more services, then in order to bring the compose down we have to use –remove-orphans option.
# docker-compose -f docker-compose-nginx/docker-compose.yml -p my_webapp down --remove-orphans
If a single service from the compose file need to be stopped. (option: stop)
# docker-compose -f docker-compose-nginx/docker-compose.yml -p my_webapp stop web1
To remove the service stopped from the docker compose in the above command. (option: rm)
# docker-compose -f docker-compose-nginx/docker-compose.yml -p my_webapp rm web1
To Create one specific service from the docker compose. (option: create)
# docker-compose -f docker-compose-nginx/docker-compose.yml -p my_webapp create web1
To Start the Created Service (option: start)
# docker-compose -f docker-compose-nginx/docker-compose.yml -p my_webapp start web1
To Restart specific service from the compose file. (option: restart). It is effective when you make any changes to the compose file and wanted the changes to be reflected. You can restart that particular service.
# docker-compose -f docker-compose-nginx/docker-compose.yml -p my_webapp restart web1
Now We will see how we can build the Dockerfile from the docker compose file itself and use the build image to run the container. We have to make Some modification to the compose file and use –build option in the command. Let’s see the below modified docker compose file. Change to our docker-compose-nginx directory
ubuntu@devopsage:~$ cd docker-compose-nginx ubuntu@devopsage:~/docker-compose-nginx$ cat docker-compose.yml version: '3' services: web1: build: context: . dockerfile: web1.Dockerfile ports: - 80:80 web2: build: context: . dockerfile: web2.Dockerfile ports: - 90:80
Now, run the below command to build and run the container at the same time.
ubuntu@devopsage:~/docker-compose-nginx$ docker-compose up -d --build
Output of the command:
Login to the created container from the docker compose command.
ubuntu@devopsage:~/docker-compose-nginx$ docker-compose exec web1 sh / #
You are logged in to the container. use exit or ctrl+d to come out of the container.
To list the running container using docker compose
ubuntu@devopsage:~/docker-compose-nginx$ docker-compose ps Name Command State Ports docker-compose-nginx_web1_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp docker-compose-nginx_web2_1 nginx -g daemon off; Up 0.0.0.0:90->80/tcp
To pause the service (running container)
ubuntu@devopsage:~/docker-compose-nginx$ docker-compose pause web1 ubuntu@devopsage:~/docker-compose-nginx$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4a0aba5363d docker-compose-nginx_web2 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:90->80/tcp docker-compose-nginx_web2_1 f2d147552c4f docker-compose-nginx_web1 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes (Paused) 0.0.0.0:80->80/tcp docker-compose-nginx_web1_1
To Unpause
# docker-compose unpause web1
To Kill Conatiner.
# docker-compose kill web1
Display the running process
# docker-compose top
To See Compose version
# docker-compose version
That’s It for this blog. I have tried to cover the use cases and all the important command in use. For more details you can check the official docker page and also check docker compose help section from the terminal.
# docker-compose help
You May Also need to look At the below posts,
How to Install Docker on Redhat/CentOS 7
How to Install Docker on Ubuntu 16.04
Install Docker Compose on Linux – Ubuntu/CentOS.
If you Like Our Content here at Devopsage, then please support us by sharing this post.
Please Like and follow us at, LinkedIn, Facebook, Twitter, and GitHub
Also, Please comment on the post with your views and let us know if any changes need to be done.
Thanks!!