Most of us learned Docker from a handful of copy-pasted commands, and they mostly work. But some of the everyday ones have defaults that quietly cost you time, hide errors, or even expose a service to the internet.
This guide covers five common Docker commands, the mistake people usually make with each one, and the better way to run it. Every example was tested on Docker Engine 29.3 in November 2026, and the same flags work on any recent Docker release.
The 5 Mistakes at a Glance
| Command | The common mistake | Do this instead |
|---|---|---|
docker logs | docker logs -f app prints the whole history, and | grep misses errors | docker logs -n 100 -f app and 2>&1 | grep |
docker exec / attach | Using attach to look inside, then pressing Ctrl-C | docker exec -it app sh; detach with Ctrl-P Ctrl-Q |
docker run -p | -p 8080:80 for a service that should be private | -p 127.0.0.1:8080:80 |
docker stop | Waiting 10 seconds every time, then switching to rm -f | Fix signal handling: exec form, --init |
docker system prune | prune -af --volumes in a cron job | Check docker system df, prune with filters |
1. docker logs: Stop Scrolling Through the Whole History
The mistake
When something breaks, the first reflex is:
docker logs -f app
--follow on its own first prints every line the container has ever logged, and only then starts following. On a container that has run for weeks, that’s thousands of lines flooding your terminal before you see anything new.
The better way
Ask for only the last lines, then follow:
docker logs -n 100 -f app
-n is the short form of --tail, and it needs a number. Two more flags make debugging much faster:
docker logs --since 10m appdocker logs -t --since 2026-11-27T09:00:00 --until 2026-11-27T09:15:00 app
--sinceand--untiltake a relative time (10m,2h) or a timestamp, so you can cut out exactly the window when the problem happened.-tadds a timestamp to every line, even if your app doesn’t print its own.
The hidden gotcha: grep misses your errors
docker logs sends what the container wrote to stderr to your terminal’s stderr. A pipe only carries stdout, so this command finds nothing when the app writes its errors to stderr (most do):
docker logs app | grep ERROR
The error lines still appear on screen, mixed with the output, so it looks as if grep is broken. Redirect stderr into the pipe:
docker logs app 2>&1 | grep ERROR
In our test, a container that printed INFO ok to stdout and ERROR db down to stderr returned no matches with the first command and the error line with the second. If your logs keep growing, set up automatic Docker log rotation so docker logs stays fast.
2. docker exec and attach: Get Inside Without Stopping the Container
The mistake
There are two ways people “go into” a running container, and both often go wrong.
The first is docker attach app. It doesn’t open a new shell: it connects your terminal to the container’s main process. If you press Ctrl-C there, the signal goes to that process, and the container stops. We tested it with a container running top: after Ctrl-C it showed Exited (0).
The second is docker exec app bash, which fails in two common ways:
OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $PATH
- Alpine-based and many slim images don’t have
bashat all. - Without
-it, the shell has no terminal and no input, so it exits straight away with no prompt and no error.
The better way
Use exec with -it to start a new shell next to the main process, and use sh when you’re not sure bash exists:
docker exec -it app sh
Typing exit or pressing Ctrl-C there only ends your shell; the container keeps running. A few flags save extra steps:
docker exec -it -u root app shdocker exec -w /var/www/html app ls -ladocker exec -e DEBUG=1 app php artisan queue:work --once
-uruns as another user, for example root in an image that normally runs as an unprivileged user.-wsets the working directory, so you don’t needcd.-esets an environment variable for that one command.
If you really do need attach, for example to answer a prompt from the main process, leave with Ctrl-P then Ctrl-Q. That detaches and leaves the container running, but it only works when the container was started with -it. More examples are in our docker exec guide.
3. docker run -p: Your Firewall Isn’t Protecting That Port
The mistake
You start a database or an admin panel for local use:
docker run -d --name db -p 5432:5432 postgres:18
Your server runs UFW and only allows SSH and HTTPS, so port 5432 should be closed. It isn’t.
A port published as -p 5432:5432 listens on every interface, IPv4 and IPv6:
docker port db
5432/tcp -> 0.0.0.0:5432
5432/tcp -> [::]:5432
Docker adds its own NAT rules that forward the traffic to the container before it reaches UFW’s input rules. Docker’s own documentation warns about this: traffic to published ports is diverted before it goes through the UFW firewall settings, so a UFW “deny” doesn’t apply to it. This is how many Redis, MongoDB and Elasticsearch servers end up open to the internet.
The better way
Bind the port to localhost when only the host (or a reverse proxy on it) needs to reach it:
docker run -d --name db -p 127.0.0.1:5432:5432 postgres:18
The same works in Docker Compose:
services:
db:
image: postgres:18
ports:
- "127.0.0.1:5432:5432"
Even better, if only other containers need the service, don’t publish the port at all and put them on the same Docker network. Other options:
- Make localhost the default for every
-pwithout an address by adding"ip": "127.0.0.1"to/etc/docker/daemon.jsonand restarting Docker. - Filter traffic to published ports in the
DOCKER-USERiptables chain, which Docker leaves for your own rules. See forwarding ports to Docker containers with Linux firewalls.
Check what’s really listening with sudo ss -ltnp: a published port shows up as docker-proxy on 0.0.0.0 or on 127.0.0.1.
4. docker stop: Why It Takes 10 Seconds (and Why rm -f Isn’t the Fix)
The mistake
docker stop app hangs for about 10 seconds every time. It’s annoying, so people switch to docker kill or docker rm -f. Those kill the process immediately, with no chance to finish requests, flush data to disk or close database connections.
What’s really happening
docker stop sends SIGTERM to the container’s main process (PID 1), waits 10 seconds, then sends SIGKILL. The process running as PID 1 in a container gets no default signal handling from the kernel: if it doesn’t handle SIGTERM itself, the signal is simply ignored. The same happens when a shell script is PID 1 and starts your app as a child process, because the app never receives the signal.
Our test container ran sleep 300 as PID 1:
| Setup | Time for docker stop | Exit code |
|---|---|---|
Plain docker run alpine sh -c 'sleep 300' | 10.1 s | 137 (killed by SIGKILL) |
Same with --init | 0.1 s | 143 (stopped by SIGTERM) |
Script with trap ... TERM | 0.1 s | 0 (clean exit) |
An exit code of 137 after docker stop is the sign that your container never shut down cleanly.
The better way
- Use the exec form in your Dockerfile, so your app is PID 1 and receives the signal:
CMD ["node", "server.js"]instead ofCMD node server.js. The shell form runs your command under/bin/sh -c. Our guide to ENTRYPOINT and CMD explains both forms. - End entrypoint scripts with exec, such as
exec "$@"orexec gunicorn app:app, so the script hands PID 1 over to your app. - Add a tiny init process when you can’t change the image. It forwards signals and cleans up zombie processes:
docker run -d --init --name app myimageIn Compose, set
init: trueon the service. - Give slow apps more time instead of less. A database that needs 30 seconds to flush to disk should get them:
docker stop -t 30 app, orstop_grace_period: 30sin Compose.
If your app expects a different signal to shut down, such as SIGQUIT for Nginx’s graceful stop, set it with STOPSIGNAL SIGQUIT in the Dockerfile or docker stop -s SIGQUIT app.
5. docker system prune: Free Disk Space Without Deleting What You Need
The mistake
The disk is full, so someone runs the “clean everything” version, or worse, puts it in a cron job:
docker system prune -af --volumes
Here’s what Docker 29 says that removes:
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all anonymous volumes not used by at least one container
- all images without at least one container associated to them
- all build cache
Each part can hurt:
- All stopped containers: including the one you stopped yesterday to debug, along with its logs and any data written inside it.
- All unused images (
-a): including the previous version you kept for a quick rollback, and images you can’t pull again easily. - Anonymous volumes (
--volumes): images like MySQL and PostgreSQL declare aVOLUMEfor their data directory. If you ran them without-v, the database lives in an anonymous volume, and once its container is gone, the next prune deletes it. - All build cache: your next build starts from scratch.
Named volumes are safe from this command: since Docker 23, --volumes only removes anonymous volumes. Deleting unused named volumes needs docker volume prune -a, which we confirmed on Docker 29.3.
The better way
First, see where the space actually goes:
docker system df
The RECLAIMABLE column shows what you would gain in each group. Often most of it is build cache or old images, and you can clean just that part:
docker system prune --filter "until=24h"docker image prune -a --filter "until=168h"docker builder prune --filter "until=72h"
- The plain
docker system prune, without-a, only removes dangling images (untagged layers left over from builds), not tagged images. until=24hskips anything created in the last 24 hours, so a container you just stopped survives.- The
untilfilter can’t be combined with--volumes: Docker answers withERROR: The "until" filter is not supported with "--volumes". Clean volumes as a separate, deliberate step. - Protect things with a label and skip them:
docker system prune --filter "label!=keep". - For build cache,
docker builder prune --max-used-space 10GBkeeps the newest 10 GB instead of wiping everything.
Before deleting any volume, back up your Docker volumes. For every prune option in detail, see docker system prune explained.
Quick Reference
docker logs -n 100 -f appdocker logs --since 10m app 2>&1 | grep -i errordocker exec -it app shdocker run -d -p 127.0.0.1:8080:80 --init --name app myimagedocker stop -t 30 appdocker system dfdocker system prune --filter "until=24h"
Frequently Asked Questions
What is the difference between docker exec and docker attach?
docker exec starts a new process, such as a shell, inside a running container. docker attach connects your terminal to the container’s existing main process, so Ctrl-C there can stop the container. Use exec for debugging.
Why does my container exit with code 137?
137 means the process was killed with SIGKILL. After docker stop, it means the app ignored SIGTERM for 10 seconds. It can also mean the container ran out of memory: check docker inspect -f '{{.State.OOMKilled}}' app. See CPU and memory limits in Docker.
Does docker system prune delete volumes?
Not by default. With --volumes it deletes anonymous volumes that no container uses. Named volumes are only deleted by docker volume prune -a (Docker 23 and later).
Is it safe to run docker system prune on a production server?
The plain command without -a or --volumes is usually safe: running containers, the images they use, tagged images and volumes are kept. Still check docker system df and docker ps -a first, and add --filter "until=24h" so recently stopped containers survive.
Conclusion
None of these commands is broken. They just have defaults that surprise people: logs -f starts at the beginning, pipes skip stderr, attach passes Ctrl-C to your app, -p opens a port on every interface, stop waits for a process that never got the signal, and prune -a --volumes removes more than most people expect. Change these five habits and you’ll debug faster and avoid the accidents that cost the most time. New to Docker? Start with installing Docker on Ubuntu 26.04, then try our advanced Docker tips for sysadmins.
How did it make you feel?
