Docker Compose is a tool for defining and running multi-container Docker applications. Docker Compose uses YAML files to configure your application’s services. Then create and start all services from the configuration with a single command. Compose works in all environments whether it be Production, staging, development, testing, or CI workflows. In this guide we will explain how to install Docker Compose on Ubuntu 20.04. It also contains commands that can manage the entire application lifecycle.
- Starting, stopping, and recreating services
- View the status of running services
- Streaming log output from running services
- Run a one-time command against a service
- 99RDP VPS/Dedicated Server(Running Ubuntu 20.04)
- Pre Installed Docker.
- Access to Terminal
Steps To Install Docker Compose On Ubuntu 20.04
In case the system is only installed with Docker then we have to do a standalone installation of Docker Compose. To ensure you have the latest stable version of Docker Compose, download the software from the official Github repository. First, check the latest version available on the releases page. At the time of writing this article, the latest stable version is 2.16.0.
# curl -L “https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# docker compose version
Steps To Create A docker-compose.yaml file
In this guide we are creating an example of web server enviroment using Nginx from the official Docker Hub. In this example we will server a static HTML page with official Nginx Alpine container. To start the process lets create a directory name compose-test in /home folder. In the directory create a docker-compose.yaml file using nano or any editor of your choice. In the docker-compose.yaml enter the given details for official Nginx image.
# mkdir ~/home/compose-test
# cd /home/compose-test
# nano docker-compose.yaml
A docker-compose.yaml file usually starts with a version definition. This tells Docker Compose which config version you’re using.Then there is the service block that sets up the services that are part of that environment. In your case you have a single service called Web. This service uses nginx.Create an Alpine image and set up port redirection using the ports directive. All requests on port 8000 on the host machine (the system running Docker Compose) are redirected to the web container on port 80 where Nginx is running.The Volumes directive creates shared volumes between the host machine and the container. This will share the local app folder with the container and the volume will reside at /usr/share/nginx/html inside the container, overriding nginx’s default document root. Save and close the file.
Next create a new directory webapp in the current directory compose-test. This directory will hold the index.html file to serve the webserver. Next browse to the webapp directory and create a Index.html file having the given contents. Save an close the file.
# mkdir webapp
# cd webapp
# nano index.html
You have set up a demo site and a docker-compose.yaml file to create a containerized web server environment to serve. The next step is to set up this environment using Docker Compose.
Running Docker Compose
With all the files in places we can run Docker Compose to bring our environment up. Using the given command we will download the required Docker images, to create a container for the web service, and run the containerized environment silently.
# docker compose up -d
Docker Compose will first look for an image defined in the yaml file on the local system, and if the image is not found, it will download the image from Docker Hub.
[email protected]:/home/compose-test# docker compose up -d [+] Running 1/0 ⠿ Container compose-test-web-1 Running 0.0s
With the output you can see that the environment is up and running. You can also verify that the container is up and running by using the given command.
# docker compose ps
You can now access the demo application by pointing your browser to either of the two localhost:8000 if running this demo on your local machine, or your_server_IP:8000 if you are running this demo on a remote server.
Next to stop the container or delete the whole environment, you can run the given commands.
# docker compose stop
# docker compose down
Docker Compose is a utility that provides a simple environment for building and running applications without intervention. This tool provides portability and automation testing capabilities. This guide showed you how to install Docker Compose and set up a containerized environment based on the Nginx web server image. We also saw how to manage this environment using the compose command.