Use of docker image command and build an image from Docker file

Create, delete, check & inspect images in Docker repository

Following are the instruction used in Docker file.

ADD – copies a file into the image but also supports tar and remote URL.

COPY- copy files into the image.

VOLUME- create a mount point as defined when the container is run.

ENTRYPOINT – The executable runs when a container is run.

EXPOSE: documents the ports that should be published.

CMD: provides arguments for the entrypoint

ENV: used to defined environmental variables in the container.

FROM: defines the base image; It is the FIRST instruction in the docker file.

ONBUILD: only used as a trigger when this image is used to build other images.

RUN: runs a new command in a new layer

WORKDIR: defines the working directory of the container.

Example of docker file: Use first FROM command then RUN to update. Save with name Dockerfile

FROM ubuntu
RUN apt-get update

Build or Create an image from the docker file: Present location has dockerfile . represent current directory

#docker build .

Sunny@Oracle1:~/Desktop$ docker build .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu
latest: Pulling from library/ubuntu
677076032cca: Pull complete 
Digest: sha256:9a0bdde4188b896a372804be2384015e90e3f84906b750c1a53539b585fbbe7f
Status: Downloaded newer image for ubuntu:latest
 ---> 58db3edaf2be
Step 2/2 : RUN apt-get update
 ---> Running in 34b6424e0051
Get:1 jammy-security InRelease [110 kB]
Get:2 jammy InRelease [270 kB]
Get:3 jammy-security/restricted amd64 Packages [752 kB]
Get:4 jammy-updates InRelease [119 kB]
Get:5 jammy-backports InRelease [107 kB]
Get:6 jammy/universe amd64 Packages [17.5 MB]
Get:7 jammy-security/main amd64 Packages [807 kB]
Get:8 jammy-security/multiverse amd64 Packages [5557 B]
Get:9 jammy-security/universe amd64 Packages [860 kB]
Get:10 jammy/main amd64 Packages [1792 kB]
Get:11 jammy/multiverse amd64 Packages [266 kB]
Get:12 jammy/restricted amd64 Packages [164 kB]
Get:13 jammy-updates/universe amd64 Packages [1095 kB]
Get:14 jammy-updates/main amd64 Packages [1136 kB]
Get:15 jammy-updates/multiverse amd64 Packages [10.9 kB]
Get:16 jammy-updates/restricted amd64 Packages [808 kB]
Get:17 jammy-backports/universe amd64 Packages [22.4 kB]
Get:18 jammy-backports/main amd64 Packages [49.0 kB]
Fetched 25.8 MB in 11s (2411 kB/s)
Reading package lists...
Removing intermediate container 34b6424e0051
 ---> 8c7e8f11d233
Successfully built 8c7e8f11d233

Verify the images:

sunny@Oracle1:~/Desktop$ docker images
REPOSITORY         TAG         IMAGE ID       CREATED          SIZE
<none>             <none>      8c7e8f11d233   23 seconds ago   119MB

Sunny@Oracle1:~/Desktop$ docker tag 8c7e8f11d233 image1
sunny@Oracle1:~/Desktop$ docker images
REPOSITORY         TAG         IMAGE ID       CREATED              SIZE
image1             latest      8c7e8f11d233   About a minute ago   119MB

For delete the image:

#docker image rm <imageid>


#docker image rmi <imageid>

For delete the image which is not associated with any container:

#docker image prune --dangling images means image which not has any tag.


#docker image prune -a   --remove all images which is not associated with any container 

Docker image inspect command for check detail information of image.

#docker image inspect 

Use Docker tag for image:

docker image tag imageid imagename:tag

#docker image tag 691f08913384   ubuntu:latest

Check all the layers for the image:

docker image history <imageid/imagename>

sunny@Oracle1:~/Desktop$ docker image history ubuntu:latest

IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT

58db3edaf2be   4 weeks ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        

<missing>      4 weeks ago   /bin/sh -c #(nop) ADD file:18e71f049606f6339…   77.8MB    

<missing>      4 weeks ago   /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B        

<missing>      4 weeks ago   /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B        

<missing>      4 weeks ago   /bin/sh -c #(nop)  ARG LAUNCHPAD_BUILD_ARCH     0B        

<missing>      4 weeks ago   /bin/sh -c #(nop)  ARG RELEASE                  0B        


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.