DEV Community

Cover image for Docker #2023 Cheat Sheet
Sm0ke
Sm0ke

Posted on • Originally published at docs.appseed.us

Docker #2023 Cheat Sheet

Hello Coders!

A Docker Cheat Sheet provides a quick reference guide for common Docker commands and concepts.

For newcomers, Docker is a containerization platform that allows you to package and distribute applications along with their dependencies in containers.

Here's a Docker cheat sheet with commonly used commands and concepts:


Docker Commands

Pull an Image

Download a Docker image from a registry (e.g., Docker Hub).

$ docker pull image_name:tag
Enter fullscreen mode Exit fullscreen mode

List Images

View a list of locally available Docker images.

$ docker images
Enter fullscreen mode Exit fullscreen mode

Run a Container

Start a new container from an image.

$ docker run image_name:tag
Enter fullscreen mode Exit fullscreen mode
  • Add -d to run in detached mode.
  • Use --name to specify a custom container name.
  • Use -p to map ports (e.g., -p host_port:container_port).

List Containers

View a list of running containers.

$ docker ps
Enter fullscreen mode Exit fullscreen mode
  • Add -a to see all containers, including stopped ones.

Stop a Container

Stop a running container gracefully.

$ docker stop container_id
Enter fullscreen mode Exit fullscreen mode

Remove a Container

Delete a stopped container.

$ docker rm container_id
Enter fullscreen mode Exit fullscreen mode

Remove an Image

Delete a Docker image.

$ docker rmi image_name:tag
Enter fullscreen mode Exit fullscreen mode

Inspect Container

View detailed information about a container.

$ docker inspect container_id
Enter fullscreen mode Exit fullscreen mode

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications.

Create and Start Services

Start containers defined in a docker-compose.yml file.

$ docker-compose up
Enter fullscreen mode Exit fullscreen mode
  • Add -d to run in detached mode.

Stop Services

Stop containers defined in a docker-compose.yml file.

$ docker-compose down
Enter fullscreen mode Exit fullscreen mode

List Services

View a list of services defined in the docker-compose.yml file.

$ docker-compose ps
Enter fullscreen mode Exit fullscreen mode

Docker Volumes

Docker volumes are used to persist data between container runs.

Create a Volume

Create a named volume.

$ docker volume create volume_name
Enter fullscreen mode Exit fullscreen mode

List Volumes

View a list of available volumes.

$ docker volume ls
Enter fullscreen mode Exit fullscreen mode

Attach a Volume to a Container

Mount a volume to a container.

$ docker run -v volume_name:/container_mount_point image_name
Enter fullscreen mode Exit fullscreen mode

Docker Networks

Docker networks allow containers to communicate with each other.

Create a Network

Create a custom network.

$ docker network create network_name
Enter fullscreen mode Exit fullscreen mode

List Networks

View a list of available networks.

$ docker network ls
Enter fullscreen mode Exit fullscreen mode

Attach a Container to a Network

Connect a container to a custom network.

$ docker network connect network_name container_name
Enter fullscreen mode Exit fullscreen mode

Dockerfile

A Dockerfile is a script used to create a Docker image.

Create a Dockerfile

Create a Dockerfile in your project directory.

# Example Dockerfile
FROM base_image:tag
RUN command_to_run
Enter fullscreen mode Exit fullscreen mode

Build an Image

Build a Docker image using a Dockerfile.

$ docker build -t image_name:tag .
Enter fullscreen mode Exit fullscreen mode

Additional Docker Commands

Logs

View container logs.

$ docker logs container_id
Enter fullscreen mode Exit fullscreen mode

Exec

Run a command in a running container.

$ docker exec -it container_id command
Enter fullscreen mode Exit fullscreen mode

Prune

Remove stopped containers, unused networks, and dangling images.

$ docker system prune
Enter fullscreen mode Exit fullscreen mode

Login to a Registry

Log in to a Docker image registry (e.g., Docker Hub).

$ docker login
Enter fullscreen mode Exit fullscreen mode

✅ Docker Templates

This section contains a few open-source starters that include production-ready Docker setups:

👉 Django Bootstrap 5 Volt

Free starter built on top of Bootstrap 5 and Django with Database, DB Tools, OAuth via Github and Docker Support.

Django Bootstrap 5 Volt - Open-source Starter with Docker Support.

👉 Django Modernize

Open-Source Seed Project crafted on top of Modernize Bootstrap 5 and Django.

The product comes with session-based authentication, DB tools, and Docker support.

Django Modernize - Open-source Starter with Docker Support.


✅ In Summary

This cheat sheet provides a quick reference for common Docker commands and concepts.

Docker has many more features and options, so be sure to consult the official Docker documentation for more detailed information: https://docs.docker.com/


✅ Resources

Top comments (0)