Docker Commands 101
6 min read
•
0 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 created this command cheat sheet. It's aimed at helping you (and future me!) work confidently with Docker day-to-day.
🐳 Checking 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 + 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 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 (interactive shell).
Run in background (detached mode):
docker run -it -d <image_name>
d: Detached mode. Runs in background.
Run with a specific name:
docker run --name my-container -it <image_name>
-name: Assign a custom name to the container.
📥 Pulling Docker Images
docker pull <image_name>
Downloads an image from Docker Hub.
▶️ Start and Stop 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
Exit container but keep it running:
CTRL/CMD + D
🧱 Building Images
docker build -t <image_name> <path>
t: Tags the image with a name.
<path>: Typically.to refer to the current directory.
Example:
docker build -t my-server .
🌐 Port Mapping
docker run --name <name> -p <host_port>:<container_port> <image_name>
p: Maps ports from container to host.
- 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.Example:
docker run -e ENV=production -e DB_URL=mongodb://localhost my-app
📦 Docker 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.
🔗 Docker Networks
Create a custom network:
docker network create my-network
List networks:
docker network ls
Types of Docker networks:
bridge: Default. Isolated internal network on the host.
host: Shares host's networking directly.
Run a container on a specific network:
docker run --name my-app --network my-network -p 3000:3000 my-image
🚀 Push Custom Images to Docker Hub
Step-by-step:
- 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
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 down everything:
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.
