Building and running a container: docker build and docker run, flag by flag

2026-12-01来源 dev.toEnglish

Your Dockerfile is ready, but Docker is not running anything yet. Two commands take you from the recipe to an app that answers: docker build makes the image, docker run starts a container from it.

We made a 27-second video that shows both:

You can also watch it on our site: Building and running a container.

This post goes through the two commands of the video, flag by flag, and the errors you will meet the first time.

The app

We start from the end of part 1: a small Python app and its Dockerfile. This time the app is a web server, so that the container has something to show.

my-app/ ├── app.py ├── requirements.txt ├── Dockerfile └── .dockerignore 

app.py:

from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "Hello from the container\n" if __name__ == "__main__": app.run(host="0.0.0.0", port=3000) 

requirements.txt:

flask==3.1.2 

Dockerfile:

FROM python:3.13-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python", "app.py"] 

The .dockerignore is the one from part 1. Keep host="0.0.0.0" in mind: we come back to it below. And app.run is Flask's development server, fine for learning Docker. In production, the CMD starts a real server such as Gunicorn.

docker build makes the image

docker build -t myapp:1.0 . 
  • -t myapp:1.0 gives the image a name and a version, the tag. Without -t, the image only has an ID, which nobody wants to type.
  • The dot at the end is the build context: the folder sent to Docker, where it reads the Dockerfile and the files that COPY takes.

Check that the image is there:

docker images myapp 
IMAGE ID DISK USAGE CONTENT SIZE myapp:1.0 bac3f5d418f5 223MB 48.4MB 

That is the layout of Docker 29. Older versions show the same image with REPOSITORY, TAG and SIZE columns.

The image is a frozen template. Nothing runs yet.

docker run starts a container

docker run -d -p 3000:3000 --name prod-app myapp:1.0 

A container is a running copy of the image. Each flag of the video has its job:

  • -d (detached) runs it in the background and gives you your terminal back. Docker prints the ID of the new container. Without -d, the app's output fills your terminal, and Ctrl+C in that terminal stops the container.
  • -p 3000:3000 links a port of your machine to a port of the container. The left one is on your machine, the right one is in the container.
  • --name prod-app gives the container a name of your choice. Without it, Docker makes one up, something like quirky_hopper.
  • myapp:1.0 is the image to start from. It comes after the flags: what you write after the image name is not read by Docker, it is passed to the container as its command.

The app answers:

curl localhost:3000 
Hello from the container 

And the container is listed:

docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 483da78b3aac myapp:1.0 "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0:3000->3000/tcp, [::]:3000->3000/tcp prod-app 

0.0.0.0:3000->3000/tcp reads "port 3000 of the machine goes to port 3000 of the container" (the [::] part is the same for IPv6).

One image, several containers

The image does not change when a container runs. You can start a second container from it, with another name and another port on your machine:

docker run -d -p 3002:3000 --name test-app myapp:1.0 curl localhost:3002 
Hello from the container 

Both apps listen on 3000 inside their own container. Only the port on your machine has to be different.

The errors you will meet

The name is already taken

Run the same command a second time:

docker: Error response from daemon: Conflict. The container name "/prod-app" is already in use by container "483da78b3aac...". You have to remove (or rename) that container to be able to reuse that name. 

The name belongs to the first container, whether it is running or stopped: a stopped container keeps its name until you remove it. Remove the old one first (docker rm -f prod-app), or pick another name.

The port is already taken

Another container already publishes port 3000 of your machine. The error is long, and it ends with:

Bind for 0.0.0.0:3000 failed: port is already allocated 

Change the left side only: -p 3001:3000. Watch out: the failed run still created the container, in the Created state, so its name is taken. docker ps -a shows it, docker rm removes it.

When the port is held by a program outside Docker, do not count on this message. On Docker Desktop for Mac, we got no error at all: the container started, next to a local server already listening on 3000. If the answer on localhost:3000 is not the one you expect, look at what else uses the port.

The flags after the image

Put the flags at the end:

docker run myapp:1.0 -d -p 3000:3000 

The error is long too, and it ends with:

exec: "-d": executable file not found in $PATH 

Docker took -d -p 3000:3000 for the command to run inside the container, in place of the CMD. Flags first, image last. Here too, the failed run left a container in the Created state.

Docker looks for the image online

Forget the tag:

docker run myapp 
Unable to find image 'myapp:latest' locally docker: Error response from daemon: pull access denied for myapp, repository does not exist or may require 'docker login' 

Without a tag, Docker reads latest. Our image is myapp:1.0, there is no myapp:latest, so Docker tries Docker Hub and finds nothing. Write the tag.

The container runs, the page does not answer

This is the one that costs an afternoon. Remove host="0.0.0.0" from app.py, rebuild the image, remove the old container (docker rm -f prod-app) and start a new one: docker ps says Up, the port is published, and yet:

curl localhost:3000 # curl: (56) Recv failure: Connection reset by peer 

(That is the message on Docker Desktop for Mac. Elsewhere it can be another one, such as an empty reply, but the page never comes.)

The logs give it away:

docker logs prod-app 

Among the startup lines:

 * Running on http://127.0.0.1:3000 

By default, Flask listens on 127.0.0.1, which is the inside of the container only. The traffic that -p forwards comes from outside the container, so nobody is listening for it. With host="0.0.0.0", the app listens on all the container's interfaces, and the logs say so:

 * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:3000 * Running on http://172.17.0.2:3000 

Flask is not the only one. Django's runserver, Rails in development and dev servers such as Vite also listen on localhost by default. Inside a container, the app must listen on 0.0.0.0.

No -p, no access

Without -p, the container runs and its app listens, but docker ps shows an empty PORTS column: nothing on your machine leads to it.

In short

Command What it does
docker build -t myapp:1.0 . Builds the image from the Dockerfile of the current folder, named myapp, version 1.0
docker run -d -p 3000:3000 --name prod-app myapp:1.0 Starts a container in the background, port 3000 of your machine to port 3000 of the container
docker ps Lists the running containers (-a adds the stopped ones)
docker logs prod-app Shows what the app printed

Next

The container now runs in the background. Part 3 shows how to watch it and stop it cleanly: Monitoring and stopping a container.

Until then, one command stops and removes the two containers of this post: docker rm -f prod-app test-app.

This video is part of a short series on Docker by Ilaria Digital School, an online tech school with live, instructor-led classes. Docker is taught in our Full-Stack Software Developer Pathway.

原文:https://www.a8q9.cn/en/article/building-and-running-a-container-docker-build-and-docker-run-flag-by-flag-2da61d.html

相关文章