Docker build image from Dockerfile

Building our own images with a Dockerfile, I use Docker build command to build a new image from Dockerfile. How to Docker build image from Dockerfile. Let’s go!

The first Docker build images

To create a directory and a Dockerfile, the sample as below:

$ mkdir project
$ cd project
$ touch Dockerfile

The example Docker build the image from Dockerfile

$ vim Dockerfile

The content a Dockerfile as below:

 # CentOS 7 Dockerfile
 # Docker build:
 # sudo docker build -t devopsroles/centos:latest .
 # Docker create:
 # sudo docker create -it --name centos -h centos devopsroles/centos
 # Docker start:
 # sudo docker start centos
 # Connect with bash
 # sudo docker exec -it centos bash
 FROM centos:latest
 MAINTAINER PHAN VAN HUU <pvhuu90@gmail.com> My blogs: devopsroles.com
 #Update centos
 RUN yum update -y && yum upgrade -y
 RUN yum -y install git curl net-tools wget vim
 # Install EPEL Repository
 RUN yum install -y epel-release
 # Clean CentOS 7
 RUN yum clean all
 # Set the environment variables
 ENV HOME /root
 # Working directory
 WORKDIR /root
 # Default command
 CMD ["bash"]

Docker build image from Dockerfile

Running the Dockerfile use Docker build commands

$ cd project
$ sudo docker build -t .
$ sudo docker build -t devopsroles/centos:latest .
$ sudo docker create -it --name centos -h centos devopsroles/centos

The result, Docker images devopsroles/centos as below:

$ sudo docker images

The output, docker images as below:

 REPOSITORY TAG IMAGE ID CREATED SIZE
 devopsroles/centos latest c9ef43af9836 2 minutes ago 428.7 MB

The docker start container centos

$ sudo docker start centos
$ sudo docker ps

The output below:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 2d1a8e53668f devopsroles/centos "bash" 43 seconds ago Up 4 seconds centos

The connect container bash

$ sudo docker exec -it centos bash

The output below:

[root@centos ~]# ifconfig
 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0
 inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link>
 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
 RX packets 8 bytes 648 (648.0 B)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 8 bytes 648 (648.0 B)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
 inet 127.0.0.1 netmask 255.0.0.0
 inet6 ::1 prefixlen 128 scopeid 0x10<host>
 loop txqueuelen 1 (Local Loopback)
 RX packets 0 bytes 0 (0.0 B)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 0 bytes 0 (0.0 B)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@centos ~]# ping 8.8.8.8
 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
 64 bytes from 8.8.8.8: icmp_seq=1 ttl=127 time=68.7 ms
 64 bytes from 8.8.8.8: icmp_seq=2 ttl=127 time=69.1 ms
 ^C
 --- 8.8.8.8 ping statistics ---
 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
 rtt min/avg/max/mdev = 68.720/68.945/69.170/0.225 ms

Or test ping 8.8.8.8 from docker run command as below

$ sudo docker run centos ping 8.8.8.8 -c4

The output below:

 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
 64 bytes from 8.8.8.8: icmp_seq=1 ttl=127 time=40.6 ms
 64 bytes from 8.8.8.8: icmp_seq=2 ttl=127 time=41.7 ms
 64 bytes from 8.8.8.8: icmp_seq=3 ttl=127 time=42.1 ms
 64 bytes from 8.8.8.8: icmp_seq=4 ttl=127 time=40.1 ms

--- 8.8.8.8 ping statistics ---
 4 packets transmitted, 4 received, 0% packet loss, time 3005ms
 rtt min/avg/max/mdev = 40.124/41.184/42.196/0.832 ms

Conclusion

Docker build image from Dockerfile. You can update and edit package from Dockerfile file. For example to install Nginx, PHP, MySQL package in Dockerfile file. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

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