How to Set Up Docker for a Web App

A guide on setting up Docker for web applications, covering the creation of │ Dockerfiles for environment definition, building images, running containers, and │ using Docker Compose for multi-service applications.

Beginner

Setting up Docker for a web application involves containerizing your application and its dependencies, ensuring consistent environments across development, testing, and production. Here's a general guide covering the essential components:

1. Install Docker

First, ensure Docker is installed on your system. You can download Docker Desktop for your operating system from the official Docker website. For command-line tools on Linux, use your distribution's package manager (e.g., sudo apt install docker.io on Ubuntu).

2. Create a Dockerfile

A Dockerfile is a text file containing instructions for Docker to build an image. It defines the environment, dependencies, and how to run your application.

Example Dockerfile for a Node.js application:

# Use an official Node.js runtime as a parent image
FROM node:18-alpine

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json (or yarn.lock)
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the rest of your application code
COPY . .

# Expose the port your application listens on
EXPOSE 3000

# Define the command to run your app
CMD [ "npm", "start" ]

Explanation of common Dockerfile instructions: * FROM <image>: Specifies the base image to build upon. * WORKDIR /app: Sets the current working directory inside the container. * COPY <src> <dest>: Copies files and directories from your host machine to the container. * RUN <command>: Executes commands during the image build process (e.g., installing dependencies). * EXPOSE <port>: Informs Docker that the container listens on this port at runtime. * CMD ["executable", "param1", "param2"]: Specifies the default command to run when the container starts.

3. Build Your Docker Image

Navigate to your project's root directory (where your Dockerfile is) in your terminal and run the build command:

docker build -t your-app-name .
  • docker build: The command to create a Docker image.
  • -t your-app-name: Tags the image with a name (e.g., my-nodejs-app).
  • .: Specifies the build context (the current directory), telling Docker where to find the Dockerfile and application files.

4. Run Your Docker Container

Once the image is built, you can create and run a container from it.

docker run -p 80:3000 your-app-name
  • docker run: The command to start a container.
  • -p 80:3000: Maps port 3000 inside the container (the one your app listens on) to port 80 on your host machine. This allows you to access your app via http://localhost.
  • your-app-name: The name of the Docker image you want to run.

Your web application will now be running inside a Docker container, accessible via your browser.

5. Using Docker Compose for Multi-Service Applications

For applications involving multiple services (e.g., a web app and a database), Docker Compose is invaluable. It uses a docker-compose.yml file to define and manage all services.

Example docker-compose.yml for a Node.js app with a PostgreSQL database:

version: '3.8' # Specifies the Compose file format version

services:
  web:
    build: . # Build the image from Dockerfile in the current directory
    ports:
      - "3000:3000"
    volumes:
      - .:/app # Mount local code for live updates (development)
    environment:
      DATABASE_URL: postgresql://user:password@db:5432/mydatabase
    depends_on:
      - db # Ensure database starts before web service
    networks:
      - app-network

  db:
    image: postgres:14-alpine # Use a pre-built PostgreSQL image
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - db_data:/var/lib/postgresql/data # Persist database data
    networks:
      - app-network

volumes:
  db_data: # Define the named volume

networks:
  app-network: # Define a custom network

To use Docker Compose, place the docker-compose.yml file in your project root and run:

docker compose up -d --build
  • docker compose up: Builds images (if --build is used) and starts the services.
  • -d: Runs containers in detached mode (in the background).

To stop and remove the containers, networks, and volumes:

docker compose down

This setup provides a reproducible and isolated environment for your web application, making development and deployment much smoother.