Docker is an open source tool that helps us to develop, ship and run applications. Each and every application needs a Dockerfile. Instructions are written over Dockerfile in a layered/step-by-step structure to run the application. The Dockerfile is then built to create Docker image. We can then push the image either to Docker Hub or keep it in our Workstation. Finally to run the application we have to run container from the image which can be treated as a concept like an instance of the image since we can have more than one container of a single image. If the Docker image is not in our local Workstation we first have to pull the image from Docker hub and then run container from it. But when we need to make changes in Dockerfile(as needed to make changes in application) we have to build a new Docker image again from it. Or we can make change in the container we are running and commit it but that doesn’t reflect the Dockerfile which contains all the instructions to run an application say it for development or production purpose. Also building Docker image locally from Dockerfile makes us doing things like build, push and all manually. Automating build of Docker image from Dockerfile that is either on Github or Bitbucket is the only solution for this 🙂 .
In Short: We first create Dockerfile and then push it to Github or Bitbucket. After authenticating Github account with Dockerhub we choose repository on Dockerhub from Github that contains the Dockerfile. After that the Dockerfile triggers build which in result creates Docker image getting ready to pull.
I will share an example of making a CentOS image having Apache httpd server pre-installed.
First we need to create a Dockerfile which can be viewed here.
FROM centos:centos6 MAINTAINER trishnag <email@example.com> RUN yum -y update; yum clean all RUN yum -y install httpd RUN echo "This is our new Apache Test Site" >> /var/www/html/index.html EXPOSE 80 RUN echo "/sbin/service httpd start" >> /root/.bashrc
After doing so
- Go to DockerHub and Settings —> Linked Account —> Link your Github account.
- Create —> Create Automated Build —> Create Auto-build Github.
- Select the repository that contains Dockerfile of your application.
- Create followed by the Automating build process.
After the image is built successfully you will be able to see
Success in Build Details which indicates that the image is successfully built. The Image is now live on docker hub https://hub.docker.com/r/trishnag/centos6-apache.
Now we have to test the image to make sure whether Apache httpd server is actually pre-installed or not.
docker pull trishnag/centos6-apache #Pull the image from Dockerhub docker run -t -i -d trishnag/centos-apache /bin/bash #Run the container as daemon docker ps #Get the name of the container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8bcd1199bb8f trishnag/centos6-apache "/bin/bash" 2 minutes ago Up 2 minutes 80/tcp jovial_cray docker logs jovial_cray #To see What is happening on bash of container.Gives httpd status docker inspect jovial_cray | grep IPAddress #Shows the IPAddress curl 172.17.0.1 #curl the IPAddress we got This is our new Apache Test Site
We just have got the text that we echoed out to index.html. Hence we can finally come to conclusion that Apache httpd server has already been pre-installed in the image.
Now even if we commit changes to the Dockerfile on Github we really don’t have to worry about the image. The build process of Docker image starts automatically once any changes are committed to Dockerfile. When we pull the newly built image and run container we will be able to find those changes.
Automating build process really makes our life easier.