- Each docker image is made of multiple layers and each layer is just another docker image.
- The very first layer is called the base layer.
- The layers in an image can be viewed using
docker history imagename:tag
command. - When we create a container from an image we create a new writable layer where all the read/write or modify actions happen in this writable layer .
- When the container is stopped this writable layer is discarded.
There are two ways to build docker images.
- Commit changes made to a container to create new image
- Create an image using Dockerfile
To create an image using docker commit
command we first run the base container for out image and then make the required changes and commit those changes using docker commit
command to create the new image.
Steps involved:
- Run the base container using the
-it
flags in order to be able to modify the writable layer - Now We add required applications or files to the image and close the container.
- Use
docker ps -a
command to list all the containers. - Copy the container ID of our modified container.
- Use command
docker commit container_ID repository_name:tag
to create the image
- A Dockerfile is a text document that contains all the instructions user provides to assemble the image.
- Each instruction will create a new image layer to the image.
- Instructions specify what to do when building the image.
Note: A Dockerfile is a file named Dockerfile with no extensions.
Steps to create a docker image using a Dockerfile:
-
Create a dockerfile
Sample Dockerfile:
FROM debian:jessie RUN apt-get update RUN apt-get install vim -y RUN apt-get install git -y
-
Use
docker build -t your_tag .
to build the image.
Docker build command takes the path to build contxt as an argumnent. Which is .
in this case. When build starts, docker client would pack all the files in the build context into a tarball and then transfers the tarball file to the daemon. By default docker would search for the Dockerfile in the build context path.
When the above mentioned dockerfile is used to build a new image
- first docker creates a new container from the base image debian/jessie.
- Then runs the command
RUN apt-get update
on the base image, modifying the writable layer. - once this instruction is executed, a new container is created with the previous changes intact and the previous container is discarded.
- In this new container the second instruction
RUN apt-get install vim -y
is executed. - creation of containers -> execution of commands -> commit changes and create new container -> discatding old container -> executing commands, this cycle is continued till all the instructions are completed and final image is created.
Only the instructions RUN
, COPY
, ADD
create layers. Other instructions create temporary intermediate images, and do not increase the size of the build.
A complete list of instructions can be found here.