Youssef Samir

Get in touch

Docker

A cheat sheet for Docker commands and workflows to manage containerized applications and development environments.

Basic Commands

  • info: Display system information
  • version: Display system version
  • login: Log in to a Docker registry
    • -u <username>
    • -p <password>
  • push <registry username>/<image name>:<tag>: Push an image to a registry
  • pull [image Name]: Pull an image from a registry
  • run [image Name]: Run a container
    • -d: Detached mode
    • -p: host_port:container_port image_name
  • start [container Name]: Start stopped containers
  • ps: List running containers
    • -a: List all containers (running and stopped)
  • stop [container Name]: Stop a container
  • kill [container Name]: Kill a container
  • image inspect [image name]: Get image info
  • rm [container name]: Remove a stopped container
  • rm $(docker ps -a -q): Remove all stopped containers
  • rmi [Image Name]: Delete an image
  • system prune -a: Remove all images not in use by any containers
  • container exec -it [container name] --bash: Attach a bash shell to a running container
  • images: List all images pulled
  • build: Build an image using a Dockerfile
    • -t [name:tag]
    • -f [file name]: Specify Dockerfile location
  • tag [image name] [name:tag]: Tag an existing image
  • create volume [volume name]: Create a new volume

Volume Management

  • volume
    • ls: List volumes
    • inspect [volume name]: Display volume info
    • rm [volume name]: Delete a volume
    • prune: Delete all unused volumes

Example Dockerfile

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

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose port 8080
EXPOSE 8080

# Specify the user to run the container as
USER node

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