Docker / Docker Compose

Defining and Running Multi-Container Apps

In this tutorial, we'll delve into the process of defining and running multi-container applications with Docker Compose. You'll learn how to write a 'docker-compose.yml' file and …

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers how to manage multi-container applications using Docker Compose.

Introduction

Welcome to this tutorial where we will explore how to define and run multi-container applications using Docker Compose. Docker Compose is a crucial tool in the Docker ecosystem and provides an efficient method to orchestrate multiple containers and create multi-container apps.

By the end of this tutorial, you will learn:
- What Docker Compose is and why it's beneficial.
- How to write a docker-compose.yml file.
- How to manage your application with Docker Compose.

Before we start, you should have Docker installed on your system. Basic knowledge of Docker and YAML syntax will be helpful but not mandatory.

Step-by-Step Guide

Concept of Docker Compose

Docker Compose is a tool for defining and managing multi-container Docker applications. It uses a YAML file (docker-compose.yml) to configure your application's services, which allows you to start all services with a single command.

Writing a docker-compose.yml file

A docker-compose.yml file is a YAML file that defines services, networks, and volumes. Here's an example:

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

In this example, we have two services: web and redis. The web service builds from the Dockerfile in the current directory and maps port 5000 on the host to port 5000 on the container. The redis service uses the redis image from Docker Hub.

Running docker-compose up

To start your application, navigate to the directory with your docker-compose.yml file and run the command docker-compose up. This command will start your application and its services.

Code Examples

Let's look at a practical example where we have an application with a web server (Node.js) and a database (MongoDB).

Dockerfile for Node.js app

First, let's create a Dockerfile for our Node.js application:

# Dockerfile
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]

Docker Compose file

Next, we will write our docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
    links:
      - db
  db:
    image: mongo

In this example, we have two services: web (Node.js server) and db (MongoDB). The web service builds from the Dockerfile in the current directory, maps port 8080 from the host to the container, and links to the db service. The db service uses the mongo image from Docker Hub.

Running Docker Compose

Run docker-compose up to start your application:

$ docker-compose up

You should see output indicating that both the web and db services have started.

Summary

In this tutorial, we've covered the basics of Docker Compose, including writing a docker-compose.yml file and running multi-container applications. We've also walked through a practical example with a Node.js server and a MongoDB database.

For further learning, consider exploring more complex docker-compose.yml files and using Docker Compose in conjunction with Docker Swarm for production-level applications.

Practice Exercises

  1. Simple Exercise: Create a docker-compose.yml file for an application with a Python Flask server and a MySQL database.
  2. Intermediate Exercise: Add a Redis cache service to the application from the first exercise.
  3. Advanced Exercise: Create a multi-container application with a load balancer (e.g., Nginx), a web application (e.g., Node.js), and a database (e.g., PostgreSQL).

Remember that practice is key in mastering Docker Compose. Happy learning!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Date Difference Calculator

Calculate days between two dates.

Use tool

Image Converter

Convert between different image formats.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help