Everyday container commands
Logs, exec, inspect, and the debugging workflow you'll use on every container you ever run.
The commands you'll type a hundred times a day
Running a container is the easy part. The commands in this lesson are the ones you actually live in day to day — checking why something isn't working, looking inside a running container, or getting a shell to poke around.
Viewing logs
Whatever a containerized process writes to standard output/error is captured by Docker as that container's logs — you don't need to configure logging separately for this to work.
docker logs my-web # print all logs since the container started
docker logs -f my-web # follow, like `tail -f` — stream new lines live
docker logs --tail 50 my-web # only the last 50 lines
docker logs -f is usually the very first command to reach for when a container isn't behaving — it's the container's equivalent of checking the console output of a normal program.
Getting a shell inside a running container
Sometimes logs aren't enough and you need to look around inside the container itself — check if a file exists, test a network call from inside its namespace, or inspect an environment variable.docker exec runs a new command inside an already running container:
docker exec -it my-web /bin/bash
-i(interactive) keeps STDIN open so you can type into it.-t(tty) allocates a pseudo-terminal so it looks and behaves like a normal shell.- Together,
-itis what makes this feel like SSH-ing into the container.
bash — many minimal images (especially ones based on Alpine Linux) only have sh:
docker exec -it my-web /bin/sh
docker logs and docker inspect are your only windows into it.
Inspecting a container's full configuration
docker inspect dumps a container's complete configuration as JSON — its IP address, mounted volumes, environment variables, resource limits, restart policy, everything:
docker inspect my-web
--format with Go template syntax to extract just it:
docker inspect --format '{{.NetworkSettings.IPAddress}}' my-web
docker inspect --format '{{.State.Status}}' my-web
docker inspect --format '{{.HostConfig.Memory}}' my-web
Checking resource usage live
docker stats shows a live, continuously updating view of CPU, memory, and network usage per container — the container equivalent of top:
docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT NET I/O
7f2a9c1d4e3b my-web 0.12% 4.2MiB / 512MiB 1.2kB / 0B
This is the fastest way to confirm whether a cgroup memory limit is about to trigger an OOM kill, or whether a container is quietly spinning your CPU.
Copying files in and out
docker cp moves files between the host and a container's filesystem, in either direction, without needing a shell inside the container:
docker cp my-web:/app/config.json ./config.json # container → host
docker cp ./config.json my-web:/app/config.json # host → container
Handy for pulling out a log file or a generated report without setting up a shared volume (covered in the next module) just for a one-off copy.
Cleaning up
Docker accumulates stopped containers, unused images, and dangling data over time. A few cleanup commands worth knowing before things pile up:
docker container prune # remove all stopped containers
docker image prune # remove unused (dangling) images
docker system prune # remove stopped containers, unused networks, and dangling images together
docker system prune -a # also remove images not used by any container at all — more aggressive
Putting it together: a realistic debugging flow
A typical "why isn't this working" sequence looks like:
docker ps -a # is the container even running?
docker logs my-web # what did it print before dying/erroring?
docker exec -it my-web /bin/sh # look around inside, if it's still running
docker inspect --format '{{.State.ExitCode}}' my-web # what exit code did it stop with?
logs, exec, and inspect, you can diagnose the overwhelming majority of container problems without needing any external tooling.
What's next
Every image so far in this course has been pulled from Docker Hub. The next lesson writes your firstDockerfile — the recipe for building your own images.