Table of Contents
These seven Docker command groups cover starting a stack, inspecting containers, reading logs, building images, and reclaiming disk space. Use them in a development environment where you understand what each container stores. Replace myapp with your container's actual name or ID.
1. Start and stop a project with Docker Compose
Run these commands from the directory containing your Compose file, such as compose.yaml or docker-compose.yml:
docker compose up -d
docker compose psThe first creates and starts the project's services in the background; the second shows their status. A running container is not necessarily a healthy application, so also check its logs and application endpoint. For an example, see a PicoShare Compose deployment.
docker compose downThis stops and removes the project's containers and its managed networks. Preserve important data outside container writable layers, and do not add volume-removal options unless you intend to delete that data.
2. Open a shell in a running container
docker exec -it myapp bashIf the image provides sh but not Bash, use:
docker exec -it myapp shThe container must be running and the requested shell must exist. Minimal images may contain neither. Use this to inspect files and configuration; lasting fixes belong in the image or deployment configuration rather than an undocumented change inside a container.
3. Read recent logs and follow new output
docker logs --tail 50 -f myappThis prints recent log output and follows new messages. Press Ctrl+C to stop following without stopping the container. Omit -f for a one-time view.
Logs can reveal startup errors, but Docker logs do not necessarily include application logs written only to files. Treat log output as potentially sensitive before sharing it.
4. Build and tag an image
docker build -t myapp:v1 .The final dot selects the current directory as the build context. The -t option names and tags the resulting image, making it easier to identify and run.
A tag can be reassigned, so it does not by itself make a build reproducible or eliminate old images. Record build inputs and use a suitable tagging policy. See the Docker learning roadmap for image and Dockerfile fundamentals.
5. List containers before removing one
docker ps -aThis includes running and stopped containers. Review the status, name, and published ports. docker ps -q returns only running container IDs; docker ps -aq includes stopped ones.
docker rm myappUse this only for a specific stopped container you no longer need. Removing it discards its writable layer. Copy or back up necessary data first; avoid piping a list of every container into a removal command.
6. Inspect configuration and state
docker inspect myapp
docker inspect --format='{{json .NetworkSettings.Networks}}' myappThe first returns detailed JSON; the second narrows output to network information, including addresses for attached networks. Inspection can help compare actual mounts, ports, and configuration with what you expected. Output may contain secrets in environment variables, so redact it before sharing.
7. Review disk use before pruning
docker system df
docker system pruneReview what you need to retain before confirming prune. By default, it removes stopped containers, unused networks, dangling images, and unused build cache. It does not retain every image.
docker system prune -a also removes images not associated with a container. Adding --volumes makes unused anonymous volumes eligible for deletion; it is not a harmless cleanup option. Back up required data, read the confirmation prompt, and check the Docker prune reference for your installed version.
Reader Comments 0
Sign in with email or Google to join the discussion.