Sub topic 8
Building, tagging, and running your image
This chapter assumes you can run docker commands, have a working Dockerfile, and know what an image and a container are. You will build an image, tag it, run it, and fix the errors people hit most often.
Building an image
docker build reads a Dockerfile and a build context, then produces an image. The final argument is the context path. A dot means the current directory.
docker build -t myapp:1.0 .The -t flag (short for --tag) names the result. Without it, the image gets only an ID such as sha256:3f1a..., which is hard to refer to later.
Docker looks for a file named Dockerfile in the context root. To use another file, pass -f:
docker build -f Dockerfile.dev -t myapp:dev .How tags work
An image reference has the form repository:tag. If you leave out the tag, Docker uses latest. The latest tag is an ordinary label, not a guarantee of the newest build. It only points at whatever was last tagged that way.
One image can carry several tags. Tagging adds a name and does not copy any data.
docker tag myapp:1.0 myapp:latest
docker tag myapp:1.0 registry.example.com/team/myapp:1.0
docker images myappA registry address in front of the name tells Docker where docker push should send the image. Use specific tags, such as a version number or a Git commit hash, for anything you deploy. A tag like 1.0 can be moved to a different image, so it is a convention and not a guarantee of immutability. For exact reproducibility, reference the image by digest (myapp@sha256:...).
Running a container
docker run creates a container from an image and starts it. This example uses the Node.js image from the earlier chapters.
docker run --rm -p 3000:3000 myapp:1.0--rmdeletes the container when it exits, so stopped containers do not pile up.-p 3000:3000publishes a port. The format ishost_port:container_port.
Useful variations:
docker run -d --name web -p 8080:3000 myapp:1.0
docker logs -f web
docker exec -it web sh
docker stop web-d runs the container in the background. --name gives it a name you can use in later commands. docker exec starts a second process inside a running container, which helps with debugging.
The right side of -p must match the port your app listens on. The left side is free to change. Here the app is reachable at http://localhost:8080.
The workflow at a glance
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `unable to prepare context: path not found` | Wrong context path | Run from the right directory or fix the final argument |
| `failed to read dockerfile` | No file named `Dockerfile` in the context | Check the name, or pass `-f` |
| `COPY failed: file not found` | The file is outside the context or excluded by `.dockerignore` | Move it into the context or adjust the ignore file |
| `port is already allocated` | Another process uses the host port | Change the left side of `-p` |
| Browser cannot connect, container is running | App listens on `127.0.0.1` inside the container | Make the app listen on `0.0.0.0` |
| `Unable to find image locally` followed by a pull error | Tag misspelled, or a private image without login | Check the name with `docker images`, or run `docker login` |
Checking your result
Confirm the image exists and inspect what it will run:
docker images myapp
docker inspect --format '{{.Config.Cmd}} {{.Config.User}}' myapp:1.0For the Node.js Dockerfile from this tutorial, the output shows the node server.js command and the node user.
Exercise
You run docker run -p 8080:3000 myapp:1.0 and the app starts. Which URL do you open on the host, and what changes if you want the container to survive after it exits, so you can read its logs later?
Show answer
Open http://localhost:8080, because the left side of -p is the host port. Leave out --rm, which deletes the container on exit. Without it, the stopped container remains, and docker logs <name-or-id> still works.
Exercise
You tag an image myapp:1.0, then run docker tag myapp:1.0 myapp:stable. How many copies of the image data are on disk, and which tag should a production deployment reference?
Show answer
One copy. Tags are extra names for the same image ID. A deployment should reference a specific version tag such as myapp:1.0, or better, a digest. The stable tag can be moved to another image later.