Skip to content

Latest commit

 

History

History
86 lines (44 loc) · 3.33 KB

10_docker_images.md

File metadata and controls

86 lines (44 loc) · 3.33 KB

Docker Images

  • 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.

image-20200316162150359

Building docker images

There are two ways to build docker images.

  1. Commit changes made to a container to create new image
  2. Create an image using Dockerfile

Create using commit

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:

  1. Run the base container using the -it flags in order to be able to modify the writable layer
  2. Now We add required applications or files to the image and close the container.
  3. Use docker ps -a command to list all the containers.
  4. Copy the container ID of our modified container.
  5. Use command docker commit container_ID repository_name:tag to create the image

Creating images using Dockerfile

  • 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:

  1. Create a dockerfile

    Sample Dockerfile:

    FROM debian:jessie
    RUN apt-get update
    RUN apt-get install vim -y
    RUN apt-get install git -y
  2. Use docker build -t your_tag . to build the image.

Understanding docker build

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.