Deploying Applications with Docker Compose

Deploying Applications with Docker Compose

17 September 2020

Docker compose is a tool which is used to deploy and manage multi-container applications on Docker nodes operating in single-engine nodes. Modern application consists of multiple smaller services. Deploying and managing lots of services is a tough task. This is the situation where Docker Compose reduces the stress, it will deploy and manage the different services for us.

Installing Compose

Docker Compose is an external Python binary that we have to install on a host running the Docker Engine. For Windows 10 and Mac it is included as a part of the Docker for Windows (DfW) and Docker for MAC (DfM). For Linux we have to install it separately on the host.

Compose files

Compose uses YAML files to define multi-service applications. The default name for the Compose YAML file is docker-compose.yml but we can also use custom filenames by specifying -f flag along with the filename in the command. The YAML file is passed to the docker-compose binary and Compose deploy it via the Docker Engine API.

Below is a sample example of Compose file:

version: "3.5" Services: demo-app: build: . command: node app.js ports: - "9090:9090" networks: - demo-net volumes: - demo_vol: /code redis: image: "redis:alpine" networks: - demo-net networks: demo-net: volumes: demo-vol:

Use the Compose to bring the app up. Run the below command to bring the app up:

docker-compose up

The default name of the Compose file is docker-compose.yml. If our Compose file has some other name then while running the above command we have to specify it with -f flag. The following example will deploy an application from a Compose file called compose_demo.yml:

docker-compose -f compose_demo.yml up

Note: We can use -d flag in the docker-compose command to bring the application up in the background.

Now, the application is up and running and we can use the normal docker commands to view the images, containers, networks, and volumes that Compose created.

Managing an application with Compose

We can manage our running application through Docker Compose, i.e. start, stop, delete and get the status of the application. As the application is running, we can bring it down by running the below command:

docker-compose down

Bring the application up again but in the background:

docker-compose up -d

Get the current status of the application:

Docker-compose ps

See the running processes inside of each service(container):

Docker-compose top

Stop the application without deleting its resources:

docker-compose stop

Restart the application:

Docker-compose restart

Stop and delete the application with the below single command:

docker-compose down

The above docker-compose down command will stop and delete a running application. It will delete containers and networks, but not volumes and images.

Request a quote