- Portable
- Scalable
- Flexible
- Lightweight
Dockerfile Example
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 oc@gmail.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.
FROM centos RUN yum install httpd -y COPY index.html /var/www/html/ CMD [“/usr/sbin/httpd”,” -D”,” FOREGROUND”] EXPOSE 80
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=”oc@gmail.com” 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
docker images
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.
Docker Cheatsheet
Wrapping up:- By dockerfile we can automate docker container deployment and can use the same dockerfile to create multiple containers in different hosts.
Thanks a lot for sharing this, it is really simple and neat article and gave me a clear picture from where should I start about Docker and which area we need to be concentrate more.
Good as an idea-likely impractical in practice, never mind eh?