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 doalpine,apk ...,["redis...]: Argument to the Instructionalpine: a base imageapk: a package manager program
The Build Process in Detail
docker build .FROM-> image1 (File System snapshot) ->RUNruns a container using image1 -> image2 (FS snapshot) ->CMDruns a container using image2 -> image3
Quiz
- Q1: What does the
FROMcommand do?- A1:
FROMcopies the filesystem snapshot and default command from another image into the custom image we are building.
- A1:
- Q2: The
hello-worldimage has a filesystem snapshot has exactly one file inside of it, thehellofile. This is a program that is executed when you first execute thehello-worldimage as a container. Thehello-worldimage 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 nodejscommand. We would see this error message because our image doesn’t have anapkprogram, since it didn’t inheritapkfromhello-world.- Q3: Why do we use
alpineas 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
alpineas a base image?- A4: No.
This post is licensed under CC BY 4.0 by the author.