A short introduction to Docker Compose

Before looking at what Docker Compose is, let's go through some facts. Shipping code to the server is always difficult, especially when you want to scale it. This is mainly because we have to manually create the same environment and make sure the application has all the necessary connectivity (to other services). This was a major pain point for teams while shipping and scaling their code.

"Shipping code to the server is difficult."

Containers were the game-changer in this field. They helped bundle the entire application along with dependencies in a shippable container, and all we need is to provide an environment in which these containers can run. This simplified the process of shipping code to the server among development teams. This also reduced the amount of time a team spent making sure that the application ran seamlessly across the environment. 

Containers solve the application deployment problem, but how do we scale them?

Docker Compose is an option we can consider. First, let's see what Docker Compose is, and then see what problems it solves.

Docker Compose is a tool that helps us define and run multi-container Docker applications with a single file. That is, we use a .yaml file to define the requirements and/or dependencies of the application. Then, with Docker Compose, we can create newer deployments and start our applications, as defined in the Docker Compose file. We can also use Docker Compose to seamlessly scale the deployed applications.

So, what is required in a Docker Compose file?

The following code segment is a sample Docker Compose file that will start a Redis database on port 5000:

version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
  redis:
    image: "redis:alpine"

Let's take a look at the code segment sequentially:

  • The first line of the Docker Compose file should be the version of the Docker Compose tool. 
  • Then, we need to specify all the necessary services that we need for our application to run. They should be defined in the services: section.
  • We can also define multiple services here, giving a name to each (web and redis).
  • This is followed by how to build the service (either via a command to build or referring to a Docker image). 
  • If the application needs any port access, we can configure it using 5000:5000 (that is, internal port: external port). 
  • Then, we have to specify the volume information. This basically tells Docker Compose to serve the files from the location specified. 

Once we have specified the services that are required for our application, we can start the application via Docker Compose. This will start the entire application, along with the services, and expose the services on the port specified. 

With Docker Compose, we can perform the following operations:

  • Start: docker-compose -f <docker_file> up
  • Stop: docker-compose -f <docker_file> down

We can also perform the following operations:

  • List the running services and their statusdocker ps 
  • Logsdocker log <container_id>

In the Compose file, we can add the project name, as follows:

version: '3'
COMPOSE_PROJECT_NAME: "myapp" services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine"

This can be used to identify multiple environments. With the help of this, we can isolate multiple environments. This helps us handle multiple instances across various dev, QA, and prod environments.

Docker Compose itself is a great tool for deploying your application, along with all the services it needs. It provides infrastructure as a code. It is an excellent choice for development, QA, and other environments, except for production. But why?

Docker Compose is really good for creating and starting your application. However, when you want to update an existing container, there will be a definite downtime since Docker Compose will recreate the entire container (there are a few workarounds to make this happen but still, Docker Compose needs some improvement in this regard). In real-world scenarios, you would often need some kind of orchestration and advanced features for scalability.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.107.241