Docker Commands 101

This Docker cheat sheet simplifies essential commands. It's packed with clear explanations and examples to save beginners time and frustration.

6 min read0 views

I spent last weekend diving deep into Docker and came out the other side with a solid understanding of its fundamentals. To cement what I learned, I put together this command cheat sheet. It's aimed at helping you (and future me) work confidently with Docker day to day.

Checking the Docker version

docker -v

Shows the version of Docker installed on your machine.

Inspecting containers

List running containers

docker ps
# or
docker container ls

List all containers (running and stopped)

docker container ls -a
  • -a or --all: show all containers, not just running ones.

Removing containers and images

Remove a container

docker rm <container_name>

List all images

docker images

Remove an image

docker rmi <image_name>
  • rmi: remove one or more images by name or ID.

Creating and running containers

Run a container interactively

docker run -it <image_name>
  • -i: keep STDIN open.
  • -t: allocate a pseudo-TTY (an interactive shell).

Run in the background (detached mode)

docker run -it -d <image_name>
  • -d: detached mode, runs in the background.

Run with a specific name

docker run --name my-container -it <image_name>
  • --name: assign a custom name to the container.

Pulling images

docker pull <image_name>

Downloads an image from Docker Hub.

Starting and stopping containers

Start an existing container

docker start <container_name>

Stop or kill a container

docker stop <container_name>
docker kill <container_name>
  • stop: graceful shutdown.
  • kill: immediate termination.

Running commands in containers

Execute a one-off command

docker exec <container_name> <command>
# Example:
docker exec my-container ls

Start an interactive shell

docker exec -it <container_name> /bin/bash

Detach from the container but keep it running

Ctrl+P, then Ctrl+Q

This only works for containers started with -it. exit or Ctrl+D ends the shell instead, which usually stops the container too.

Building images

docker build -t <image_name> <path>
  • -t: tags the image with a name.
  • <path>: typically . to refer to the current directory.

For example:

docker build -t my-server .

Port mapping

docker run --name <name> -p <host_port>:<container_port> <image_name>
  • -p: maps ports from the container to the host.

For example:

docker run --name mongo-service -p 27017:27017 mongo

Update port mapping for an existing container

docker container update --publish-add 8080:80 <container_name>

Environment variables

docker run -e KEY=VALUE <image_name>

You can pass multiple -e flags for multiple environment variables. For example:

docker run -e ENV=production -e DB_URL=mongodb://localhost my-app

Volumes

Create a volume

docker volume create my-volume

List all volumes

docker volume ls

Mount a volume to a container

docker run -v my-volume:/data/db mongo
  • -v: mounts a volume.
  • /data/db: where MongoDB stores its data internally.

Networks

Create a custom network

docker network create my-network

List networks

docker network ls

Types of Docker networks

  • bridge: the default. An isolated internal network on the host.
  • host: shares the host's networking directly.

Run a container on a specific network

docker run --name my-app --network my-network -p 3000:3000 my-image

Pushing custom images to Docker Hub

  1. Create a repo on hub.docker.com (e.g. myusername/myimage).
  2. Tag the image locally:
    docker build -t myusername/myimage .
  3. Log in:
    docker login
  4. Push:
    docker push myusername/myimage

Docker Compose

An example docker-compose.yaml

services:
  mongodb:
    image: "mongo"
    container_name: mongodb
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db

  backend:
    build: .
    container_name: backend_app
    depends_on:
      - mongodb
    ports:
      - "3000:3000"
    environment:
      MONGO_URL: "mongodb://mongodb:27017"

volumes:
  mongodb_data:

Run the compose setup

docker-compose up

Tear everything down

docker-compose down --volumes

This sheet is still evolving as I go deeper into Docker, but I hope it helps you as much as it helps me stay productive and confident.