Tutorial

Local Development with Docker Compose

This tutorial guides you through setting up a complete development environment using Docker Compose, essential for managing software migrations. You'll learn to create and run multi-container applications, troubleshoot common issues, and gain practical skills to enhance your development workflow.

Difficulty
Beginner

Tutorial: Local Development with Docker Compose

Learning Objectives and Outcomes

In this tutorial, you will:

  • Understand the basics of Docker and Docker Compose.
  • Set up a complete development environment using Docker Compose.
  • Create a multi-container application and manage its lifecycle.
  • Learn to troubleshoot common issues during the setup.

By the end of this tutorial, you will be proficient in creating a local development environment that simulates production-like conditions, making your software migration projects smoother and more efficient.

Prerequisites and Setup

Before you start, ensure that you have the following:

  • Docker: Installed on your local machine. You can download it from Docker's official website.
  • Basic knowledge of Docker: Familiarity with Docker commands and concepts such as images, containers, and volumes.

Installation Steps

  1. Install Docker: Follow the installation instructions for your operating system on the Docker website.
  2. Verify Installation: Open your terminal or command prompt and run:
    docker --version
    docker-compose --version
    
    Ensure both commands return version numbers without errors.

Step-by-Step Instructions with Examples

Step 1: Create a Project Directory

Create a new directory for your project:

mkdir my-docker-app
cd my-docker-app

Step 2: Create a Dockerfile

The Dockerfile defines your application's environment. Create a file named Dockerfile in your project directory:

# Use the official Node.js image as a base
FROM node:14

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application files
COPY . .

# Expose the application port
EXPOSE 8080

# Command to run the application
CMD [ "node", "app.js" ]

Step 3: Create a Docker Compose File

Create a file named docker-compose.yml in the same directory:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
    volumes:
      - .:/usr/src/app

This configuration defines a service named web, which builds the Docker image from the current directory, maps port 8080, and sets up a volume for live updates.

Step 4: Build and Run Your Container

In your project directory, run:

docker-compose up --build

This command builds the Docker image and starts the container. You should see logs indicating that your application is running.

Step 5: Access Your Application

Open your browser and navigate to http://localhost:8080. You should see your application running.

Key Concepts Explained Along the Way

  • Dockerfile: A text file containing instructions to build a Docker image. It defines the environment and configuration needed to run your application.
  • Docker Compose: A tool for defining and running multi-container Docker applications. It allows you to manage services, networks, and volumes in a single file.
  • Services: In Docker Compose, services are defined in the docker-compose.yml file. Each service corresponds to a container that runs a specific application component.

Common Mistakes and How to Avoid Them

  • Forgetting to rebuild the image: If you change your Dockerfile, always run docker-compose up --build to rebuild the image.
  • Port conflicts: Ensure the port you are mapping (e.g., 8080) is not already in use by another application.
  • Volume issues: If you don’t see updates in your application, check that your volume is correctly set up for live editing.

Exercises and Practice Suggestions

  • Modify the application code and observe how the live updates work.
  • Add another service to your docker-compose.yml, such as a database (e.g., MySQL or MongoDB) to see how services can interact.
  • Experiment with different ports and configurations to deepen your understanding of Docker Compose.

Next Steps and Further Learning

  • Explore the Docker documentation for more advanced topics like networking and security.
  • Learn about orchestrating multiple containers with Docker Swarm or Kubernetes.
  • Experiment with deploying your Docker containers to cloud platforms like AWS, Google Cloud, or Azure.

By mastering Docker Compose, you are well on your way to managing your software migrations more effectively, ensuring a smooth transition from legacy systems to modern architectures.