Post

03. Building Custom Images Through Docker Server

03. Building Custom Images Through Docker Server

Creating Docker Images

  • Dockerfile -> Docker client -> Docker Server -> Usable Image!
  • Dockerfile: Configuration to define how our container should behave
    • Specify a base image -> Run some commands to install additional programs -> Specify a command to run on container startup

Dockerfile Teardown

1
2
3
4
5
6
7
8
9
10
11
# Step 1: Use an existing docker image as a base

FROM alpine

# Step 2: Download and install a dependency

RUN apk add --update redis

# Step 3: Tell the image what to do when it starts as a container

CMD ["redis-server"]
  • FROM, RUN, CMD: Instruction telling Docker Server what to do
  • alpine, apk ..., ["redis...]: Argument to the Instruction
  • alpine: a base image
  • apk: a package manager program

The Build Process in Detail

  • docker build .
  • FROM -> image1 (File System snapshot) -> RUN runs a container using image1 -> image2 (FS snapshot) -> CMD runs a container using image2 -> image3

Quiz

  • Q1: What does the FROM command do?
    • A1: FROM copies the filesystem snapshot and default command from another image into the custom image we are building.
  • Q2: The hello-world image has a filesystem snapshot has exactly one file inside of it, the hello file. This is a program that is executed when you first execute the hello-world image as a container. The hello-world image has absolutely no other programs inside of it. With that in mind, what would happen if we tried to build an image with this Dockerfile:
1
2
3
FROM hello-world
RUN apk add nodejs
CMD ["node", "-e", "console.log('hi there');"]
  • A2: We would get an error message during the RUN apk add nodejs command. We would see this error message because our image doesn’t have an apk program, since it didn’t inherit apk from hello-world.

  • Q3: Why do we use alpine as a base image when building our own custom images?
    • A3: Alpine includes a default set of programs that are useful for setting up our own custom image. And Alpine is a very small image. This means that Docker can create containers out of our base image slightly faster.
  • Q4: Are all custom images required to use alpine as a base image?
    • A4: No.
This post is licensed under CC BY 4.0 by the author.