Docker, start to ship · Chapter 7 of 26
Your first Dockerfile
By the end of this chapter you can write a Dockerfile, build your own image from it, run it, and ship a change as a new version. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Images and Dockerfiles
- 4 tasks
- Docker
- In the all labs plan
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.
Why you need this
Ravi needs Tapri's menu API (a small node server in app/ that serves app/menu.txt) on the server today, and on two more next month.
Every image so far came from somebody else. Bunty's plan is to start a plain node container and docker cp the files in, as in chapter 3. That is a step by hand on every server, and the day the container is replaced the files are gone: the new one starts from plain node again.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Write the menu API's Dockerfile
Write the recipe Docker uses to build the menu API's image.
1. Look inside the app folder. It has server.js and menu.txt.
2. Make a file named Dockerfile in the chapter folder, with nano or printf.
3. Write five lines: FROM node:24-alpine, a WORKDIR of /app, a COPY of the files in app/, an EXPOSE of port 3000, and a CMD that runs node server.js.
4. Copy the files so that server.js and menu.txt are directly in /app, not in /app/app.
To pass: Dockerfile is in the chapter folder, starts FROM a node image, and has a WORKDIR, a COPY, an EXPOSE and a CMD line.
- Task 2
Build it as chai-07-api:1.0
Build an image from your Dockerfile.
1. Run docker build in the chapter folder. The build folder is the chapter folder, a single dot.
2. Name the image chai-07-api:1.0 with -t.
3. If the build stops, read the error, fix the Dockerfile and build again.
To pass: the image chai-07-api:1.0 is there, its working folder is /app, it exposes port 3000, and its command runs node server.js.
- Task 3
Run chai-07-api on port 8007
Run your own image as a container.
1. Run a container named chai-07-api from chai-07-api:1.0, detached (-d).
2. Publish the container's port 3000 on the lab's port 8007. The lab's port comes first.
3. Run curl localhost:8007 to see the menu.
To pass: chai-07-api runs from chai-07-api:1.0, and localhost:8007 shows the menu. After task 4 it runs 1.1 instead, and that still passes.
- Task 4
Ship Ginger chai as version 1.1
Add Ginger chai to the menu, and ship it as a new image.
1. Add the line Ginger chai,25 at the end of app/menu.txt, exactly as written. Keep the other lines.
2. Build a new image named chai-07-api:1.1.
3. Delete the old chai-07-api container with docker rm -f.
4. Run a new chai-07-api from chai-07-api:1.1, detached, on port 8007 again.
To pass: chai-07-api runs from chai-07-api:1.1, and localhost:8007 shows Ginger chai at 25.
What you will learn
- A Dockerfile: the recipe for an image, written down once, one instruction per line.
- docker build: turns the recipe and your files into an image with a name and a tag.
- Layers: what each instruction adds to the image, and what it only sets.
- Versions: an image never changes, so a change ships as a new tag and a new container.
More in Images and Dockerfiles
Build "Your first Dockerfile" yourself
Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.