⚡ Pro Tips
- Order Dockerfile layers from least-to-most frequently changed. Copy
package.jsonandRUN npm installbeforeCOPY . .so the npm layer stays cached on source changes. - Use
.dockerignoreto excludenode_modules,.git,*.log, and.envfiles from the build context — speeds up builds and keeps secrets out of images. - Run containers as non-root: create a user in the Dockerfile (
RUN adduser --disabled-password app && USER app) to limit the blast radius of a container escape. - Use
docker run --rmfor one-off tasks (migrations, scripts, debugging). The container cleans itself up automatically. - Name your containers (
--name) and services in Compose: containers on the same network reach each other by service name as hostname. No IPs needed. - Tag images semantically: push both
image:1.2.3andimage:latest. Use the version tag in production deployments, never:latest, to ensure reproducibility. - Use
docker system dfregularly. Unused images and build cache can quietly consume 20–50 GB on active development machines. - In Docker Compose,
depends_ononly waits for the container to start, not for the app inside to be ready. Use a health check or a wait script for services that need database connectivity at startup.
Frequently Asked Questions
How do I see all running Docker containers?
Use docker ps to list all currently running containers. Add the -a flag (docker ps -a) to see all containers including stopped ones. The output shows container ID, image, command, creation time, status, ports, and name. Use docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" to customize the output columns.
How do I run a Docker container in the background?
Use the -d (detach) flag: docker run -d image:tag. This starts the container in the background and prints the container ID. Use docker logs container_id to view its output later, or docker logs -f container_id to follow the logs in real time. Use docker attach container_id to reconnect to the container's main process.
How do I remove all stopped Docker containers?
Run docker container prune to remove all stopped containers with a confirmation prompt. Alternatively use docker rm $(docker ps -aq -f status=exited) to remove only exited containers. To force-remove all containers (including running ones) use docker rm -f $(docker ps -aq). Add --volumes to also remove associated anonymous volumes.
How do I access a running container's shell?
Use docker exec -it container_name /bin/bash (or /bin/sh for Alpine-based images). The -i flag keeps stdin open and -t allocates a pseudo-TTY. You can also run any one-off command: docker exec container_name env to list environment variables, or docker exec container_name cat /etc/hosts to read a file. If the container doesn't have bash, try sh, ash, or install bash with apt-get install bash or apk add bash.
What is the difference between CMD and ENTRYPOINT in Dockerfile?
ENTRYPOINT defines the executable that always runs when a container starts. CMD provides default arguments that can be overridden at docker run time. Best practice: use ENTRYPOINT ["node"] + CMD ["app.js"] — this runs node app.js by default, but docker run image server.js would run node server.js. If you only use CMD, the entire command can be replaced at runtime. Use exec form (["node", "app.js"]) not shell form (node app.js) to ensure signals are forwarded correctly.
How do I rebuild a Docker image without cache?
Use docker build --no-cache -t image:tag . to rebuild without using any cached layers. This forces re-downloading dependencies and re-running all RUN commands. In Docker Compose: docker compose build --no-cache. To selectively bust the cache for a specific RUN command in your Dockerfile, add ARG CACHEBUST before it and pass --build-arg CACHEBUST=$(date +%s) at build time.
How do I view Docker container logs?
Use docker logs container_name to print all logs. Options: -f to follow in real time, --tail 100 for the last 100 lines, --since 1h for logs from the last hour, --until 2026-05-29T12:00:00 for logs up to a time. For Docker Compose: docker compose logs -f web follows logs for just the 'web' service. View logs since a timestamp with docker logs --since "2026-05-29T10:00:00" container.
How do I clean up unused Docker resources?
Use docker system prune to remove stopped containers, unused networks, dangling images, and build cache. Add -a to also remove unused images (not just dangling): docker system prune -a. Add --volumes to include anonymous volumes. Check disk usage first with docker system df. On a development machine this can recover 5–50 GB. Individual pruning: docker image prune -a, docker container prune, docker volume prune, docker network prune.