Docker Commands in Labs: Pull, Run, Exec, and Container Management
The Working With Docker Command exercise introduces core Docker operations that appear across 5 courses. This article covers common failures with docker pull, docker run, docker exec, and container lifecycle management.
Table of Contents
- Container Exits Immediately
- Container Name Conflict
- Port Binding Must Use
-p 80:80, Not127.0.0.1:80:80 - Environment Variables with
-e docker execon Stopped Containers- Common Questions
Container Exits Immediately
No Foreground Process Means Instant Exit
If you run docker run -d --name myubuntu ubuntu without the -i flag, the container starts and immediately exits:
$ docker ps -a
CONTAINER ID IMAGE STATUS
abc123 ubuntu Exited (0) 3 seconds ago
This is expected Docker behavior: a container runs only as long as its main process (PID 1) is alive. The ubuntu image has no default foreground process.
Fix: Add -i (interactive/keep stdin open) to keep the container running:
docker run -d --name myubuntu -i ubuntu
Or run a long-lived command:
docker run -d --name myubuntu ubuntu sleep infinity
Container Name Conflict
Can't Reuse a Container Name
If you try to create a container with a name that already exists:
docker: Error response from daemon: Conflict. The container name "/myubuntu" is already in use by container "abc123". You have to remove (or rename) that container to be able to reuse that name.
Fix: Remove the existing container first:
docker rm -f myubuntu
docker run -d --name myubuntu -i ubuntu
The -f flag forces removal even if the container is running.
Port Binding Must Use -p 80:80, Not 127.0.0.1:80:80
Port Must Bind to All Interfaces
The exercise verification checks that port 80 is exposed on 0.0.0.0 (all interfaces). If you bind to localhost only, the verification fails:
# WRONG - binds to localhost only:
docker run -d --name webserver -p 127.0.0.1:80:80 nginx:1.21.3
# CORRECT - binds to all interfaces:
docker run -d --name webserver -p 80:80 nginx:1.21.3
Fix: Use -p 80:80 to bind to 0.0.0.0:80.
Verification Checks
The exercise verifies port binding via:
docker inspect webserver | jq '.[].NetworkSettings.Ports."80/tcp"[] | select(.HostIp=="0.0.0.0" and .HostPort=="80")'
Environment Variables with -e
Passing Environment Variables to Containers
Use -e to set environment variables:
docker run -d --name webserver -e APP=nginx nginx:1.21.3
Verify the environment variable was set:
docker inspect webserver | jq -r ".[].Config.Env" | grep "APP=nginx"
Common mistake: Forgetting -e or using --env-file when the exercise expects inline -e KEY=VALUE.
docker exec on Stopped Containers
Can't Exec Into an Exited Container
ERROR: Cannot execute in a non-running container
This happens when you try to docker exec into a container that has already exited.
Fix: Check if the container is running first:
docker ps | grep myubuntu
If it's not listed, restart it or recreate it with -i to keep it running:
docker rm -f myubuntu
docker run -d --name myubuntu -i ubuntu
docker exec -it myubuntu bash
Common Questions
| Question | Answer |
|---|---|
| My container shows "Exited (0)" immediately. | The image has no foreground process. Add -i or run a long-lived command like sleep infinity. |
docker run says the container name is already in use. |
Run docker rm -f <name> first, then create the container. |
docker exec says "Cannot execute in a non-running container." |
The container has stopped. Check docker ps and restart if needed. |
docker pull isn't needed before docker run. |
Correct. docker run automatically pulls the image if it's not present locally. |
| How do I see all containers, including stopped ones? | Use docker ps -a (the -a flag shows all containers). |
| How do I check what process is running inside a container? | Use docker top <container-name>. |
Wrap-Up
Docker container management boils down to keeping the main process alive, managing container names, binding ports correctly, and using the right flags. Key takeaways:
-ikeeps containers running when the image has no default foreground process.docker rm -fremoves containers so you can reuse names.-p 80:80binds to all interfaces —127.0.0.1:80:80will fail verification.-e KEY=VALUEsets environment variables.docker execonly works on running containers.
The DevSecOps Box is stateless — any Docker containers you create will be lost on page refresh.