DEV Community

Cover image for Docker Commands Cheat Sheet
Madhav Ganesan
Madhav Ganesan

Posted on • Updated on

Docker Commands Cheat Sheet

Docker is a platform that allows you to develop, ship, and run applications inside containers. Docker provides better resource management, allowing you to allocate specific resources (CPU, memory, etc.) to each container. This helps in optimizing resource usage and improving performance.

Containers

A container is a runnable instance of an image. They are lightweight, standalone, and executable packages that contain everything needed to run a piece of software, including the code, runtime, libraries, and dependencies.

Image

An image is a read-only template with instructions for creating a Docker container.Images are created using a Dockerfile and can be stored in registries like Docker Hub.

Create an image
A Dockerfile is a script containing a series of instructions on how to build a Docker image. For example, this is a Dockerfile for creating NodeJs backend application

# Use an official Node.js runtime as a parent image
FROM node:22

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 4100

# Define the command to run the app
CMD ["node", "server.js"]

Difference between running locally and using docker?

Local
Installing Redis locally involves downloading the Redis binaries or using a package manager (e.g., apt, brew) to install Redis on your machine. You would then start the Redis server using the redis-server command.

Docker
Running Redis in Docker involves pulling the Redis image from DockerHub using the docker pull redis command and then running a container using docker run command with specific options, such as port mapping.

Ease of Use and Deployment:

Local
Running Redis locally requires manual setup and configuration. You need to ensure that the Redis server is properly configured and running.

Docker
Running Redis in Docker simplifies deployment and management. Docker provides a consistent environment, and you can easily start, stop, and manage Redis containers using Docker commands.

Important Commands for Docker

Note: Open Docker Desktop before executing below commands to start daemon

Pulling Docker Images
To get started with Docker, you often need to pull images from a registry like Docker Hub. For example, to pull the official Redis image, you can use the following command:

docker pull redis

This command fetches the Redis image and makes it available on your local machine.

Pushing Docker Images
This command uploads an image to a Docker registry.

docker push <repository>/<image>:<tag>

Viewing Docker Images
This command displays a list of all pulled and present images on your local machine.

docker images

Remove a specific image

docker rmi <image_id>

Running Containers
To run a container from an image, use the docker run command. For example, to start a Redis container in detached mode (running in the background), use:

docker run --name <container_name> -p <host_port>:<container_port> -d <image>

Here’s what each option does:

--name : Assigns a name to your container.
-p : Maps port Ex. 6379:6379
-d: Runs the container in detached mode.

Start a container:

docker start <container_id>

Stop a container:

docker stop <container_id>

Stop a container immediately:

docker kill <container_id>

Remove a specific container:

docker rm <container_id>

Remove all stopped containers and dangling containers:

docker container prune -f

Remove all stopped containers, unused networks, and dangling images:

docker system prune -f

Remove all unused containers, networks, images, and volumes:

docker system prune --all --volumes

Lists all running containers

docker ps

Lists all containers, including those that are stopped

docker ps -a

Cleaning Up Build Cache
Docker caches intermediate layers during image builds to speed up the process. To remove all build cache and free up space, use:

docker builder prune -a

Managing Docker Compose
Docker Compose allows you to define and run multi-container Docker applications. To build and start services defined in your docker-compose.yml file, use:

docker-compose up --build

Sample docker-compose.yml file:

version: '3'
services:
  server:
    build:
      context: ./server
      dockerfile: Dockerfile
    ports:
      - "4100:4100"
    environment:
      NODE_ENV: production

This command builds images as necessary before starting the services.


Feel free to reach out if you have any questions or need further assistance. 😊📁✨

Top comments (0)