Docker Cheatsheet: Essential Commands You Need to Know
Tags: docker, devops, productivity, devrel
Published: 2024-07-09 15:16:50

Docker has become a go-to tool for developers, simplifying the process of creating, deploying, and running applications using containers. Here’s a handy cheat sheet of essential Docker commands to help you navigate Docker like a pro.

### 1. Docker Basics

- **docker --version**  
  This shows you the current version of Docker you have installed.
  ```
  docker --version
  ```

- **docker info**  
  This will list detailed information about your Docker setup.
  ```
  docker info
  ```

### 2. Working with Images

- **docker pull [image]**  
  This command downloads an image from the Docker Hub.
  ```
  docker pull [image_name]
  ```

- **docker images**  
  List all the images you have on your system.
  ```
  docker images
  ```

- **docker rmi [image]**  
  Remove an image from your system.
  ```
  docker rmi [image_name] e.g docker rmi postgres
  ```

### 3. Managing Containers

- **docker run [image]**  
  Create and start a new container from an image.
  ```
  docker run [image_name]
  ```

- **docker ps**  
  List all running containers.
  ```
  docker ps
  ```

- **docker ps -a**  
  List all containers, including those that are stopped.
  ```
  docker ps -a
  ```

- **docker stop [container_id]**  
  Stop a running container.
  ```
  docker stop [container_id]
  ```

- **docker start [container_id]**  
  Start a stopped container.
  ```
  docker start [container_id]
  ```

- **docker rm [container_id]**  
  Remove a stopped container.
  ```
  docker rm [container_id]
  ```

### 4. Accessing Containers

- **docker exec -it [container_id] /bin/bash**  
  Access a running container with an interactive shell.
  ```
  docker exec -it [container_id] /bin/bash
  ```

### 5. Building Images

- **docker build -t [name:tag] [path]**  
  Build a new image from a Dockerfile.
  ```
  docker build -t [name:tag] [path] e.g docker build -t app:latest .
  ```

### 6. Docker Networks

- **docker network ls**  
  List all networks.
  ```
  docker network ls
  ```

- **docker network create [network_name]**  
  Create a new network.
  ```
  docker network create [network_name] e.g docker network create 
  backend_default
  ```

- **docker network rm [network_name]**  
  Remove a network.
  ```
  docker network rm [network_name]
  ```

### 7. Docker Volumes

- **docker volume ls**  
  List all volumes.
  ```
  docker volume ls
  ```

- **docker volume create [volume_name]**  
  Create a new volume.
  ```
  docker volume create [volume_name] e.g docker volume create 
  backend_pgdata
  ```

- **docker volume rm [volume_name]**  
  Remove a volume.
  ```
  docker volume rm [volume_name]
  ```

### 8. Docker Compose

- **docker-compose up**  
  Start and run your Docker Compose services.
  ```
  docker-compose up
  ```

- **docker-compose down**  
  Stop and remove Docker Compose services.
  ```
  docker-compose down
  ```

- **docker-compose build**  
  Build or rebuild services.
  ```
  docker-compose build
  ```

This cheatsheet covers the most essential Docker commands to get you started. Whether you’re managing images, containers, networks, or volumes, these commands will help you streamline your workflow and make the most out of Docker.

If you found this article helpful, please like and share it. Comment below with any other Docker commands or tips you’ve found useful. Happy coding :)