Dockerfile is a sequence of executable command which run while building the image in a normal script which has instruction to build the docker base image from the given arguments. Dockerfile help to automate base Docker image configuration process from start to end otherwise once docker builds from the base image we need to set up and configure the docker based on our requirement. Dockerfile should be created with “Dockerfile” name only and can call with docker build command while creating a new custom image.
So before digging more into Dockerfile we should first familiar with some important Docker I terminology which is most widely used while creating docker images.
Docker:- It a platform design for developer and sysadmin to quickly build, deploy, test, and run the application as required. Docker container will deploy on top of your Linux kernel, So far only Linux kernel only supporting docker container.
Docker container is more famous because of its various features such as :
Docker Images: Docker image is an executable package which includes everything needed to run the application. In short, it’s an image to build an operating system for your application on top of the existing kernel. By running the docker image we can build docker container.
ADD/ COPY:- This command has two argument source and destination. It copies the new file from given path or URL and placed to the destination.
CMD/ RUN:- The main function of this command to execute the command.
ENV:- Define the environment variable. ENV take input in the form of key-value pairs formate. (like JAVA environment variable).
EXPOSE:- This command will expose the application port to the outside world. (like Apache post 8080,80) but make sure the same port should not open from host side where docker application installed.
VOLUME:- This command will allow creating a volume outside of the container (host server) and attach to the docker application, So by accident, if docker container deletes we can save our application data and later reattach to another docker container.
WORKDIR:- This command changes the default directory to the working directory where the different set of commands like CMD, RUN.
ENTRYPOINT:- Allows you to configure a container that will run as an executable.
So finally we know basic terminology and commands which can help to create dockerfile and build docker image.
Example 1:- Install the HTTP package via docket file into centos base image.
#HTTP web Image # define base image path FROM centos # Provide author name MAINTAINER email@example.com RUN yum install –y httpd CMD [“echo”, ” Web Image created”]
As per the dockerfile code, it will pull latest centos base image from docker repo and once download complete it will run HTTP package and run the command to show “Web Image created”
Example 2:- Install Apache server and copy an index.html file from the host location to Docker container expose port 80 and start the httpd services. Make sure you have already having index.html file exist in your host location.
In this code, centos image downloaded from docker repo, install httpd package, copy an index.html file from the host server to docker image and finally run the httpd services and expose port 80 to access the web application outside of container http://x.x.x.x:80
Example 3:- Install MongoDB from MongoDB repo create the default directory and expose port.
First, create a file name mongo.repo in the host and add below the line.
# base image set to centos FROM centos # MongoDB installation Instructions Docs # Ref: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ # copy the Mongo repository from host to docker image COPY mongo.repo /etc/yum.repos.d/ # Update the yum repository RUN yum update all # Install MongoDB package RUN yum install MongoDB-org -y # mount volume VOLUME [“/mnt/mongo_storage”, “/data”] # create a directory for MongoDB RUN mkdir -p /data/MongoDB # Expose port EXPOSE 27017 # (MongoDB)Port to execute the entrypoint CMD ["--port 27017"] # Set default container command ENTRYPOINT usr/bin/mongod
Above code installing MongoDB on top of centos image and provide external VOLUME from host to container and EXPOSE port.
Example 4:- Create a CentOS docker image and copy some shell script from Host to container under container /tmp working directory provide executable permission and execute the script.
cat script.sh echo "hello docker" > /tmp/myfile.txt
FROM centos WORKDIR /tmp ADD script.sh /tmp/script.sh RUN chmod -v +x /tmp/script.sh RUN ./script.sh
In above code script.sh file move from the host server to image and execute the script accordingly.
Example 5: Pull Jenkins latest image from Docker repo and build a custom image with 4GB limited memory and 300 count request to handle the traffic, make a log folder expose port 8080 to outside access the Jenkins page.
FROM Jenkins:latest LABEL maintainer=”firstname.lastname@example.org” USER root ENV JAVA_OPTS="-Xmx4096m" ENV JENKINS_OPTS=" --handlerCountMax=200" RUN mkdir /var/log/jenkins-log EXPOSE 8080
Above code download Jenkins latest image from docker repo and limit the memory to 4GB and limit 200 web application connection and create Jenkins log directory and expose port 8080 to access the Jenkins from outside the host.
By default docker build command will take the input from Dockerfile.
Docker build from dockerfile
docker build -t <name of your image> .
once docker container running we can test the docker application by login the container or in case of web application we can try on the browser by typing hostname and EXPOSE port link x.x.x.x:8080
Once docker image created from dockerfile we need to check the image
This command show existing images under your host.
docker run -it <container-image-name> /bin/bash
Above command will start the container based on the image provide and login shell.
We can delete the docker container by executing docker rm container-id and delete an image by executing a docker rmi image-name command.
Wrapping up:- By dockerfile we can automate docker container deployment and can use the same dockerfile to create multiple containers in different hosts.