How to Launch Container within Container?
In this article we will see how we can use the Concepts of Docker to achieve Containerization within Container. To launch another container within a running container is quite a tedious task but in this article I will show you how easily you can achieve this by applying right concepts.
In this article I will be using few concepts of docker like Dockerfile, mounting volume, mounting port and few basic docker commands.
Before starting with the container we need to make sure that Docker is installed and configured in our system. If docker is not configured then you can use my automated script which I made for installing and configuring docker from my GitHub Repository.
After setting up with docker now we need launch a container so that we can launch another container inside it to achieve Containerization within container. There are two ways to launch container.
- By using docker pull command
- By using Dockerfile
In this article I will be using docker file concept to launch a centos container with Jenkins installed along with some of the basic packages like git, sudo, etc installed. Here is the dockerfile code which I have used for installing centos.
Now after creating docker file, we need to build an image by using that docker file. For building an image, we can use the command,
- docker build -t docker-within-container:v1.0 Dockerfile
After creating the image using Dockerfile we can launch it using different methods. Since we want to achieve containerization in this article we will be mounting docker socket while launching the container. To launch the container use the following command,
In this command you can see I have mounted port 1234 of my base OS ie, RHEL 8 to my container port 8080. I have mounted this because as we all know that jenkins runs on 8080 port and jenkins is being installed on the centos. So now we can run jenkins by using my base OS IPAddress:1234.
Now by using Jenkins we can do a lot of stuffs. I have tried to achieve containerization within conatainer by using some of the docker commands but before launching another container within this container we need to install and configure docker inside the container. We can do it manually by running docker exec commands for running docker installation commands but I have Made it automated by using Jenkins. Here is the commands which you can write to execute from jenkins to install and configure docker automatically.
This is one of the approach that we can do to launch container within container by using Jenkins just to automate the tasks. Another way would be going inside the shell of one container and the installing and configuring docker inside the container but to achieve containerization within container we will need to mount docker socket for that task also.
The advantages of mounting docker socket during launching of container is that we can monitor and view all those container which are running inside the container from our base OS. In my case I am using RHEL 8 as my base os so by the following command you can see all the containers running at the time.
docker container ls
Here in the image you can see that we have successfully launched the another container within container and since we have launched a web server we can cross verify it by running on the url. If it gives the webpage then it is working fine or else you might have made some mistake.
This is the default web page of httpd server when launched. You can launch you complete website by applying mounting concept or by using jenkins also you can completely launch you website automatically, but in that also you will need to mount the complete folder in which your website files are there to /usr/local/apache2/htdocs to launch your own website.
- docker run -d -it -P --privileged -p 1234:8080 -v /:/host -v /var/run/docker.sock:/var/run/docker.sock --name task2 docker-within-container:v1.0
- docker attach task2
In this command you can see I have mounted port 1234 of my base OS ie, RHEL 8 to my container port 8080. I have mounted this because as we all know that jenkins runs on 8080 port and jenkins is being installed on the centos. So now we can run jenkins by using my base OS IPAddress:1234.
Now by using Jenkins we can do a lot of stuffs. I have tried to achieve containerization within conatainer by using some of the docker commands but before launching another container within this container we need to install and configure docker inside the container. We can do it manually by running docker exec commands for running docker installation commands but I have Made it automated by using Jenkins. Here is the commands which you can write to execute from jenkins to install and configure docker automatically.
The advantages of mounting docker socket during launching of container is that we can monitor and view all those container which are running inside the container from our base OS. In my case I am using RHEL 8 as my base os so by the following command you can see all the containers running at the time.
docker container ls
Here in the image you can see that we have successfully launched the another container within container and since we have launched a web server we can cross verify it by running on the url. If it gives the webpage then it is working fine or else you might have made some mistake.
This is the default web page of httpd server when launched. You can launch you complete website by applying mounting concept or by using jenkins also you can completely launch you website automatically, but in that also you will need to mount the complete folder in which your website files are there to /usr/local/apache2/htdocs to launch your own website.
Outcome of the Article
So from this article we can conclude that docker can be used for several purposes. Firstly in this task I have launched jenkins as soon as my container is launched. As like jenkins we can launch other things also like Wordpress, mysql, etc as soon as docker is launched.
Second thing that we can conclude is that we can use networking concepts of docker for launching another docker-container within a container by mounting volumes and also port numbers for accessing containers easily from base os.
We can also conclude from the article that we can use these concepts of docker for launching our website that is present in a nested container but by using networking concepts of docker we can easily access those sites by using IP Address of base os.
Soon I will be uploading the image in my docker hub profile so that you can use it by pulling my conatiner image and ease your task.
Note: httpd might not work with everyone, so you can use other image also and also I have mounted one of my base os dir with container so that container can easily access website folders.
So I hope you all might have understood the power of docker and the some of the concepts of docker that we can use to solve some of the great use cases of industry.
Comments
Post a Comment