October 29, 2018
This lab works on 2 ways of creating a docker image, namely, from binary files of container or from Dockerfile. The advantages of using Dockerfile and how Dockerfile relates to image layers are both discussed.
This example shows that we can create a container, add all the libraries and binaries in it and then commit it in order to create an image. This approach of manually installing software in a container and then committing it to a custom image is just one way to create an image. It works fine and is quite common.
docker container run -ti ubuntu bash
apt-get update
apt-get install -y figlet
figlet "hello docker"
docker container ls -a
Commit creates an image locally on the system running the Docker engine.
docker container commit CONTAINER_ID
docker image ls
docker image tag <IMAGE_ID> ourfiglet
docker container run ourfiglet figlet hello
Instead of creating a static binary image, we can use a file called a Dockerfile to create an image. The final result is essentially the same, but with a Dockerfile we are supplying the instructions for building the image, rather than just the raw binary files. This is useful because it becomes much easier to manage changes, especially as your images get bigger and more complex.
var os = require("os");
var hostname = os.hostname();
console.log("hello from " + hostname);
FROM alpine
RUN apk update && apk add nodejs
COPY . /app
WORKDIR /app
CMD ["node","index.js"]
docker image build -t hello:v0.1 .
docker container run hello:v0.1
Dockerfile Explained
A Docker image is built up from a series of layers. Each layer represents an instruction in the image’s Dockerfile. Each layer except the last one is read-only. This design principle is important for both security and data management. If someone mistakenly or maliciously changes something in a running container, you can very easily revert back to its original state because the base layers cannot be changed. Or you can simply start a new container instance which will start fresh from your pristine image.
docker image inspect alpine
docker image inspect --format "{{ json .RootFS.Layers }}" alpine
For the custom image,
docker image inspect --format "{{ json .RootFS.Layers }}" <image ID>
Written by Warren who studies distributed systems at George Washington University. You might wanna follow him on Github