Feb 26, 2023

Docker - working steps | Part-2 Dockerfiles

Dockerfiles

Docker builds images automatically by reading the instructions from a Dockerfile -- a text file that contains all commands, in order, needed to build a given image. A Dockerfile adheres to a specific format and set of instructions.

-- Create docker file

[root@ip-172-31-38-12 ~]# cd docker_class/
[root@ip-172-31-38-12 docker_class]# touch dockerfile

[root@ip-172-31-38-12 docker_class]# vi dockerfile

FROM ubuntu
MAINTAINER gmohapat
RUN apt-get update
RUN echo "Hi docker" > /tmp/demofile

[root@ip-172-31-38-12 docker_class]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    58db3edaf2be   4 weeks ago   77.8MB

# docker build -t mynewimage .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM ubuntu
 ---> 58db3edaf2be
Step 2/4 : MAINTAINER gmohapat
 ---> Running in c70490836769
Removing intermediate container c70490836769
 ---> da0ef27edd59
Step 3/4 : RUN apt-get update
 ---> Running in 22211fb6e919
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
...
Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [49.0 kB]
Fetched 25.8 MB in 9s (2777 kB/s)
Reading package lists...
Removing intermediate container 22211fb6e919
 ---> 32e0c4cba503
Step 4/4 : RUN echo "Hi docker" > /tmp/demofile
 ---> Running in 40cd8fb3ace0
Removing intermediate container 40cd8fb3ace0
 ---> db016147e5c7
Successfully built db016147e5c7
Successfully tagged mynewimage:latest
[root@ip-172-31-38-12 docker_class]#

# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
mynewimage   latest    db016147e5c7   About a minute ago   119MB
ubuntu       latest    58db3edaf2be   4 weeks ago          77.8MB

Here, all steps of docker file executed when we build new image. you can see "mynewimage" created and alog with that as per docker file it created a file in /tmp location as well.
let us try to create a container and see this.

# docker run -it --name container51 mynewimage
root@59105008f353:/#
root@59105008f353:/# cd /tmp
root@59105008f353:/tmp# ls
demofile
root@59105008f353:/tmp# cat demofile
Hi docker

Perfect. Here in the container, the required file also created.
I real-time, we can do/ run post steps/ configuration steps as soon as container created.

Note: dockerfile is default. But any customized name can be used as per industry standard but call of that file is different.

example:

Assigment-3


1) create scenario

[root@ip-172-31-38-12 docker_class]# touch index.html
[root@ip-172-31-38-12 docker_class]# touch config
[root@ip-172-31-38-12 docker_class]#
[root@ip-172-31-38-12 docker_class]# tar -cvf config.tar config
config
[root@ip-172-31-38-12 docker_class]# gzip config.tar
[root@ip-172-31-38-12 docker_class]# ls
config  config.tar.gz  dockerfile  Dockerfile_dev  index.html
[root@ip-172-31-38-12 docker_class]#

2) now create docker file

[root@ip-172-31-38-12 docker_class]# touch Dockerfile_dev
[root@ip-172-31-38-12 docker_class]# vi Dockerfile_dev
FROM ubuntu
MAINTAINER gmohapat
# assume /tmp is our application file location
WORKDIR /tmp
RUN apt-get update
RUN echo "hi kudos, setting app" > /tmp/setupfile
#create environment variable
ENV NAME variable1
# copy a file
COPY index.html /tmp/html/
# add containt
ADD config.tar.gz /tmp/newdir/

3) Now build docker file

[root@ip-172-31-38-12 docker_class]# docker build -t testingimage1 -f Dockerfile_dev .
Sending build context to Docker daemon   5.12kB
Step 1/8 : FROM ubuntu
 ---> 58db3edaf2be
Step 2/8 : MAINTAINER gmohapat
 ---> Using cache
 ---> da0ef27edd59
Step 3/8 : WORKDIR /tmp
 ---> Running in 84ee2d0c7642
Removing intermediate container 84ee2d0c7642
 ---> 962604ce9ba8
Step 4/8 : RUN apt-get update
 ---> Running in 2a9d7e1a2ad2
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...
Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
Fetched 25.8 MB in 7s (3620 kB/s)
Reading package lists...
Removing intermediate container 2a9d7e1a2ad2
 ---> ce9942e3c96f
Step 5/8 : RUN echo "hi kudos, setting app" > /tmp/setupfile
 ---> Running in 1c824429816f
Removing intermediate container 1c824429816f
 ---> 2313e4610ec0
Step 6/8 : ENV NAME variable1
 ---> Running in 01b199067fac
Removing intermediate container 01b199067fac
 ---> e3e9371cb4ed
Step 7/8 : COPY index.html /tmp/html/
 ---> 3b45e19ba67b
Step 8/8 : ADD config.tar.gz /tmp/newdir/
 ---> 50cf5debc384
Successfully built 50cf5debc384
Successfully tagged testingimage1:latest
[root@ip-172-31-38-12 docker_class]#

Here, docker file build successfully and all instructions passed successfully.

Now lunch a container and validate all.

[root@ip-172-31-38-12 docker_class]# docker run -it --name container61 testingimage1 /bin/bash

root@704302e1dac8:/tmp# ls
html  newdir  setupfile
root@704302e1dac8:/tmp# cd newdir/
root@704302e1dac8:/tmp/newdir# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 24 17:54 config
root@704302e1dac8:/tmp/newdir#

Perfect. As per the instruction image created as per dockerfile content.
The ADD command copy the config.tar.gz to /tmp/newdir wth extracted mode. This is the significance of ADD command over COPY command.

No comments:

Post a Comment

Translate >>