Skip to main content

Using Docker: A Practical Guide

Essential Docker Commands

Container Management

1. Running Containers

# Run a container with the default image (nginx in this case)
docker run nginx[^1]

# Run in detached mode (runs in the background without attaching to the terminal)
docker run -d nginx

# Run with port mapping (maps port 8080 on the host to port 80 in the container)
docker run -p 8080:80 nginx

# Run with environment variables (set environment variables inside the container)
docker run -e MY_VAR=value nginx

# Run with a name (assigns a human-readable name to the container)
docker run --name my-container nginx

2. Container Operations

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Stop a container
docker stop container_id

# Start a stopped container
docker start container_id

# Remove a container
docker rm container_id

# Force remove a running container
docker rm -f container_id

Image Management

1. Working with Images1

# List images
docker images

# Pull an image
docker pull ubuntu:latest

# Remove an image
docker rmi image_name

# Build an image from Dockerfile
docker build -t my-app:1.0 .

2. Image Information

# View image details
docker inspect image_name

# View image history
docker history image_name

Working with Dockerfiles2

Basic Dockerfile Structure

# Use an official base image
FROM node:16-alpine[^4] # Specifies the base image (Node.js in this case)

# Set working directory
WORKDIR /app # Sets the directory inside the container where commands will run

# Copy package files
COPY package*.json ./ # Copies package files into the container

# Install dependencies
RUN npm install # Runs npm install to install dependencies inside the container

# Copy application code
COPY . . # Copies all application files into the container

# Expose port
EXPOSE 3000 # Declares that the container will listen on port 3000

# Set startup command
CMD ["npm", "start"] # Defines the command to run the application

Explanation of Key Dockerfile Instructions3:

  • FROM – Defines the base image that will be used to build the new image
  • WORKDIR – Sets the working directory inside the container
  • COPY – Copies files from the host machine to the container
  • RUN – Executes a command inside the container during the image build process
  • EXPOSE – Specifies which port the application will listen on inside the container
  • CMD – Defines the default command to run when a container starts from this image

Building Images

# Basic build
docker build -t my-app .

# Build with different Dockerfile
docker build -f Dockerfile.dev -t my-app-dev .

# Build with build arguments
docker build --build-arg VERSION=1.0 -t my-app .

Docker Compose4

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. Instead of manually running docker run multiple times for different containers, you can use a docker-compose.yml file to define your entire application stack in a single file and manage it easily.

Basic docker-compose.yml5

version: '3'
services:
web:
build: . # Builds the application from the current directory
ports:
- "3000:3000" # Maps port 3000 on the host to 3000 in the container
environment:
- NODE_ENV=production # Defines environment variables
depends_on:
- db # Ensures that the database starts before the web service
db:
image: mongo:latest # Uses the official MongoDB image
volumes:
- db-data:/data/db # Maps a named volume for persistent storage

volumes:
db-data:

Explanation of Key docker-compose.yml Elements6:

  • version – Specifies the Docker Compose file format version
  • services – Defines the different containers/services
  • web – The application container
    • build – Builds the image from the local directory
    • ports – Maps container ports to host machine ports
    • environment – Defines environment variables inside the container
    • depends_on – Ensures that the database starts before the web service
  • db – The database container
    • image – Specifies which Docker image to use
    • volumes – Maps a volume for persistent data storage

Compose Commands7

# Start services
docker-compose up

# Start in detached mode
docker-compose up -d

# Stop services
docker-compose down

# View logs
docker-compose logs

# Rebuild services
docker-compose up --build

Best Practices8

1. Container Management

  • Always use meaningful container names
  • Clean up unused containers regularly
  • Use docker-compose for multi-container apps
  • Set appropriate resource limits

2. Development Workflow

  • Use volumes for development
  • Implement hot-reloading
  • Use docker-compose for local development
  • Keep development and production configs separate

3. Debugging

# View container logs
docker logs container_id

# Follow log output
docker logs -f container_id

# Execute commands in container
docker exec -it container_id bash

# View container stats
docker stats

4. Resource Cleanup

# Remove all stopped containers
docker container prune

# Remove unused images
docker image prune

# Remove all unused objects
docker system prune

# Remove volumes
docker volume prune

Next Steps

In the next section, we'll look at how to deploy a Docusaurus application using Docker, including:

  • Creating a production-ready Dockerfile
  • Setting up automatic builds
  • Implementing continuous deployment
  • Managing updates and rollbacks

Footnotes

  1. Docker images are the building blocks of containers. They are read-only templates that contain a set of instructions for creating a container that can run on the Docker platform.

  2. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It's like a recipe for creating Docker images.

  3. Each instruction in a Dockerfile creates a new layer in the image. Layers are cached and reused, which makes building images faster and more efficient.

  4. Docker Compose simplifies the process of managing multi-container applications by allowing you to define your application stack in a single YAML file.

  5. The docker-compose.yml file is a YAML file that defines services, networks, and volumes for a Docker application. It's the main configuration file for Docker Compose.

  6. Each service in a Docker Compose file represents a container. Services can be built from a Dockerfile or pulled from a registry, and they can be configured with various options like ports, volumes, and environment variables.

  7. Docker Compose commands manage the entire application lifecycle, from starting services to viewing logs. The -d flag runs services in detached mode (background).

  8. Following best practices helps ensure your Docker containers are secure, efficient, and maintainable. This includes proper resource management, logging, and cleanup procedures.