Card 03/ 06

WalkthroughDifficulty: Beginner1 min

From a Dockerfile to a Container You Can Open

A project with a Dockerfile in it, turned into something you can open.

Building an image and starting a container from it

Step 1 of 4

Build it

bash
$ docker build -t my-api:1 .
[+] Building 12.4s (10/10) FINISHED
 => naming to docker.io/library/my-api:1

-t my-api:1 names the result. The . is the build context: the directory sent to the builder, and the only place COPY can read from.

Check it exists

bash
$ docker images my-api
REPOSITORY   TAG   IMAGE ID       SIZE
my-api       1     7b1e4c9a2f30   142MB

Run it

bash
$ docker run -d --name api -p 3000:3000 my-api:1
$ curl -s localhost:3000/health
{"status":"ok"}

Change something and build again

bash
$ docker build -t my-api:2 .
$ docker run -d --name api2 -p 3001:3000 my-api:2

A new tag rather than the same one. The old image is still there, still runnable, which is what makes going back cheap.