Skip to the content.

Docker Basics

This README provides an overview of essential Docker commands and concepts, including working with Docker images, containers, and using Docker in development and production environments.

Table of Content

  1. Role of Kubernetes and Docker
  2. Auto Scaling
  3. Image Pull Policy
  4. Service and Auto Discovery
  5. Selector and Labels
  6. Load Port and Load Balancer
  7. External Application Access
  8. Deployment Strategies
  9. Rolling Deployment
  10. Auto Scaling Deployment
  11. DaemonSet

Introduction to Docker

Docker is a platform for automating the deployment, scaling, and management of applications inside lightweight containers. Containers bundle an application with all its dependencies and configurations.

Docker Images vs Containers

Key Differences:

Docker Commands

Working with Images

Working with Containers

Docker Authentication

Build Docker Container

To build a Docker container, use the following command:

docker build -t <container-name-with-version> .

This command builds an image based on the Dockerfile in the current directory and tags it with the specified name and version.

Run Your Container

To run a container from an image:

docker run -p 3000:3000 <container-name-with-version>

This command maps the host’s port 3000 to the container’s port 3000, allowing you to access the application on localhost:3000.

Starting a New Container With Specific Name

To start a container with a specified name and port mapping:

docker run --name <container_name> -p 8080:80 <container_image_with_version>

This command starts a new container named <container_name> and maps port 80 inside the container to port 8080 on your host.

List Docker Containers

To list all containers, including stopped ones:

docker ps -a

Remove a Container

To remove a stopped container by its ID:

docker rm <container ID>

List Running Containers

To list only running containers:

docker ps

Stop a Running Container

To stop a running container:

docker stop <container name>

Remove a Running Container

To remove a running container:

docker rm <container name>

Rename a Container

To rename an existing container:

docker rename <from_name> <to_name>

Production/Development Docker Environment

When developing and deploying with Docker, it’s common to have different configurations for development and production.

Separate Development and Production Modes

Enable Hot-Reloading with Volumes

In development, use Docker volumes to mount your local code directory into the container:

docker run -p 5500:5500 -v $(pwd):/app <image-name>

Simplify the Development Workflow

Install nodemon and tsc in your dev environment. Then, use npm run dev to automatically rebuild and restart the application when changes are detected.


Development Dockerfile

For development, use a Dockerfile like the following:

# Use Node.js Alpine image for smaller builds.
FROM node:23-alpine

# Set working directory
WORKDIR /app

# Copy package.json and lockfile for dependency installation
COPY package.json yarn.lock ./

# Install dependencies
RUN npm install

# Copy the rest of the application files
COPY . ./

# Expose application port
EXPOSE 5500

# Set environment variables
ENV NODE_ENV=development
ENV PORT=5500
ENV HOSTNAME="0.0.0.0"

# Default command for development
CMD ["npm", "run", "dev"]

Running the Development Container

  1. Build the container

    docker build -f Dockerfile.dev -t rock-oak-backend-dev .
    

    Run the Docker Container with Hot-Reloading**: Use Docker volumes to map your local code to the container:

    docker run -p 5500:5500 -v $(pwd):/app rock-oak-backend-dev
    
  2. Ensure Dependencies are Installed in the Container: If you modify package.json, rebuild or reinstall dependencies inside the container:

    docker exec -it <container-id> npm install
    
  3. Use npm run dev: The dev script uses nodemon and tsc to watch for changes and restart the server automatically.

Production Dockerfile

For production, use a different Dockerfile, Dockerfile.prod, optimized for production:

FROM node:23-alpine

# Set working directory
WORKDIR /app

# Copy package.json and lockfile
COPY package.json yarn.lock ./

# Install production dependencies
RUN npm install --production

# Copy the rest of the application files
COPY . ./

# Build the application
RUN npm run build

# Expose application port
EXPOSE 5500

# Set environment variables
ENV NODE_ENV=production
ENV PORT=5500
ENV HOSTNAME="0.0.0.0"

# Start the application
CMD ["npm", "run", "start"]

To build and run for production:

docker build -t <image-name> .
docker run -p 5500:5500 <image-name>

Additional Resources


Here are your structured notes for Docker, including a table of contents:


1. Role of Kubernetes and Docker


2. Auto Scaling


3. Image Pull Policy


4. Service and Auto Discovery


5. Selector and Labels


6. Load Port and Load Balancer


7. External Application Access


8. Deployment Strategies


9. Rolling Deployment


10. Auto Scaling Deployment


11. DaemonSet


Let me know if you need any modifications or additional details! 🚀