Skip to main content

Command Palette

Search for a command to run...

Docker Project: Part 1 – Building a Multi-Container Application with Docker

Updated
2 min read
Docker Project: Part 1 – Building a Multi-Container Application with Docker
P

I’m a Cloud/DevOps enthusiast currently learning how to build and manage reliable, scalable solutions. I’m excited about exploring modern technologies and best practices to streamline development and deployment processes. My aim is to gain hands-on experience and contribute to creating robust systems that support growth and success in the tech world.

Tuesday, 24th September 2024


Introduction to the Project

In this project, we'll build a simple multi-container web application using Docker. The application will have a front-end (NGINX) and a back-end (MySQL) service. We'll leverage various Docker concepts like Dockerfiles, Docker Compose, volumes, and networking to bring everything together.


Step 1: Setting Up the Project Structure

To get started, create a new directory for your project:

mkdir docker-multi-container-app
cd docker-multi-container-app

Inside this directory, we'll create separate folders for the web and database components.

mkdir web db

Step 2: Creating Dockerfile for the Web Service (NGINX)

Next, we’ll set up an NGINX web server. Create a Dockerfile inside the web/ directory:

touch web/Dockerfile

Add the following content to the Dockerfile:

FROM nginx:latest
COPY ./html /usr/share/nginx/html

This Dockerfile sets up the NGINX image and copies an HTML file to serve as our website.


Step 3: Setting Up the Database (MySQL)

For the database, we’ll use the MySQL official image. There’s no need for a custom Dockerfile for the database, but we’ll configure the environment variables later in the Compose file.


Step 4: Creating the Docker Compose File

Now, we’ll tie everything together using Docker Compose. In the root of your project directory, create a docker-compose.yml file:

touch docker-compose.yml

Add the following content:

version: '3'
services:
  web:
    build: ./web
    ports:
      - "80:80"
    networks:
      - app-network

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - app-network

networks:
  app-network:

volumes:
  db_data:

Step 5: Running the Application

To start the entire application, simply run:

docker-compose up

Docker Compose will build and start both services (web and database). You’ll see logs from both services in your terminal.


In the Next Blog

In the next part of this project, we’ll dive deeper into how networking and volumes work in this setup. We’ll also explore how to scale services and manage the application in a production-like environment.

More from this blog

Untitled Publication

27 posts