This tutorial post will help you with How to Write Dockerfile To Build Your Own Docker Images with following topics.
End of this post, you will be able to understand,
1. What is dockerfile?
2. Why dockerfile is required?
3. How to write dockerfile?
2. Why dockerfile is required?
3. How to write dockerfile?
In the previous posts, already we have explained the below topics. Refer those links to understand this topic from basics.
What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
Dockerfile Instructions Explained with Examples
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Docker CMD & ENTRYPOINT Differences Explained in Detail
What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
Dockerfile Instructions Explained with Examples
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Docker CMD & ENTRYPOINT Differences Explained in Detail
Also You can Watch this Tutorial video on our YouTube Channel.
"How to Write Dockerfile To Build Your Own Docker Images"
How to Write Dockerfile To Build Your Own Docker Images
Lets get started.
What is Dockerfile?
Dockerfile is a file used to build our own docker images by giving some instructions to customize an existing docker images based on our requirement in an automated way without running a docker container.Why dockerfile is required?
We have lot of pre-existing docker images available in docker hub registry. It can be pulled directly and we can use it for our need.but still we may need to customize the existing docker images based on our requirement. So dockerfile helps us to build our own docker images automatically as per the instructions given.
How to write dockerfile?
Use any editor to create a dockerfile with filenames either "dockerfile" or "Dockerfile" and add the instructions based on the requirement. Refer this link to know about all dockerfile instructions available to write dockerfile with examples.Let's take an example to write dockerfile.
Assume that we are going to build our own docker image automatically for httpd web server using dockerfile.
Steps involved to build our own docker image is,
1. Create a Dockerfile in current directory or any directory.
2. Define the instructions to customize the existing docker image as per our requirement.
3. Build a Docker Image from the dockerfile.
4. Run and test our customized docker image by launching docker container.
Step 1 : Create a Dockerfile I create a dockerfile under /root directory.
[root@docker-host ~]# pwd
/root
[root@docker-host ~]# touch dockerfile
[root@docker-host ~]#
Step 2 : Define the instructions to customize the existing docker image as per our requirement.
Edit the created "dockerfile" and specify some instructions as below to customize the existing image to build our own docker image for httpd web server.
[root@docker-host ~]# cat dockerfile
FROM docker.io/centos
MAINTAINER admin
RUN yum update && yum -y install httpd
RUN echo "Welcome to our homepage created using dockerfile" > /var/www/html/index.html
EXPOSE 80
CMD apachectl -D FOREGROUND
Explanation
1. Used FROM instruction to specify the image.
2. Used MAINTAINER instruction to specify information about the author.
3. Used RUN instruction to update all packages and install the httpd package
4. Again used RUN instruction to create a homepage "index.html" under the apache default directory /var/www/html.
5. Used EXPOSE instruction to open the listening port 80 of httpd service.
6. Used CMD instruction to run a apache service command as an executable when container is launched.
We have few more instructions available, that can be used based on our requirement. Refer this link to know more about all instructions available for writing dockerfile.
Step 3. Build a Docker Image from the dockerfile.
Use "docker build ." command to build the docker image if dockerfile exists in current directory. If dockerfile exists in different directory, use "docker build /path/dir"
[root@docker-host ~]# docker build .
Sending build context to Docker daemon 134.6 MB
Step 1/6 : FROM docker.io/centos
---> 49f7960eb7e4
Step 2/6 : MAINTAINER admin
---> Running in 30cb77afb8ac
---> 3ccbf1f7abac
Removing intermediate container 30cb77afb8ac
Step 3/6 : RUN yum update && yum -y install httpd
---> Running in f2d19e5deb7a
---> 6d75cdc88ec8
Removing intermediate container f2d19e5deb7a
Step 4/6 : RUN echo "Welcome to our homepage created using dockerfile" > /var/www/html/index.html
---> Running in d6fae868142c
---> 0bda113ffde7
Removing intermediate container d6fae868142c
Step 5/6 : EXPOSE 80
---> Running in 9f885b216b90
---> c2da840eec09
Removing intermediate container 9f885b216b90
Step 6/6 : CMD apachectl -D FOREGROUND
---> Running in 1439c499048a
---> 480a0b12adb0
Removing intermediate container 1439c499048a
Successfully built 480a0b12adb0
[root@docker-host ~]#
Our new docker image is built with Image ID "480a0b12adb0".
[root@docker-host /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 480a0b12adb0 32 minutes ago 308 MB
docker.io/centos latest 49f7960eb7e4 3 weeks ago 200 MB
[root@docker-host /]#
Tag the newly created image now using "docker tag" command.
[root@docker-host /]# docker tag 480a0b12adb0 learnitguide.net/httpd_server
[root@docker-host /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learnitguide.net/httpd_server latest 480a0b12adb0 43 minutes ago 308 MB
docker.io/centos latest 49f7960eb7e4 3 weeks ago 200 MB
[root@docker-host /]#
Step 4. Run and test our customized docker image by launching docker container.
Use "docker run" command to run the docker container using the docker image created newly.
[root@docker-host /]# docker run -d -it --name myweb1 -p 192.168.2.1:80:80 learnitguide.net/httpd_server
afadc843d8d2db0121d7391a1fe1e4332ab6b2a5e0def0143ed12c70fba38e24
[root@docker-host /]#
Where,
run - used to run the container
-d - daemonize (continously)
-i - Interactive session
-t - To allocate a pseudo terminal for container
--name : To specify the unique container name
-p : specify the ipaddress and listening ports with port mapping.
Use "docker ps" command to see the status of the launched docker container.
[root@docker-host ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
afadc843d8d2 learnitguide.net/httpd_server "/bin/sh -c 'apach..." 2 minutes ago Up 2 minutes 192.168.2.1:80->80/tcp myweb1
[root@docker-host ~]#
Lets try accessing our webserver using the url as "http://192.168.2.1:80" from any other system.
Perfect, we have got the webpage content what we have created using dockerfile.
That's all about how to write dockerfile to build our own docker images. Going forward we will play more with docker tool.
If you are interested in learning, Request you to go through the below recommended tutorial.
DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online
Stay connected with us on social networking sites, Thank you.
0 Comments