New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsDockerDocker
FreeSheet 04 of 11

Container Management

A comprehensive developer cheatsheet covering advanced Docker container operations, featuring essential CLI commands and workflows for viewing logs, executing commands, copying files, tracking filesystem changes, and exporting or importing containers.

docker logs — View Container Logs

Fetches logs from a container.

Basic Usage

bash
$ docker logs web-server
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2026/01/15 10:30:15 [notice] 1#1: using the "epoll" event method
2026/01/15 10:30:15 [notice] 1#1: nginx/1.25.3

Common Flags

FlagPurpose
-f, --followFollow log output (stream)
--tail NShow last N lines
--sinceShow logs since time (e.g., 1h, 10m)
--untilShow logs until time
-t, --timestampsShow timestamps
--detailsShow extra details

Examples

Stream logs in real-time:

bash
$ docker logs -f web-server

Show last 50 lines:

bash
$ docker logs --tail 50 web-server

Show logs from last hour:

bash
$ docker logs --since 1h web-server

Show logs with timestamps:

bash
$ docker logs -t web-server

Show logs with details:

bash
$ docker logs --details web-server

docker exec — Execute Commands in Container

Runs a command in a running container.

Basic Usage

bash
$ docker exec web-server ls -la
total 72
drwxr-xr-x   1 root root 4096 Jan 15 10:30 .
drwxr-xr-x   1 root root 4096 Jan 15 10:30 ..
-rwxr-xr-x   1 root root    0 Jan 15 10:30 .dockerenv

Common Flags

FlagPurpose
-itInteractive terminal
-d, --detachRun in background
-e, --envSet environment variable
-w, --workdirWorking directory
-u, --userUsername or UID

Examples

Get an interactive shell:

bash
$ docker exec -it web-server /bin/bash
root@abc123:/#

Run as a specific user:

bash
$ docker exec -u www-data web-server whoami
www-data

Run with environment variable:

bash
$ docker exec -e ENV=production web-server env

Run in background:

bash
$ docker exec -d web-server touch /tmp/background.txt

Run with working directory:

bash
$ docker exec -w /var/log web-server pwd
/var/log

Run multiple commands:

bash
$ docker exec web-server sh -c "echo hello && echo world"
hello
world

docker cp — Copy Files To/From Container

Copies files or directories between host and container.

Basic Usage

Copy from container to host:

bash
$ docker cp web-server:/var/log/nginx/access.log ./access.log

Copy from host to container:

bash
$ docker cp ./config.json web-server:/etc/config.json

Syntax

bash
$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
$ docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Examples

Copy directory from container to host:

bash
$ docker cp web-server:/var/log/nginx/ ./logs/

Copy directory from host to container:

bash
$ docker cp ./static/ web-server:/usr/share/nginx/html/

Copy with specific container:

bash
$ docker cp app-container:/app/data/ ./data/

docker commit — Create Image from Container

Creates a new image from a container's changes.

Basic Usage

bash
$ docker commit web-server my-nginx:v1
sha256:abc123def456...

Common Flags

FlagPurpose
-a, --authorAuthor of the image
-m, --messageCommit message
-p, --pausePause container during commit (default true)

Examples

Commit with message and author:

bash
$ docker commit -a "John Doe" -m "Added custom config" web-server my-nginx:v1

Commit without pausing:

bash
$ docker commit -p false web-server my-nginx:v1

docker diff — Show Container Changes

Shows changes to files and directories in a container.

Basic Usage

bash
$ docker diff web-server
C /var
C /var/cache
A /var/cache/nginx
C /var/log
A /var/log/nginx
A /var/log/nginx/access.log
A /var/log/nginx/error.log

Change Types

SymbolMeaning
AAdded
CChanged
DDeleted

docker export — Export Container Filesystem

Exports a container's filesystem as a tar archive.

Basic Usage

bash
$ docker export web-server > container.tar

Common Flags

FlagPurpose
-o, --outputOutput to a file instead of STDOUT

Examples

Export to file:

bash
$ docker export -o container.tar web-server

Export and compress:

bash
$ docker export web-server | gzip > container.tar.gz

docker import — Import Filesystem as Image

Imports a tarball as a Docker image.

Basic Usage

bash
$ docker import container.tar my-image:latest
sha256:def456789abc...

Common Flags

FlagPurpose
-m, --messageCommit message
-c, --changeApply Dockerfile instructions

Examples

Import with message:

bash
$ docker import -m "Imported from tarball" container.tar my-image:latest

Import with Dockerfile instructions:

bash
$ docker import -c "CMD ['nginx', '-g', 'daemon off;']" container.tar my-nginx:latest

Import from URL:

bash
$ docker import https://example.com/container.tar my-image:latest

Quick Reference — Most Used Commands

TaskCommand
View logsdocker logs web-server
Stream logsdocker logs -f web-server
Last 50 log linesdocker logs --tail 50 web-server
Execute commanddocker exec web-server ls -la
Interactive shelldocker exec -it web-server /bin/bash
Copy from containerdocker cp web-server:/path/file ./file
Copy to containerdocker cp ./file web-server:/path/file
Commit containerdocker commit web-server myimage:v1
Show container changesdocker diff web-server
Export containerdocker export -o container.tar web-server
Import tarballdocker import container.tar myimage:latest

Pro Tips

  1. Use docker logs -f for debugging running containers.

  2. docker exec requires a running container.

  3. For large file copies, mount a volume instead of docker cp.

  4. docker commit is useful for debugging but not for production images.

  5. Use docker diff to see what changed after running commands.

  6. docker export vs docker save — export exports container filesystem, save exports image layers.

  7. docker import creates a single-layer image — not ideal for production.

  8. Use -it with exec for interactive commands like bash.


Next in DockerManaging Container Lifecycle Pro
Docker cheat sheet: Container Management | InfraThrone DevOps Park