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 -vShows the version of Docker installed on your machine.
Inspecting containers
List running containers
docker ps
# or
docker container lsList all containers (running and stopped)
docker container ls -a-aor--all: show all containers, not just running ones.
Removing containers and images
Remove a container
docker rm <container_name>List all images
docker imagesRemove 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 lsStart an interactive shell
docker exec -it <container_name> /bin/bashDetach from the container but keep it running
Ctrl+P, then Ctrl+QThis 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 mongoUpdate 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-appVolumes
Create a volume
docker volume create my-volumeList all volumes
docker volume lsMount 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-networkList networks
docker network lsTypes 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-imagePushing custom images to Docker Hub
- Create a repo on hub.docker.com (e.g.
myusername/myimage). - Tag the image locally:
docker build -t myusername/myimage . - Log in:
docker login - 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 upTear everything down
docker-compose down --volumesThis 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.