Tuesday 21 May 2019

Docker: Create an image


In this post, I am going to explain how to build your own docker image.

To create a docker image, you should create ‘Dockerfile’, that contains specific information that docker needs while creating the image and pass the complete directory (where this Dockerfile is located) to docker daemon.



Step 1: Create a directory ‘myDockerImage’.

Step 2: Create ‘Dockerfile’ in the directory ‘myDockerImage’.
$mkdir myDockerImage
$
$cd myDockerImage/
$touch Dockerfile
$
$ls
Dockerfile

Step 3: Update Dockerfile with below content.


Dockerfile
FROM ubuntu

CMD echo "Welcome to Docker Application Development"

FROM ubuntu
Specifies base operating system is ubuntu. While deploying the image, docker container download the ubuntu image from docker hub. Since I do not specify any ubuntu version here, docker downloads the latest available ubuntu version from docker hub.

CMD echo "Welcome to Docker Application Development"
Execute this command in the container.

Step 4: Build the image by running below command from the directory where your Dockerfile is located.

docker image build -t welcomedocker .

here ‘.’ Represents current directory.
$ls
Dockerfile
$
$docker image build -t welcomedocker .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu
latest: Pulling from library/ubuntu
898c46f3b1a1: Pull complete 
63366dfa0a50: Pull complete 
041d4cd74a92: Pull complete 
6e1bee0f8701: Pull complete 
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:latest
 ---> 94e814e2efa8
Step 2/2 : CMD echo "Welcome to Docker Application Development"
 ---> Running in 6c474bf79854
Removing intermediate container 6c474bf79854
 ---> ed204233a34e
Successfully built ed204233a34e
Successfully tagged welcomedocker:latest


Step 5: Execute the command 'docker image ls', you can see all the image with name 'welcomedocker'.

$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
welcomedocker       latest              ed204233a34e        2 minutes ago       88.9MB
redis               latest              a55fbf438dfd        4 weeks ago         95MB
ubuntu              latest              94e814e2efa8        6 weeks ago         88.9MB
jboss/wildfly       latest              5de2811bb236        7 weeks ago         745MB
hello-world         latest              fce289e99eb9        3 months ago        1.84kB


If you want to see, how the image is built, execute the command 'docker history welcomedocker'.
$docker history welcomedocker
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ed204233a34e        3 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
94e814e2efa8        6 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           6 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           6 weeks ago         /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           6 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
<missing>           6 weeks ago         /bin/sh -c #(nop) ADD file:1d7cb45c4e196a6a8…   88.9MB              
$


Step 6: Run the image welcomedocker by executing the command ‘docker run welcomedocker’.

$docker run welcomedocker
Welcome to Docker Application Development



Previous                                                 Next                                                 Home

No comments:

Post a Comment