DCubes
Docker module

Lesson 5

Look inside

docker exec for a shell in a running container, and inspect for the JSON facts.

beginner25 minRun on your machine

What you will be able to do

  • Open a shell in a running container with docker exec
  • Print a container’s state and mounts with inspect
  • Exit without stopping the container

Why this matters for DE and AI

The extract is “running” but writing nowhere useful. docker logs shows the app. docker exec is you standing on that machine: ls the output directory, df, env. inspect is the engine’s record: mounts, env, IP, restart count. Together they replace a lot of guesswork.

Concepts

docker exec runs a new process in an already running container. It is not docker run (that would start a second container).

docker exec -it NAME sh

Alpine has sh, not bash. Debian-based images often have bash. If exec says the container is not running, there is nothing to enter — start it, or you are too late.

exit from exec leaves the container running. That is the point. docker stop is how you end the main process.

docker inspect NAME prints JSON. It is noisy. Format strings pick one field:

docker inspect -f '{{.State.Status}}' NAME

You will use inspect for Mounts (did my CSV actually attach?) and Env (did POSTGRES_PASSWORD land?).

Practice on your machine

cd ~/dcubes/docker-lab
docker run -d --name dcubes-box alpine:3.20 sleep 600

Confirm it is up, then enter it:

docker ps --filter name=dcubes-box
docker exec -it dcubes-box sh

Inside, you should get a prompt. Run:

hostname
cat /etc/os-release
ps
exit

What you should see: a hostname that is not your laptop’s (often the short container ID), Alpine in os-release, a short ps list that includes sleep. After exit, the host prompt returns.

The container should still be running:

docker ps --filter name=dcubes-box

Inspect a few facts without drowning in JSON:

docker inspect -f '{{.State.Status}}' dcubes-box
docker inspect -f '{{.Config.Image}}' dcubes-box
docker inspect -f '{{.Config.Cmd}}' dcubes-box

What you should see: running, alpine:3.20, and something like [sleep 600].

Run a one-off command without an interactive shell:

docker exec dcubes-box ls /tmp

Empty listing is fine. The command ran inside dcubes-box.

Clean up:

docker stop dcubes-box
docker rm dcubes-box

Common mistakes

  • exec on an exited container. Start a new one, or docker start the old one first.
  • exit panic. Exiting exec does not kill Postgres. Check docker ps.
  • Installing packages with exec and calling it a new image. That is a snowflake container. Next lesson is tags; Dockerfiles come after that.

Next

Images and tags — Hub, pull, tags, and why latest is a trap.