Sub topic 3
Starting from a local image
A push uploads an image that already exists on your machine. This chapter covers how to produce that image, how to confirm it is the one you mean to publish, and what to check before you spend time on login and tagging. I assume Docker is installed and running, you can use a terminal, and you have built an image from a Dockerfile before.
What "local image" means
Docker keeps images in a local image store managed by the Docker daemon (the background service that does the building and running). When you run docker build, the result goes into that store. docker push reads from the same store. If the image is not there, there is nothing to push.
Three terms matter here:
- Image ID: a hash that identifies the image's configuration. It is shown in the
IMAGE IDcolumn and never changes for a given image. - Name and tag: a human-readable label such as
myapp:1.0. A label points at an image and can be moved to a different one. - Layer: one filesystem change produced by a Dockerfile instruction. An image is a stack of layers plus metadata.
Names are only labels. One image can carry several names, and a name can later move to a newer image. This is why the image you push is decided by the name you give to docker push, not by the one you last built.
Create a minimal image
This tutorial uses a very small image so that each step finishes quickly. Create an empty directory and save this as Dockerfile:
FROM alpine:3.20
CMD ["echo", "Hello from Docker Hub"]FROM alpine:3.20 starts from a small Linux base image, and CMD sets the command that runs when a container starts. The base is pinned to 3.20 instead of latest, so rebuilding next month gives you the same base.
Build it and list the result:
docker build -t myapp:1.0 .
docker images myappThe -t flag (short for --tag) names the image as it is built. The final . is the build context, which is the directory whose files Docker can use during the build. The output of docker images myapp looks similar to this:
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp 1.0 3f1c2a9b7d10 10 seconds ago 7.8MBThe ID, age, and size on your machine will differ. What matters is that a row exists with the repository myapp and the tag 1.0.
Reading the `docker images` output
The columns can mislead if you read them as Docker Hub concepts. At this point nothing involves Docker Hub.
| Column | Meaning | Common confusion |
|---|---|---|
| `REPOSITORY` | The name part before the colon | A local `myapp` is not yet a Docker Hub repository. |
| `TAG` | The label after the colon | If you omit it at build time, Docker uses `latest`. |
| `IMAGE ID` | Short form of the image's hash | Two rows with the same ID are the same image with two names. |
| `CREATED` | When the image was built | It is not when you pulled or tagged it. |
| `SIZE` | Disk usage of the image | Shared layers are counted once on disk, but each row shows its full size. |
Pay attention to the repository column. A name like myapp has no namespace, meaning no account or organization prefix. Docker Hub needs a namespace to know where an upload belongs, and the tagging chapter adds it. Building under a short name first is fine.
Confirm the image does what you expect
Run the image before you publish it. Pushing a broken image is easy to do and annoying to undo, because other people or other machines may pull it before you notice.
docker run --rm myapp:1.0The --rm flag deletes the container when it exits, so test runs do not pile up. You should see Hello from Docker Hub.
For a closer look at what you are about to publish, use docker image inspect. The --format option takes a Go template and prints only the fields you ask for:
docker image inspect myapp:1.0 --format '{{.Id}}'
docker image inspect myapp:1.0 --format '{{.Os}}/{{.Architecture}}'The first command prints the full image ID. The second prints the platform the image was built for, for example linux/amd64 or linux/arm64. Check the platform whenever you build on one kind of machine and plan to run elsewhere.
Build context and `.dockerignore`
The . at the end of docker build sends the whole directory to the builder. This matters for two reasons:
- A large context makes every build slower, because Docker has to transfer and scan it.
- Instructions such as
COPY . .place those files into the image. Anything in the context can end up in a layer you later upload.
The second point is a security issue. Secrets that were copied into an image can be read by anyone who can pull it. Removing a file in a later RUN rm step does not help, because the earlier layer still contains it.
A .dockerignore file in the context root lists paths that are excluded from the build context. The syntax is similar to .gitignore:
.git
.env
node_modules
*.logOur alpine example copies nothing, so it has no risk. Add a .dockerignore as soon as an image uses COPY or ADD.
Names you give at build time
docker build -t accepts any valid name, so you could build straight to YOUR_USERNAME/myapp:1.0. This tutorial builds as myapp:1.0 and tags it afterward, for two reasons. First, the same local image often needs several names, for example a version and latest, or one per registry. Second, tagging is a cheap local operation that does not rebuild anything, so the build step stays independent of where you publish.
You can add more names to an existing image at any time. That is the subject of the tagging chapter. For now, remember that building produces one image and that names are separate, movable labels.
A few rules apply to names even at this stage:
- The repository part must be lowercase.
MyApp:1.0is rejected. - A tag may contain letters, digits, underscores, periods, and dashes, up to 128 characters, and cannot start with a period or a dash.
- If you leave out the tag, Docker uses
latest. This is only a default name. It does not mean "the newest image".
What happens when you rebuild
Building again with the same -t value moves the name to the new image. The previous image keeps its ID but loses the name, and then shows as <none> in listings.
docker build -t myapp:1.0 .
docker images
docker images --filter dangling=trueAn untagged image that is not used as a layer by any named image is called dangling. If you edit the Dockerfile and rebuild, expect to see these. They are harmless but use disk space. Remove them with:
docker image prunedocker image prune asks for confirmation and removes only dangling images. It does not touch images that have a name. Adding -a removes every image that no container is using, which is more than most people want, so read the prompt before you confirm.
If the build cache hides a change, for example a package list that went stale, add --no-cache to force every step to run again. Use it sparingly, since caching is what keeps builds fast.
Common problems before you push
| Symptom | Likely cause | Fix |
|---|---|---|
| `docker: command not found` or "Cannot connect to the Docker daemon" | Docker is not installed or not running | Start Docker Desktop or the `docker` service, then retry. |
| `invalid reference format` | Uppercase letters or illegal characters in the name | Use lowercase repository names and a valid tag. |
| `docker images myapp` prints only the header | The image was built under a different name | Run `docker images` with no filter and look for `<none>` rows. |
| The container exits at once with no output | `CMD` or `ENTRYPOINT` is missing or wrong | Run `docker run --rm -it myapp:1.0 sh` and test the command by hand. |
| Build is slow before any step runs | Large build context | Add a `.dockerignore`, or build from a smaller directory. |
If you use Docker Desktop with a newer engine, the image store may be backed by containerd instead of the classic store. The commands in this tutorial behave the same, but the listing can show extra detail about the platforms an image contains.
Checklist before moving on
Before you tag and push, you should be able to answer yes to each of these:
docker images myappshows a row with the tag you intend to publish.docker run --rm myapp:1.0prints the expected output.- The platform from
docker image inspectmatches where the image will run. - Nothing secret is in the build context or in any instruction.
With a verified local image, the remaining steps are authentication, tagging with your namespace, and the push itself.
Exercise
You run docker build -t myapp . and then docker images myapp. The row shows the tag latest, even though you never typed it. Why, and what happens to that row if you edit the Dockerfile and run the same build again?
Show answer
When no tag is given, Docker uses latest as the default. Rebuilding with the same name moves the name myapp:latest to the new image. The previous image keeps its image ID but loses its name and appears as <none> (a dangling image) until you remove it with docker image prune.
Exercise
A teammate says that deleting a secret file with RUN rm secret.txt after COPY . . makes the image safe to push. Is that right? What should they do instead?
Show answer
No. The COPY instruction created a layer that still contains the file, and anyone who pulls the image can extract it. They should keep the file out of the build context with .dockerignore, and pass secrets at runtime or through BuildKit secret mounts instead of copying them into the image.