Docker Lesson 3 — Building Custom Images Through Docker Server

Yao-Shang Tseng
Sep 2, 2018 · 2 min read

It’s the note for Docker and Kubernetes: The Complete Guide
https://www.udemy.com/docker-and-kubernetes-the-complete-guide/

Dockerfile : Configuration to define how our container should behave.

Dockerfile -> Docker Client -> Docker Server -> usable Image!

Creating a Dockerfile

  1. Specify a base image
  2. Run some commands to install additional programs
  3. Specify a command to run on container startup
Create an image that runs redis-server1. $ mkdir redis-image
2. $ cd redis-image
3. $ code .
4. $ touch Dockerfile
5.
# Use an existing docker image as a base
FROM alpine
# Download and install a dependency
RUN apk add --update redis
# Tell the image what to do when it starts as a container
CMD ["redis-server"]
6. $ docker build .
7. $ docker run fc60771eaa08
--
Instruction telling Docker Server what to do
FROM, RUN, CMD
--Writing a dockerfile == Being given a computer with no OS and being told to install Chrome
# Specify a base image
1st. install an operating system
# Run commands to install additional programs
2nd. Start up your default browser
3rd. Navigate to chrome.google.com
4th. Download installer
5th. Open file/folder explorer
6th. Execute chrome_installer.exe
# Command to run on startup
7th. Execute chrome.exe
--More detailFROM alpine
- Download alpine image
RUN apk add --update redis
- Get image from previous step
- Create a container out of it -> Container!
- Run `apk add --update redis` in it -> Container! with modified FS
- Take snapshot of that container's FS -> FS snapshot
- Shut down that temporary container
- Get image ready for next instruction
CMD ["redis-server"]
- Get image from last step
- Create a container out of it -> container!
- Tell container it should run `redis-server` when started -> Container! with modified primary command
- Shut down that temporary container
- Get image ready for next instruction
No more steps!
Output is the image generated from previous step
--Tagging am Image
# -t stephengrider/redis:latest -> Tags the image
# . -> Specifies the directory of files/folders to use for the build
$ docker build -t stephengrider/redis:latest .NOTE:
-t stephengrider/redis:latest
# stephengrider -> your Docker ID
# redis -> Repo/Project name
# latest -> Version
$ docker run stephengrider/redis:latest--$ docker commit -c 'CMD ["redis-server"]' jko27891ejk1
and would get a new container id
jko27891ejk1 is the previous container id that existing, and the commit with -c means to run 'CMD ["redis-server"]' with this container id

The concept is like each step finished in their specific container id, and the next step would take this container id to make its own container with new id. Stack by stack.

Building Dockerfile would use cache for existing container.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade