Sneak view into Docker for Web devs | Part-2

Welcome to this second section to learn about Docker for Web developers. In case, you are directly landing here, it’s recommended to first visit this page.

So what we have seen so far is that, the docker-engine sits on top of the host machine and is responsible for spawning multiple containers.

  • First Container runs on the port 80 hosted on Tomcat-7 and JDK 1.7.
  • Second Container runs on the port 80 hosted on Tomcat-8 and JDK 1.8.

Remember these two are virtual machines which means they are as good as a PC running inside a PC.

In this blog, we are going to look at details about use-case-2, as part of which, we shall be hosting the website on Nginx httpServer. Below is what we are going to do :-

Below is how the flow for this use-case would look like :-

Build-Ship-Run flow diagram

So this is going to be kind of simulation of “Build-Ship-Run” philosophy :-

  • A developer will develop an image & put it into a repository.
  • A QA engineer would get the image and deploy it into the respective QA environment.
  • Post QA signoff, devops engineer would get the image and deploy it to production.

So let’s take a look at what how this can be done.

Step #1 :- We have below as docker-file.

  • It means that, we want to download the “nginx ”Docker-Image with version as 1.14.
  • We can label to this image with our team or individual name.
  • We have also modified the configuration file and the same shall be put into the container’s specified directory-structure, once the container would come up.
  • We also copy the website/ folder structure into the directory “/usr/share/nginx/html/static-website” of container.

Following are the modifications being done to the configuration file of nginx and the same shall be replaced to the default configuration file of nginx of container :-

Step #2:- docker build This command will build the Docker image from aforementioned Dockerfile. --tag , -t calls out to have the Name and optionally a tag in the 'name:tag' format. Here, we have defined the name of our image as “my-nginx”.

Step #3:- docker images This command will show all top level images, their repository and tags, and their size. Pl note here that, we have just built an docker-image with name “my-nginx” and the same is being reflected in output of this command :-

Step #4:- docker run This command creates a container from a given image and starts the container. Pl note that “-d” option represents detached mode i.e. even after terminal is exited, still the container shall be running. Here, we shall be launching 2 different containers from the same underlying docker image :-

Launched first Container from Image.
Launched second Container from Image.

Step #5:- docker ps -a This command shows all the containers that we have currently :-

Displays all containers that we have currently.

Now, the above output indicates that, the container is running well and we can access it now. So, let’s head to our browser and try to access the 2 ports: 7777 & 7778. Recall that in previous steps, we exposed these 2 corresponding hardware ports, which are in-turn mapping to the port no. 80 of the respective container.

Yay ! our website is accessible from within the freshly launched container now.

Step #6:- docker exec -it <container_id>This command would help us to log-inside the docker container. Remember from our previous discussions that, our docker is also a virtual computer on the top of our actual computer. Let’s now log-into the container :-

Step #7:- Let’s now see the various processes running inside our container. Note that pid with 1, is that process, which was started as the part of the container being startup.

Let’s now explore the directory structure, which we copied from the DockerFile, during the container boot-up time :-

Step #8:- Basically by now, we have customised the original docker-image as we have installed the “ps” package on this image. We would now push this image to our dockerHub account. Let’s now login into our docker-hub account :-

Let’s create a repository named “nginx-website” now :-

Step #9:- Let’s now connect to our dockerhub’s account from our terminal, as we shall be pushing an image into the dockerhub using our account.

Step #10:- docker tag <currentimage>:<tag> <repository-name>/<image-name>:<tag>This command creates a new tag from our last image. This image is a snapshot of this image that you see here, but note that the ids are same for both of these images.

Step #11:- docker push <repository-name>/<image-name>:<tag>This command would enable us to push our home-brewed docker-image to the dockerhub.

Let’s now go ahead and verify, if this image did really reached to our dockerhub and we can see that, there do exists an image-tag here with version as 1.0

Step #12:- CleanUp :-docker stop <container_id> This command would stop both the containers that we have recently launched using the underlying image.

Step #12:- CleanUp :-docker rm <container_id> This command would remove both the containers that we have recently launched using the underlying image.

Step #12:- CleanUp :-docker rmi <image_id> This command would remove the docker-image that we have used, to launch the container.

Step #12:- Now since everything has been cleaned up, we can next, try to pull this image from dockerhub repository :-

Step #13:- Next, let’s try to see, if we have the appropriate images :-

Step #14:- Next, lets launch a fresh container from this recently pulled docker-image from dockerhub.

Step #14:- Let’s test it out, whether our static website is available now on the new port :-

That’s all for this blog. We shall see further things in next blog.

References :-

Software Engineer for Big Data distributed systems