Docker Images, Dockerfiles, and Building: Common Errors and Fixes
The Create Docker Image using Dockerfile and Dockerfile exercises teach how to build custom Docker images, write Dockerfiles, and manage image tags. These exercises appear across 4 courses. This article covers the most common build errors and Dockerfile pitfalls.
Table of Contents
- Missing Dot in
docker build - apt install Fails: No
apt update - Interactive Prompt Aborts the Build
- Multiple CMD Instructions
- ENTRYPOINT vs CMD
- Container Exits After Starting a Service
- Tagging and Removing Images
- Common Questions
Missing Dot in docker build
The Build Context
The dot (.) at the end of docker build specifies the build context — the directory whose contents are sent to the Docker daemon:
# WRONG - missing context:
docker build -t myimage:1.0
# CORRECT:
docker build -t myimage:1.0 .
Without the dot, Docker doesn't know where to find the Dockerfile and source files.
apt install Fails: No apt update
Package Not Found on First Install
If your Dockerfile runs apt install nginx without first updating the package index, the build fails:
E: Unable to locate package nginx
Fix: Always run apt update before apt install:
RUN apt update && apt install -y nginx
Interactive Prompt Aborts the Build
apt install Waits for User Confirmation
Even with apt update, apt install nginx without -y will prompt for confirmation, which Docker can't answer:
Do you want to continue? [Y/n] Abort.
Fix: Add -y to auto-confirm:
RUN apt update && apt install -y nginx
Progressive Debugging Pattern
The exercise teaches a progressive debugging approach:
- First attempt:
RUN apt install nginx→ FAILS (no apt update) - Second attempt:
RUN apt update && apt install nginx→ FAILS (interactive prompt) - Final fix:
RUN apt update && apt install -y nginx→ SUCCEEDS
Multiple CMD Instructions
Only the Last CMD Survives
If a Dockerfile has multiple CMD instructions, only the last one takes effect:
CMD ["/bin/bash", "-c", "service nginx start"]
CMD ["sleep infinity"]
The first CMD is silently ignored. If the second CMD is wrong, the container will fail:
exec: ";sleep infinity": executable file not found in $PATH
Fix: Combine into a single CMD:
CMD ["/bin/bash", "-c", "service nginx start; sleep infinity"]
ENTRYPOINT vs CMD
ENTRYPOINT Sets the Command, CMD Provides Arguments
| Instruction | Purpose | Override Behavior |
|---|---|---|
ENTRYPOINT |
Sets the main command | Hard to override — requires --entrypoint |
CMD |
Provides default arguments | Easily overridden by CLI args |
Example:
ENTRYPOINT ["/bin/bash", "-c"]
CMD ["service nginx start"]
Running docker run myimage executes service nginx start.
Running docker run myimage sleep 10 overrides CMD to sleep 10.
Key concept: Use ENTRYPOINT for the command that shouldn't change, and CMD for defaults that can be overridden.
Overriding ENTRYPOINT
To override the entrypoint, use --entrypoint:
docker run --name webserver -it --entrypoint /bin/bash django.nv:1.0
This is useful when you need to debug a container or run a different command than what the image was designed for.
Container Exits After Starting a Service
service nginx start Doesn't Keep the Container Alive
CMD ["/bin/bash", "-c", "service nginx start"]
This starts nginx in the background, but the CMD process (bash running the service command) exits immediately, causing the container to stop:
$ docker ps -a
CONTAINER ID IMAGE STATUS
abc123 custom-nginx Exited (0) 3 seconds ago
Fix: Keep the foreground process alive:
CMD ["/bin/bash", "-c", "service nginx start; sleep infinity"]
Tagging and Removing Images <a name="tag-remove>
Renaming an Image with docker tag
docker tag django.nv:1.0 django.nv:1.1
This creates a new tag pointing to the same image. The original django.nv:1.0 still exists.
Removing an Image
docker rmi django.nv:1.0
Common error: If you're inside a Docker container (e.g., after docker run -it), docker command may not be available:
bash: docker: command not found
Fix: Exit the container first with exit, then run docker rmi from the host shell.
Common Questions
| Question | Answer |
|---|---|
My docker build fails with "Unable to locate package." |
Add apt update && before apt install. |
| My build hangs at "Do you want to continue?" | Add -y flag: apt install -y <package>. |
| My container exits immediately after starting a service. | Add sleep infinity or another foreground process to keep the container alive. |
| I have multiple CMD lines in my Dockerfile. | Only the last CMD takes effect. Combine them into one. |
docker rmi says "docker: command not found." |
You're inside a container. Exit first, then run the command on the host. |
| What's the difference between ENTRYPOINT and CMD? | ENTRYPOINT sets the main command (hard to override). CMD provides default arguments (easily overridden). |
Wrap-Up
Dockerfile authoring follows a few key patterns that prevent most build failures:
- Always run
apt update && apt install -y(the-yis critical). - Don't use multiple CMD instructions — only the last one matters.
- Keep the container alive with a foreground process (
sleep infinity,nginx -g 'daemon off;', etc.). - The dot (
.) indocker buildis the build context — don't forget it. - ENTRYPOINT + CMD pattern: ENTRYPOINT for the fixed command, CMD for overridable defaults.
The DevSecOps Box is stateless — all Docker images and containers are lost on page refresh.