...

/

Containerize the Application

Containerize the Application

Learn how to containerize the application in detail.

Building the Docker image

In this lesson, we’ll build the application into a container image. Run the following command to build a new image called ddd-book:ch8.node. Be sure to include the trailing period (.), as this tells Docker to use our current working directory as the build context. Remember, the build context is the directory where our app files live.

Press + to interact
$ docker build -t ddd-book:ch8.node .
[+] Building 16.2s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.21kB 0.0s
=> => transferring context: 659B 0.0s
=> [stage-0 1/4] FROM docker.io/library/node:20.8.0-alpine 3s <<---- Base layer
=> [stage-0 2/4] WORKDIR /usr/src/app 0.2s <<---- New layer
=> [stage-0 3/4] RUN --mount=type=bind,source=package... 1.1s <<---- New layer
=> [stage-0 4/4] COPY . . 0.1s <<---- New layer
=> exporting to image 0.2s
=> => exporting layers 0.2s
=> => writing image sha256:f282569b8bd0f0...016cc1adafc91 0.0s
=> => naming to docker.io/library/ddd-book:ch8.node

We’ve snipped the output, but you can see four numbered steps that created four image layers. Check that the image exists in your Docker host’s local repository.

Press + to interact
$ docker images
REPO TAG IMAGE ID CREATED SIZE
ddd-book ch8.node 24dd040fa06b 18 minutes ago 268MB

Congratulations, you’ve containerized the app as an OCI image!

Verifying the image

Run a docker inspect ddd-book:ch8.node command to verify the image and see the settings from the Dockerfile. We should be able to see the image layers and metadata such as the Exposed Ports, WorkingDir, and Entrypoint values.

$ docker inspect ddd-book:ch8.node
[
{
"Id": "sha256:24dd040fa06baf6e40144c5a59f99a749159a932ecebb737751f7f862963527a",
"RepoTags": [
"ddd-book:ch8.node"
<Snip>
"ExposedPorts": {
"8080/tcp": {}
"WorkingDir": "/usr/src/app",
"Cmd": [
"/bin/sh",
"-c",
"node app.js"
],
<Snip>
"Layers": [
"sha256:5f4d9fc4d98de91820d2a9c81e501c8cc6429bc8758b43fcb2cd50f4cab9a324",
"sha256:6b20c4e93dbab9786f96268bbe32c208d385f2c4490a278ad3b1e55cc79480e4",
"sha256:012c308a78ec993a47fdb7c4c6d17b53d8ce2649a463be28ae5c48ab1af2e039",
"sha256:35a839ac7cc922afd896a0297e692141c77ed6e03eff6a70db13bb23f6cd4f8f",
"sha256:918caa8070410ccfb2c5b3b4d62ca66742c46bf21fe0bd433738b7796c530e68",
"sha256:a48b3b3d0c5a693840e7e4abd7971f130b4447573483628bcb996091e1e8e8b8",
"sha256:ea2d4594dbbef4009441a33dd1dd4c5076d7fe09a171381a6b7583605569dd11"
]
<Snip>
Inspecting the Docker image

We might wonder why the image has seven layers when only four Dockerfile instructions created layers. This is because the node:20.8.0-alpine base image already had four layers. Therefore, the FROM instruction pulled a base image with four layers, and ...