Sunday, May 13, 2018

Building Docker Image From Scratch


Beginners Guide to dockers part 1, has covered the architecture of dockers. Along with this, so far, I have covered different types of dockers installation, how to download, install and delete docker image and Docker Beginners Guide - Troubleshooting

This post is more focused on creating a docker image and it can be used anywhere in your project basis on the requirements.

Lets create a new folder in windows directory called create-image and change the current directory to create-image.

        
PS C:\Lab\create-image>


Now create a new file called Dockerfile.txt in the current directory with below mentioned commands.
        
PS C:\Lab\create-image> cat .\Dockerfile.txt
FROM centos:7
RUN yum install -y wget
PS C:\Lab\create-image>


Let's create a new image by using Dockerfile.txt created in the previous step.
        
PS C:\Lab\create-image> docker image build -t my-new-image -f ./Dockerfile.txt .


Below is the output after running the above command.
        
PS C:\Lab\create-image> docker image build -t my-new-image -f ./Dockerfile.txt .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos:7
7: Pulling from library/centos
Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Status: Downloaded newer image for centos:7
 ---> e934aafc2206
Step 2/2 : RUN yum install -y wget
 ---> Running in cfc91e766858
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: ftp.cuhk.edu.hk
 * extras: ftp.cuhk.edu.hk
 * updates: ftp.cuhk.edu.hk
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-15.el7_4.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package        Arch             Version                   Repository      Size
================================================================================
Installing:
 wget           x86_64           1.14-15.el7_4.1           base           547 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
Public key for wget-1.14-15.el7_4.1.x86_64.rpm is not installed
warning: /var/cache/yum/x86_64/7/base/packages/wget-1.14-15.el7_4.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f
4a80eb5: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) "
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-4.1708.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-15.el7_4.1.x86_64                                  1/1
install-info: No such file or directory for /usr/share/info/wget.info.gz
  Verifying  : wget-1.14-15.el7_4.1.x86_64                                  1/1

Installed:
  wget.x86_64 0:1.14-15.el7_4.1

Complete!
Removing intermediate container cfc91e766858
 ---> 4a991aace711
Successfully built 4a991aace711
Successfully tagged my-new-image:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and director
ies added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions f
or sensitive files and directories.


If you remeber, in the Dockerfile created in the previous step has two steps, the same can be found in the above output also. Let's do he postmartem of the above output. The first thing the builder does is package the files in the current build context and sends the resulting .tar file to the Docker daemon.
        
Sending build context to Docker daemon  2.048kB


Now we have the next output mentioned in step1/2. It will pull the centos from the docker registry if not available locally
        
Step 1/2 : FROM centos:7
7: Pulling from library/centos
Status: Downloaded newer image for centos:7


Below is the shortend output of next output mentioned in step2/2. It will run the "yum" command as mentioned in Dockerfile and download the wget package. Finally it will remove the inter mediator container and finally you can see the container name at the end "4a991aace711"
        

Step 2/2 : RUN yum install -y wget
 ---> Running in cfc91e766858
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: ftp.cuhk.edu.hk
 * extras: ftp.cuhk.edu.hk
 * updates: ftp.cuhk.edu.hk
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-15.el7_4.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package        Arch             Version                   Repository      Size
================================================================================
Installing:
 wget           x86_64           1.14-15.el7_4.1           base           547 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
Running transaction
  Installing : wget-1.14-15.el7_4.1.x86_64                                  1/1
install-info: No such file or directory for /usr/share/info/wget.info.gz
  Verifying  : wget-1.14-15.el7_4.1.x86_64                                  1/1

Installed:
  wget.x86_64 0:1.14-15.el7_4.1

Complete!
Removing intermediate container cfc91e766858
 ---> 4a991aace711
Successfully built 4a991aace711
Successfully tagged my-new-image:latest


Finally you can check your image by running "docker images" command.
        
PS C:\Lab\create-image> docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
my-new-image               latest              4a991aace711        5 minutes ago       263MB


People who read this post also read :



No comments: