Docker file to create docker container

How to create docker file to create docker container

     Docker file can be used to create a docker container. Instead of using vanilla image from docker hub you can modify that image using docker file. In this example we will be downloading centos:cento6 image. Then we will modify the image and create a new image using docker file.

      These are the things we will be doing in this project:

     Download centos:centos6 image. Create a directory BUILD, switch to Build directory  and create a file Dockerfile. Inside dockerfile we will do following things. From centos:centos6 (which image to use). MAINTAINER (email of the person maintaining the file). We created a user dockeruser which we will be using for the container created using this image. RUN (commands you want to execute inside the image), we installed openssh-server httpd and sudo packages. We enabled sshd httpd to start at boot. We created a file called /var/www/html/index.html and wrote HEllo Linux CLass! (This will behave as our web page). We downloaded jdk8 RPM from oracle and installed using yum. Used visudo to give dockeruser sudo previlages. Exposed port 22 for sshd and 80 for httpd in order to be able to access ssh and http from outside. Defined default user as dockeruser, JAVA_HOME inside .bashrc for dockeruserand JAVA_BIN for all user. Restarted httpd and sshd.


[root@docker7 ~]# docker pull centos:centos6
centos6: Pulling from library/centos
Digest: sha256:b358c4a16ef77db3a07eaaaf62c707f51aa15bca820489392cc9d97046bc483a
Status: Image is up to date for centos:centos6
[root@docker7 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             b5e5ffb5cdea        4 weeks ago         194MB
centos              latest              5182e96772bf        4 weeks ago         200MB
[root@docker7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@docker7 ~]# mkdir BUILD/
mkdir: cannot create directory ‘BUILD/’: File exists
[root@docker7 ~]# cd BUILD/
[root@docker7 BUILD]# vi Dockerfile
From centos:centos6
RUN useradd -s /bin/bash dockeruser
RUN yum update -y
RUN yum install wget curl httpd net-tools openssh-server sudo mlocate telnet -y
RUN chkconfig sshd on
RUN service sshd start
RUN chkconfig httpd on
RUN service httpd start
RUN echo "HEllo Linux Class!" > /var/www/html/index.html
RUN cd ~ && wget --no-cookies --no-check-certificate --header "Cookie:; oraclelicense=accept-securebackup-cookie;" ""
RUN yum install -y ~/jdk-8u181-linux-x64.rpm
RUN echo "dockeruser        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
EXPOSE 22 80
USER dockeruser
RUN cd ~ && echo "export JAVA_HOME=/usr/java/jdk1.8.0/" /home/dockeruser/.bashrc
ENV JAVA_BIN /usr/java/jdk1.8.0/jdk/bin/
RUN sudo service httpd restart
RUN sudo service sshd restart
We will build docker image from Docker file using docker build command from directory where we created Dockerfile.Check newly built image with docker images.Now create a container, name it dockerfile, publish the exposed port 22 and 80, and start in daemonized  form.Check the started container using command docker ps -a.
     Note the ports>22/tcp,>80/tcp, sshd service in container is accessible through hostip:32779 and httpd service is accessible through hostip:32778
[root@docker7 BUILD]#  docker build -t centos6/dockerfile:v1 .
[root@docker7 BUILD]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos6/dockerfile   v1                  1c6607b574f4        34 seconds ago      868MB
centos               centos6             b5e5ffb5cdea        4 weeks ago         194MB
centos               latest              5182e96772bf        4 weeks ago         200MB
[root@docker7 BUILD]#  docker run --name dockerfile -P -itd centos6/dockerfile:v1
[root@docker7 BUILD]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS                                          NAMES
04d953fe5d38        centos6/dockerfile:v1   "/bin/bash"         5 seconds ago       Up 5 seconds>22/tcp,>80/tcp   dockerfile


Check the ipaddress of the running container using docker inspect command

[root@docker7 BUILD]#  docker inspect dockerfile | grep -i "ipaddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "",
            "IPAddress": "",

Use docker exec command to run commands inside running container.We will restart httpd and sshd service.We will check the status of httpd and sshd service

root@docker7 BUILD]#  docker exec -it dockerfile bash -c "sudo service httpd restart; sudo service sshd restart"
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using for ServerName
                                                           [  OK  ]
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[root@docker7 BUILD]#  docker exec -it dockerfile bash -c "sudo service httpd status; sudo service sshd status"
httpd (pid  1069) is running...
openssh-daemon (pid  1107) is running...


Now finally to verify httpd and sshd is running and is accisable using host ip


Leave a Reply

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