This article will serve as a very basic hands on tutorial of Dockerfile. I will be assuming that you are already aware of docker. If not, you can read about docker in one of my other posts here.
What are we trying to do here?
Run Apache http server inside a docker container and access it from host machine.
What is the purpose?
Familiarize ourself with Dockerfile. I have done the same exercise using docker commandline here.
Where am i running everything?
I am running everything on my Mac, We will first be creating a CentOS VM using Vagrant. Within CentOS VM, we will be installing docker and then run apache http server inside a docker container. (You don't have to necessarily run docker in VM, you can directly run it on your machine. If you decide to run it directly on your machine, please ignore Step 1 below)
You should have Oracle Virtual Box and Vagrant installed on your machine (only if you decide to run docker inside VM).
Let's get started.
Step 1: Create a CentOS VM
Inside a directory on your machine (~/vagarnt in my case), create a file named Vagrantfile with following contents
# -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "centos65" config.vm.box_url = "https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :public_network config.ssh.forward_agent = true end
I am not going to explain What Vagrant and Vagrantfile does, if you are not aware of them, please read about them. Also create a directory called htdocs parallel to Vagrantfile and inside htdocs directory create a file index.html with following contents.
This file is being server by http server running inside docker container
Once Vagrantfile and htdocs directory is ready. Execute following commands
vagrant up # Will start VM, you may be asked to select network interface vagrant ssh # Once VM is started, ssh into the VM
Step 2: Install docker
Run following commands to install docker
sudo yum -y update # Update Installed packages sudo yum -y install docker-io # Install Docker sudo service docker start # Start Docker
Step 3: Create first custom container image
Create a directory called docker (or any name of your choice) and add a file Dockerfile in it with following contents
# use the centos base image FROM centos #RUN command is equivalent to the docker commands: docker run image command + docker commit container_id, Where image would be replaced automatically with the current image, and container_id would be the result of the previous RUN instruction. RUN yum -y update RUN yum -y install python-setuptools RUN easy_install supervisor RUN mkdir -p /var/log/supervisor RUN yum -y install which RUN yum -y install git
Once Dockefile is ready, execute following command to build your first custom container image
sudo docker build -t custom/base . #This will create an image called custom/base based on DockerFile in current directory
Once the process is finished, If you execute sudo docker images now, you will see a new container that above command committed and it can now be used as base to start a new container. REPOSITORY TAG IMAGE ID CREATED custom/base latest 05b6cecd370b 2 minutes ago centos 6.4 539c0211cd76 10 months ago centos latest 539c0211cd76 10 months ago...
Step 4: Install apache
Create a file supervisord.conf in current directory with following contents. You can read more about supervisor here.
[supervisord] nodaemon=true [program:httpd] command=/bin/bash -c "exec /usr/sbin/apachectl start"
Change the contents of Dockefile to
FROM custom/base RUN yum -y install httpd ADD supervisord.conf /etc/supervisord.conf EXPOSE 22 80 CMD ["/usr/bin/supervisord"]
Run the docker build again
sudo docker build -t custom/httpd . #This will create an image called custom/httpd
I am sure you have already realized that we have just created a reusable container image with http server installed on it. This idea can be used for anything and everything. You can create container for each component of your technology stack and they can be used on developer machine as well as production environment.
Step 5: Running http server
sudo docker run -p 80:80 -v /vagrant/htdocs:/var/www/html -t -i custom/httpd # -v will Mount a volume from VM to the container which was also shared from host to Vagrant VM. # -p forward VM port 80 to container port 80; VM port 80 is mapped to host port 8080 in Vagrantfile
Step 8: Open a browser and test
Open a browser and hit http://localhost:8080