Linux Labs

Containers: Docker and compose / Stage 4 of 11

Dockerfile: your own image

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 7 · on a machine · 7 min

An image is built from a Dockerfile

You will read the site image's recipe and ship the edit: build a new image and a container from it.

The site:1 image was built from a recipe — the Dockerfile in ~/site. Each line is an instruction for the builder:

dockerfile
FROM busybox
WORKDIR /www
COPY index.html .
RUN date -u '+%F %T' > built.txt
CMD ["httpd", "-f", "-p", "8080"]
Instruction What it does during the build
FROM busybox takes the ready-made busybox image as the base
WORKDIR /www creates the /www directory and makes it current
COPY index.html . copies a file from the build directory into the image
RUN … runs a command inside the future image — here it writes the build time to built.txt
CMD […] sets the container's start command; without its own CMD the image takes the base's one

docker build builds the image:

bash
sudo docker build -t site:2 .

-t site:2 is the new image's name. The dot at the end is the build context: the directory docker hands over to the builder in full (the [internal] load build context step in the build output). COPY sees files from it and nowhere else.

COPY works once, during the build: the file lands in the image as a copy, and every container from site:1 gets the same Site v1. docker restart restarts the process in the same container, with the same files. So an edit in ~/site reaches the site in two steps: a new image, then a new container from it.

Note

The build output is lines numbered #1, #2…: first the service steps [internal], then the Dockerfile instructions in order — [1/4] FROM …, [2/4] WORKDIR /www. CMD does not get a step of its own: it is an image setting.

Exercise · do it

Ship the edit to the site

Build the site:2 image from ~/site and replace the web container with a new one from that image, with the same name and port 8080.

Sign in and start The machine is available after signing in

Goals · 0 of 2

  • Build the site:2 image from ~/site not done yet
  • Run web from the new image not done yet

Step goals

0 of 2
  • Build the site:2 image from ~/site — not done yet
  • Run web from the new image — not done yet
Ubuntu 24.04

Step goals

0 of 2
  • Build the site:2 image from ~/site — not done yet
  • Run web from the new image — not done yet
Loading…