02 - Docker Advanced Usage - How to dockerize an application

we'll create a standalone app container using Dockerfile. Meaning we'll create a Docker container that can be pushed to the repositories and pulled by anybody else in the world which will run a pre-installed and pre-configured app perfectly out of the box every time.
  Configure the app template with a Dockerfile 
Let's take a look at our Dockerfile. First, we pull the base source from Ubuntu using the FROM method. Then as with many other Dockerfiles we install first updating the repo using the app-get update command and then install Apache 2 which is going to be our web server and a couple of other pieces of software that I'd like to have whenever I'm running a container nano which is a terminal text editor in curl which is a way of viewing web pages from a command line. Then, again using the RUN method we will set Apache to start on boot.
 
This is a little bit of a hack but we will echo the words service Apache 2 start which is the command you'd use from the command line to start Apache and we will append to them the two right arrows will append to the existing file etc/bash.bashrc on the container. When the container boots, it will read bach.bashrc and execute all the commands that it finds.
 
The last command it will find will be service Apache 2 start. That will start Apache 2 running and that will give us an operating web service. Finally, we will populate the folder var/www/html which was created when Apache was installed and the contents of which will be available publicly as web browser services. We will populate that directory with the index.html file that is found in the Dockerfile root on the host.
 
What is the Dockerfile root? That is the directory from which Dockerfile was built. So in this particular case I just created an index.html file and I saved it to the Docker file root. When Dockerfile is run and our image is built, whatever happens to be called index.html in that directory will be copied to the var/www/html directory on the container. This of course could be dynamic, that is if you update the index.html file or any other file you point to on your host every time you build or wherever you happen to build this container based on this Dockerfile, whatever is there, whatever is index.html will be available to the container at this address.
  How to assign a static IP address to Docker containers 
We have to make sure that our container has an IP address that we can reliably and predictably access from the outside. Docker assigns dynamic IP addresses to its containers, often in the 176.17.0 range. But since they're dynamic we can never predict exactly what they'll be. It might be 176.17.0.2 or .3 or .4. We don't know, that's not good enough for us because we have to point our client browsers directly to the appropriate address.
 
So how do you create a static IP address for our container? One way is to create a special interface for the Docker container on the host computer. That is you go on the host to etc/network/interfaces, the file interfaces, and add this new interface auto docker0 which means we're creating an interface called docker0 which happens to be the name that the container will be given when eventually it's run. Iface docker0 inet static because the address we're going to give it should be its permanent address. The address in my case will be 176.17 which is in the network, in the range that Docker is willing to service .0.251. I'm giving it the address 251. It could have been anywhere between one, or really between two and 254 but I chose 251 just to keep it out of the way so there's less likely to be a conflict. The netmask must be 255.255.0.0. Finally on the host let's run sudo ifup docker0 to bring the interface up. Don't worry about that error message.
 
There's no container yet that exists for it to be effective. But once the app is running, this interface will be running along with it. Let's go to the directory where our Docker file currently lives and let's build the Apache A image from the Docker file in the local directory.
 
Let's actually run our new image, "sudo docker run--net=host" which means the networking configuration of this container will equal, will really rely on that of the host, -d which means we'll be running this but detached so it will run in the background even though our shell session will remain a no error access, -t Apache -a.
 
And let's see what happens. It looks like the app is running. To be sure, however, let's actually go to 176.17.0.251 which was the static IP address we gave to this container and see if it works. That is what should have been displayed based on the contents of the index.html file that I created. Everything seems to be working.

Comments

Popular posts from this blog

Cloud Computing in simple

How to Write an Effective Design Document

Bookmark