When working with microservices in Docker, it’s a best practice to separate the services into different containers. The interdependence makes it easier for developers to coordinate and scale applications, accelerating time to market for new features. For your application to function, you must manage and run multiple containers simultaneously. That's where Docker Compose comes in! Using this tutorial, let’s learn how to deploy a full-stack Flask app with a front and back end. You will also learn what is and how to use it in Container management. Docker Compose What Is Docker Compose? Docker Compose is a tool used to deploy and manage multiple Docker containers. It manages each container in isolation but coordinates interactions with each other. With Docker Compose, you can run an entire application stack by defining its volumes, services, and networks in a single YAML file. Docker Compose uses a scripting language called , an XML-based language, to write the manifest. With YAML, you can run several services using only one command. YAML Getting Started With Docker Compose You will build an application with two microservices, a front, and a backend. Then, use Docker Compose to connect the containers and run the application. Skip to step 4 if you have containerized your microservices. Prerequisites To create this project successfully, you will need the following: Familiarity with Docker containers Familiarity with APIs 1. Create an Application Create an application with two or more microservices. I created a Flask app with two microservices, a front end and a back end. If you don't want to create an app from scratch, to get started. fork this Flask application 2. Build Docker Images for Each Microservice Use Dockerfiles to create Images for each microservice. Use the following command: touch quote_disp/Dockerfile touch quote_gen/Dockerfile The quote_disp folder is the front end, and the qoute_gen hosts the app's backend. In the Dockerfiles, define the commands needed to build your application. The application file structure should include the new Docker files. In the Dockerfiles, add instructions to build and run the application in Docker containers. For my Flask App, it appears like this: FROM python:3.8-slim-buster COPY . /app WORKDIR /app RUN pip install -r requirements.txt ENTRYPOINT [ "python" ] CMD [ "app.py" ] Then, build the images using the following commands: docker build -t quote-gen-service ./quote_gen docker build -t quote-disp-service ./quote_disp Run command on the terminal to ensure that the image generation was a success. docker images 3. Run the Docker Containers Now that you have the images run the Images to get the containers running. Use to run the containers. docker run [OPTIONS] IMAGE [COMMAND] [ARG...] docker run -d --name quote-gen-container -p 5000:5000 quote-gen-service docker run -d --name quote-disp-container -p 5001:5001 quote-disp-service The above code represents the detached mode, which means you run the containers in the background. Next is the container and is the port number exposing the container. -d --name -p The port number has the syntax . HOST_PORT: CONTAINER _PORT Ports are not infinite. They are represented as 16-bit unsigned integers, meaning they go up to 65535. In general, just use any value between 1024 and 65535; values below 1023 are referred to as system ports and are used by other applications. Run to view the running containers. docker ps Click on the following prompt to view the application on the browser. You should see both applications on the browser : Did you notice that you cannot access the other application via the link on the front end? This is because the containers cannot communicate. To help them communicate, we have to create a network using Docker Compose. First, stop and remove all running containers with the following commands: docker stop $(docker ps -a -q) docker rm $(docker ps -a -q) 4. Create a Docker Compose Manifest Docker Compose makes it easier to manage multiple containers. Compose creates the necessary networks and connectivity between containers defined in the manifest file. Let's create a Docker Compose manifest to orchestrate our services. In the root folder, create a docker-compose.yml file and add the following information: version: Version"3.7" services: web1: build: ./quote_gen ports: - "5000:5000" web2: build: ./quote_disp ports: - "5001:5001" depends_on: - web1 In the above manifest, the refers to the Docker Compose version you are using. refer to the networks you are creating. In this case, you are using the web service names and instead of a container name. version Services web1 web2 Using the service name allows Docker Compose to handle load balancing between multiple replicas of the same container during scaling. Both services will communicate under ports and . Web 2 service depends on the web1 service. 5000:5000 5001:5001 refers to the container image used to build the containers during deployment. Build 5. Run Docker Compose Run the following command to start Docker Compose: docker compose up -d The result is that Docker compose spins up the containers, creating new networks. Next, navigate to the area on the terminal and click the network part to see the applications on the browser. ports Notice that you can now access the application's backend via the link on the front end. Docker Compose automatically linked the two containers. The containers are now communicating thanks to the networks set up by Docker Compose. The front end now has access to the backend APIs. What’s Next? I hope the tutorial was helpful in you getting started with Docker Compose. To further your knowledge of Docker containers, learn how to manage volumes and scale up your application. Also published . here