Skip to content
Sushmoy
Go back

Docker Cheat Sheet

Edit page

Basic Usage

Running an image

docker run -it <image-name>

-it makes the shell interactive.

Download, build an image, and start it with bash

docker run -p 5173:5173 -it --name hello-node node:24 bash

-p exposes the port to the host.

bash starts the container with bash.

Building an image from a Dockerfile

docker build -t <image-name> -f <Dockerfile-name> .

-f file name option.

List the containers

docker ps -a

-a lists all the containers.

Stop the container

docker stop <container-name>

Dockerfile example

FROM node:24
WORKDIR /usr/src/app
COPY . .
RUN npm ci --omit=dev
CMD [ "npm", "run", "dev", "--", "--host" ]

Multi-stage Dockerfile example

FROM node:24 AS base
WORKDIR /usr/src/app
COPY . .
RUN npm ci

FROM base AS test-stage
RUN npm run test

FROM test-stage AS build-stage
RUN npm run build

FROM nginx:1.29-alpine
COPY --from=build-stage /usr/src/app/dist /usr/share/nginx/html

Docker Compose

docker compose -f <compose.yml> up --build -d

--build rebuilds the images inside.

-d runs the compose in the background quietly.

docker compose -f <compose.yml> down

Docker Compose example

services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - 5173:5173
    volumes:
      - ./frontend:/usr/src/app
    container_name: patientor-front

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - 3001:3001
    volumes:
      - ./backend:/usr/src/app
    container_name: patientor-back
	
  nginx:
    image: nginx
    volumes:
      - ./templates:/etc/nginx/templates
    ports:
      - "8080:80"
    environment:
      - NGINX_HOST=foobar.com
      - NGINX_PORT=80

volumes let us mount a file path from host to the container for hot-reloading during the development.

Nginx Proxy configuration example

events {}

http {
    server {
        listen 80;

        location / {
            proxy_pass http://frontend:80;
        }

        location /api {
            proxy_pass http://backend:3001/api;
        }
    }
}

proxy_pass recieves the http request and routes them to the correct internal Docker container based URL path.

Architecture

└── my-app
    ├── frontend
    |    └── Dockerfile
    ├── backend
    |    └── Dockerfile
    ├── nginx.conf
    └── docker-compose.yml

Real-World Application

I recently applied this exact architecture to containerize my Patientor application. It uses the multi-stage Dockerfiles and Nginx reverse proxy setup outlined above to manage both the local development environment and the optimized production build.


Edit page
Share this post on: