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
$ 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.3Common Flags
| Flag | Purpose |
|---|---|
-f, --follow | Follow log output (stream) |
--tail N | Show last N lines |
--since | Show logs since time (e.g., 1h, 10m) |
--until | Show logs until time |
-t, --timestamps | Show timestamps |
--details | Show extra details |
Examples
Stream logs in real-time:
$ docker logs -f web-serverShow last 50 lines:
$ docker logs --tail 50 web-serverShow logs from last hour:
$ docker logs --since 1h web-serverShow logs with timestamps:
$ docker logs -t web-serverShow logs with details:
$ docker logs --details web-serverdocker exec — Execute Commands in Container
Runs a command in a running container.
Basic Usage
$ 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 .dockerenvCommon Flags
| Flag | Purpose |
|---|---|
-it | Interactive terminal |
-d, --detach | Run in background |
-e, --env | Set environment variable |
-w, --workdir | Working directory |
-u, --user | Username or UID |
Examples
Get an interactive shell:
$ docker exec -it web-server /bin/bash
root@abc123:/#Run as a specific user:
$ docker exec -u www-data web-server whoami
www-dataRun with environment variable:
$ docker exec -e ENV=production web-server envRun in background:
$ docker exec -d web-server touch /tmp/background.txtRun with working directory:
$ docker exec -w /var/log web-server pwd
/var/logRun multiple commands:
$ docker exec web-server sh -c "echo hello && echo world"
hello
worlddocker cp — Copy Files To/From Container
Copies files or directories between host and container.
Basic Usage
Copy from container to host:
$ docker cp web-server:/var/log/nginx/access.log ./access.logCopy from host to container:
$ docker cp ./config.json web-server:/etc/config.jsonSyntax
$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
$ docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATHExamples
Copy directory from container to host:
$ docker cp web-server:/var/log/nginx/ ./logs/Copy directory from host to container:
$ docker cp ./static/ web-server:/usr/share/nginx/html/Copy with specific container:
$ docker cp app-container:/app/data/ ./data/docker commit — Create Image from Container
Creates a new image from a container's changes.
Basic Usage
$ docker commit web-server my-nginx:v1
sha256:abc123def456...Common Flags
| Flag | Purpose |
|---|---|
-a, --author | Author of the image |
-m, --message | Commit message |
-p, --pause | Pause container during commit (default true) |
Examples
Commit with message and author:
$ docker commit -a "John Doe" -m "Added custom config" web-server my-nginx:v1Commit without pausing:
$ docker commit -p false web-server my-nginx:v1docker diff — Show Container Changes
Shows changes to files and directories in a container.
Basic Usage
$ 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.logChange Types
| Symbol | Meaning |
|---|---|
A | Added |
C | Changed |
D | Deleted |
docker export — Export Container Filesystem
Exports a container's filesystem as a tar archive.
Basic Usage
$ docker export web-server > container.tarCommon Flags
| Flag | Purpose |
|---|---|
-o, --output | Output to a file instead of STDOUT |
Examples
Export to file:
$ docker export -o container.tar web-serverExport and compress:
$ docker export web-server | gzip > container.tar.gzdocker import — Import Filesystem as Image
Imports a tarball as a Docker image.
Basic Usage
$ docker import container.tar my-image:latest
sha256:def456789abc...Common Flags
| Flag | Purpose |
|---|---|
-m, --message | Commit message |
-c, --change | Apply Dockerfile instructions |
Examples
Import with message:
$ docker import -m "Imported from tarball" container.tar my-image:latestImport with Dockerfile instructions:
$ docker import -c "CMD ['nginx', '-g', 'daemon off;']" container.tar my-nginx:latestImport from URL:
$ docker import https://example.com/container.tar my-image:latestQuick Reference — Most Used Commands
| Task | Command |
|---|---|
| View logs | docker logs web-server |
| Stream logs | docker logs -f web-server |
| Last 50 log lines | docker logs --tail 50 web-server |
| Execute command | docker exec web-server ls -la |
| Interactive shell | docker exec -it web-server /bin/bash |
| Copy from container | docker cp web-server:/path/file ./file |
| Copy to container | docker cp ./file web-server:/path/file |
| Commit container | docker commit web-server myimage:v1 |
| Show container changes | docker diff web-server |
| Export container | docker export -o container.tar web-server |
| Import tarball | docker import container.tar myimage:latest |
Pro Tips
-
Use
docker logs -ffor debugging running containers. -
docker execrequires a running container. -
For large file copies, mount a volume instead of
docker cp. -
docker commitis useful for debugging but not for production images. -
Use
docker diffto see what changed after running commands. -
docker exportvsdocker save—exportexports container filesystem,saveexports image layers. -
docker importcreates a single-layer image — not ideal for production. -
Use
-itwithexecfor interactive commands likebash.