Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

7 Docker Commands for Managing and Troubleshooting Containers

Use Compose, exec, logs, build, ps, inspect, and prune with practical examples and checks that help prevent accidental container or data removal.

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

Memorize these 7 commands to stop messing around with Docker containers. Picture 1

Run these commands from the directory containing your Compose file, such as compose.yaml or docker-compose.yml:

docker compose up -d
docker compose ps

The 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 down

This 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

Memorize these 7 commands to stop messing around with Docker containers. Picture 2

docker exec -it myapp bash

If the image provides sh but not Bash, use:

docker exec -it myapp sh

The 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

Memorize these 7 commands to stop messing around with Docker containers. Picture 3

docker logs --tail 50 -f myapp

This 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

Memorize these 7 commands to stop messing around with Docker containers. Picture 4

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

Memorize these 7 commands to stop messing around with Docker containers. Picture 5

docker ps -a

This 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 myapp

Use 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

Memorize these 7 commands to stop messing around with Docker containers. Picture 6

docker inspect myapp
docker inspect --format='{{json .NetworkSettings.Networks}}' myapp

The 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

Memorize these 7 commands to stop messing around with Docker containers. Picture 7

docker system df
docker system prune

Review 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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.