Sub topic 3
Starting from a local image
You can only push an image that exists on your machine. This chapter covers building a small image, confirming it is in the local image store, and understanding what the push will later work from. It assumes Docker is installed and running, you can use a terminal, and you have written a basic Dockerfile before.
Why the local image comes first
docker push does not build anything. It uploads an image that is already in your local image store, so a missing or misnamed image is the most common reason a first push fails before it starts. Building and checking the image first gives you a known starting point for the tagging and login steps in the next chapters.
Create a minimal Dockerfile
Make an empty directory and add a file named Dockerfile with this content:
FROM alpine:3.20
CMD ["echo", "Hello from Docker Hub"]FROM alpine:3.20 starts from a small Linux base image. CMD sets the command that runs when a container starts. Alpine is a good choice here because the base image is only a few megabytes, so the later upload is quick.
Build and list the image
Run the build from the directory that holds the Dockerfile. The final . is the build context, the directory sent to Docker for the build.
docker build -t myapp:1.0 .
docker images myappThe -t flag gives the image a name and tag in the form name:tag. Here the name is myapp and the tag is 1.0. The docker images myapp command lists local images whose repository name is myapp. You should see one row with the tag 1.0, an image ID, and a size.
| Column | Meaning |
|---|---|
| REPOSITORY | The name part, here `myapp` |
| TAG | The label after the colon, here `1.0` |
| IMAGE ID | A short form of the image's unique ID |
| SIZE | The disk size of the image |
Check that it runs
Before uploading, run the image once. A broken image is easier to fix now than after it is published.
docker run --rm myapp:1.0The --rm flag deletes the container when it exits, so no stopped container is left behind. The output should be Hello from Docker Hub.
A local name is not yet a Docker Hub name
The image is currently called myapp:1.0. That name has no namespace, and Docker Hub needs one. If you tried to push myapp:1.0 now, Docker would treat it as belonging to the official library namespace, which you cannot write to. The push would be refused with an access error.
The next chapters fix this. You will log in, then add a second name of the form YOUR_USERNAME/myapp:1.0 with docker tag. Tagging does not copy the image. Both names point to the same image ID.
Common problems at this stage
docker: command not foundor "Cannot connect to the Docker daemon". Docker is not installed, or the daemon is not running. Start Docker Desktop or the Docker service and retry.- "no such file or directory" for the Dockerfile. You ran
docker buildin the wrong directory. Check withlsthatDockerfileis in the current directory, with that exact capitalization. docker images myappshows nothing. The build failed, or you used a different name with-t. Rundocker imageswith no filter to see everything.- The tag is
latestwhen you expected1.0. You left the:1.0off the-tvalue. Docker then defaults tolatest. Rebuild with the explicit tag.
Summary of the starting state
By the end of this chapter you should have these things in place:
- A
Dockerfilethat builds without errors. - A local image named
myapp:1.0, visible indocker images myapp. - A confirmed run that prints the greeting.
Exercise
You run docker build -t myapp . and then docker images myapp. Which tag appears, and why does it matter before you push?
Show answer
The tag is latest, because no tag was given in the -t value and Docker defaults to latest. It matters because the pushed image would be labeled latest rather than a specific version, so you could not tell later which build it was. Rebuild with -t myapp:1.0 to set an explicit tag.