Link copied to clipboard
Docker

Docker Cheatsheet: Container Commands & Tips

Essential Docker commands for container management, images, volumes, and networking. Quick reference for developers.

·10 min read·
Docker Cheatsheet: Container Commands & Tips

Docker Cheatsheet

Essential Docker commands for container management, images, volumes, and networking.

Useful Docker Resources


Container Lifecycle

docker run <image>

Create and start a container from an image

docker run -d <image>

Run container in detached (background) mode

docker run -it <image> /bin/bash

Run container interactively with a shell

docker run -p 8080:80 <image>

Map host port 8080 to container port 80

docker run -v /host/path:/container/path <image>

Mount a volume from host to container

docker run --name my-container <image>

Run container with a custom name

docker start <container>

Start a stopped container

docker stop <container>

Stop a running container

docker restart <container>

Restart a container

docker rm <container>

Remove a stopped container

docker rm -f <container>

Force remove a running container


Container Information

docker ps

List running containers

docker ps -a

List all containers (including stopped)

docker logs <container>

View container logs

docker logs -f <container>

Follow container logs in real-time

docker exec -it <container> /bin/bash

Execute a command inside a running container

docker inspect <container>

Get detailed container information

docker top <container>

Show running processes in a container

docker stats

Display live resource usage statistics


Image Management

docker images

List all local images

docker pull <image>

Download an image from registry

docker push <image>

Upload an image to registry

docker build -t <name:tag> .

Build an image from Dockerfile

docker tag <image> <new-name:tag>

Tag an image with a new name

docker rmi <image>

Remove an image

docker image prune

Remove unused images

docker history <image>

Show image layer history


Volume Management

docker volume create <name>

Create a named volume

docker volume ls

List all volumes

docker volume inspect <name>

Get volume details

docker volume rm <name>

Remove a volume

docker volume prune

Remove all unused volumes


Network Management

docker network ls

List all networks

docker network create <name>

Create a network

docker network connect <network> <container>

Connect container to network

docker network disconnect <network> <container>

Disconnect container from network

docker network inspect <network>

Get network details

docker network rm <network>

Remove a network


Docker Compose

docker-compose up

Create and start all services

docker-compose up -d

Start services in detached mode

docker-compose down

Stop and remove all services

docker-compose build

Build or rebuild services

docker-compose logs -f

Follow logs for all services

docker-compose ps

List running services

docker-compose exec <service> <command>

Execute command in a service container

docker-compose restart

Restart all services


System & Cleanup

docker system df

Show Docker disk usage

docker system prune

Remove unused data (containers, networks, images)

docker system prune -a

Remove all unused data including unused images

docker info

Display system-wide information

docker version

Show Docker version information


Dockerfile Instructions

FROM <image>

Set base image

WORKDIR /app

Set working directory

COPY . .

Copy files from host to container

RUN npm install

Execute command during build

EXPOSE 3000

Document exposed port

ENV NODE_ENV=production

Set environment variable

CMD ["node", "server.js"]

Default command to run

ENTRYPOINT ["python", "app.py"]

Set container entrypoint

More articles