Skip to main content

docker containers cheatsheet

1. List Containers

docker ps

• Lists all running containers.

Flags:

• -a: Show all containers (running, stopped, and exited).

• -q: Show only container IDs.

• --filter: Filter containers (e.g., --filter status=exited).

2. Run a Container

docker run <options> <image-name>

• Starts a new container from an image.

Common Options:

• -d: Run in detached mode (background).

-p <host-port>:<container-port>: Map ports.

--name <container-name>: Assign a name to the container.

-e <VAR>=<value>: Set environment variables.

-v <host-path>:<container-path>: Mount a volume.

Example:

docker run -d -p 8080:80 --name my-web nginx

3. Start and Stop a Container

Start a Stopped Container:

docker start <container-id-or-name>

Stop a Running Container:

docker stop <container-id-or-name>

4. Restart a Container

docker restart <container-id-or-name>

• Stops and starts a container.

5. Remove a Container

docker rm <container-id-or-name>

• Removes a stopped container.

Force Removal:

docker rm -f <container-id-or-name>

6. View Logs of a Container

docker logs <container-id-or-name>

• Displays logs from a container.

Follow Logs in Real-Time:

docker logs -f <container-id-or-name>

7. Execute Commands in a Running Container

docker exec <options> <container-id-or-name> <command>

• Runs a command inside a running container.

Example:

docker exec -it my-container /bin/bash

• -i: Interactive mode.

• -t: Allocates a pseudo-TTY.

8. Inspect a Container

docker inspect <container-id-or-name>

• Displays detailed information about a container, including its configuration and state.

9. Stop All Running Containers

docker stop $(docker ps -q)

10. Remove All Stopped Containers

docker container prune

• Prompts to remove all stopped containers.

Skip Confirmation:

docker container prune -f

11. View Resource Usage

docker stats

• Displays real-time CPU, memory, and network usage for running containers.

12. Rename a Container

docker rename <old-name> <new-name>

13. Pause and Unpause a Container

Pause:

docker pause <container-id-or-name>

Freezes all processes in the container.

Unpause:

docker unpause <container-id-or-name>

14. Attach to a Running Container

docker attach <container-id-or-name>

• Connects your terminal to a running container’s standard input, output, and error.

15. Export a Container’s Filesystem

docker export <container-id-or-name> > <filename>.tar

• Saves the filesystem of a container as a tar archive.

16. Import a Filesystem into Docker

cat <filename>.tar | docker import - <new-image-name>

17. Copy Files to/from a Container

Copy Files to Container:

docker cp <host-path> <container-id-or-name>:<container-path>

Copy Files from Container:

docker cp <container-id-or-name>:<container-path> <host-path>

Common Workflows

Run and Interact with a Container

1. Run a container:

docker run -it ubuntu

2. Inside the container, interact with the terminal.

3. Exit:

exit

Run a Background Container and Check Logs

1. Run:

docker run -d nginx

2. View logs:

docker logs <container-id>

Remove All Containers

docker rm $(docker ps -a -q)