How to Create a Container From Dockerfile (Method-3)
Dear Readers,
In this article,we will see Create a Container From Dockerfile.
First, we need to know about Dockerfile.
What is Dockerfile ?
Dockerfile is normal text file.
We mention required instructions in the Dockerfile.
Docker can build images automatically by reading the instructions from a Dockerfile.
Dockerfile that contains all commands, in order, needed to build a Docker image.
The name must be Dockerfile (D must be uppercase and there is no space between them).
Each instruction creates one layer:
FROM creates a layer from the ubuntu from Docker image.
WORKDIR to change the working directory.
COPY copies a file/directory to your image.
ADD copy a file/directory from to your image, but can also extract TAR files(Auto extract) while copying.
RUN builds your application with make.
CMD specifies what command to run within the container.
ENV to assign variables (key and values).
Steps to Follow
- Create a file “Dockerfile” and Add Instructions.
- Create image from Dockerfile.
- Verify Images(created or not).
- Create a container “kt2-container” from image “kt2-image”.
- Verify file “kt-file” inside the container.
- Create another container from Dockerfile.
- Create 2 files “kt-file1” and “kt-file2”.
- Apply zip and tar to file “kt-file2”.
- Create image “kt3-image” from Dockerfile.
- Verify Images(created or not).
- Create a container “kt3-container” from image”kt3-image”.
- Verify files.
1. Create a file “Dockerfile” and Add Instructions
1 2 3 |
[root@ip-172-31-4-99 ec2-user]# vi Dockerfile FROM centos RUN echo "Welcome to our website : www.ktexperts.com" > /tmp/kt-file |
Note
:wq! — to save & quit
2. Create image from Dockerfile
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-4-99 ec2-user]# docker build -t kt2-image . Sending build context to Docker daemon 7.68kB Step 1/2 : FROM centos ---> 0f3e07c0138f Step 2/2 : RUN echo "Welcome to our website : www.ktexperts.com" > /tmp/kt-file ---> Running in 6d1735459a5e Removing intermediate container 6d1735459a5e ---> b4aeae0884e8 Successfully built b4aeae0884e8 Successfully tagged kt2-image:latest |
3. Verify Images
To see the list of all images inside your machine
1 2 3 4 5 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kt2-image latest b4aeae0884e8 55 seconds ago 220MB kt1-image latest 7cc564a503d4 20 minutes ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB |
4. Create a container “kt2-container” from image “kt2-image”
To see all the containers
1 2 3 4 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9c5a84b639b kt1-image "//bin/bash" 12 minutes ago Exited (0) 11 minutes ago kt1-container afeb1d941942 centos "//bin/bash" 33 minutes ago Exited (0) 29 minutes ago kt-container |
Using below command to create a container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run --name kt2-container -it kt2-image //bin/bash [root@a85222c2ebd6 /]# |
5. Verify file “kt-file” inside the container
1 2 3 4 5 6 |
[root@a85222c2ebd6 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@a85222c2ebd6 /]# ls /tmp/ ks-script-0n44nrd1 ks-script-w6m6m_20 kt-file [root@a85222c2ebd6 /]# cat /tmp/kt-file Welcome to our website : www.ktexperts.com |
Exit from the container
1 2 |
[root@a85222c2ebd6 /]# exit exit |
6. Create another container from Dockerfile
1 2 3 4 5 6 7 8 |
[root@ip-172-31-4-99 ec2-user]# vi Dockerfile FROM centos WORKDIR /tmp RUN echo "Welcome to our website : www.ktexperts.com" > /tmp/kt-file RUN echo "It a knowledge sharing platform"> /tmp/kt-file2 ENV myname KTEXPERTS COPY kt-file1 /tmp ADD kt-file2.tar.gz /tmp |
Note
We added two instructions for copying files in the Dockerfile.
Those two files must be in current directory.
The second file must be in tar and zip format (kt-file2.tar.zip).
:wq! — to quit
7. Create 2 files “kt-file1” and “kt-file2”
1 2 3 4 |
[root@i p-172-31-4-99 ec2-user]# touch kt-file1 [root@ip-172-31-4-99 ec2-user]# touch kt-file2 [root@ip-172-31-4-99 ec2-user]# ls Dockerfile kt-file1 kt-file2 |
8. Apply zip and tar to file “kt-file2”
Apply tar
1 2 3 4 |
[root@ip-172-31-4-99 ec2-user]# tar -cvf kt-file2.tar kt-file2 kt-file2 [root@ip-172-31-4-99 ec2-user]# ls Dockerfile kt-file1 kt-file2 kt-file2.tar |
Apply zip
1 2 3 |
[root@ip-172-31-4-99 ec2-user]# gzip kt-file2.tar [root@ip-172-31-4-99 ec2-user]# ls Dockerfile kt-file1 kt-file2 kt-file2.tar.gz |
Remove file “kt-file2”
1 2 3 |
[root@ip-172-31-4-99 ec2-user]#rm -rf kt-file2 [root@ip-172-31-4-99 ec2-user]#ls Dockerfile kt-file1 kt-file2.tar.gz |
9. Create image “kt3-image” from Dockerfile
To see the list of images
1 2 3 4 5 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kt2-image latest b4aeae0884e8 37 minutes ago 220MB kt1-image latest 7cc564a503d4 About an hour ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB |
Using below command to create image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@ip-172-31-4-99 ec2-user]# docker build -t kt3-image . Sending build context to Docker daemon 9.728kB Step 1/7 : FROM centos ---> 0f3e07c0138f Step 2/7 : WORKDIR /tmp ---> Running in 30f744daa0a4 Removing intermediate container 30f744daa0a4 ---> 61ef2264481b Step 3/7 : RUN echo "Welcome to our website : www.ktexperts.com" > /tmp/kt-file ---> Running in 78a3ac7fa634 Removing intermediate container 78a3ac7fa634 ---> 785e08084d72 Step 4/7 : RUN echo "It a knowledge sharing platform" > /tmp/kt-file2 ---> Running in 84f8a49f92a5 Removing intermediate container 84f8a49f92a5 ---> f00cee2ddbc6 Step 5/7 : ENV myname KTEXPERTS ---> Running in 177d6906756a Removing intermediate container 177d6906756a ---> 856aa0552aeb Step 6/7 : COPY kt-file1 /tmp ---> 80f14edf7d25 Step 7/7 : ADD kt-file2.tar.gz /tmp ---> cd8575a1f5d5 Successfully built cd8575a1f5d5 Successfully tagged kt3-image:latest |
10. Verify images
1 2 3 4 5 6 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kt3-image latest 4ebf604d3d7e 8 seconds ago 220MB kt2-image latest b4aeae0884e8 37 minutes ago 220MB kt1-image latest 7cc564a503d4 About an hour ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB |
11. Create a container “kt3-container” from image”kt3-image”
To see all the images inside your machine
1 2 3 4 5 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a85222c2ebd6 kt2-image "//bin/bash" 33 minutes ago Exited (127) 29 minutes ago kt2-container d9c5a84b639b kt1-image "//bin/bash" About an hour ago Exited (0) About an hour ago kt1-container afeb1d941942 centos "//bin/bash" About an hour ago Exited (0) About an hour ago kt-container |
Using below command to create container
We will be there /tmp by default because we mentioned working directory (/tmp) in the Dockerfile.
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run --name kt3-container -it kt3-image //bin/bash [root@0cbb41ce6abc tmp]# |
12. Verify files
1 2 3 4 |
[root@0cbb41ce6abc tmp]# ls ks-script-0n44nrd1 ks-script-w6m6m_20 kt-file kt-file1 kt-file2 [root@0cbb41ce6abc tmp]# cat kt-file Welcome to our website : www.ktexperts.com |
To see the my name
1 2 |
[root@c2ca6a1d0fe4 tmp]# echo $myname KTEXPERTS |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform