Configuring Web Server in Docker Inside a Cloud Instance
How to configure a Web Server in a Docker Container, which is launched in a Cloud Instance.
Hello Geeks, I Hope you are here to learn about Web-Server and Docker, so let's get started with the blog…
We will explain how we can launch a container technology inside a cloud instance and configure a Web-Server inside the blog's same container.
The task we are going to complete:
- Launch and start a docker container in EC2 instance
- Configuring HTTPD Server on Docker Container
- Setting up Python Interpreter and running Python Code on Docker Container.
Here is basic information about the technology we will be using in the blog.
Container Technology: Container technology is a method of packaging an application to be run with isolated dependencies. They have fundamentally altered the development of software today due to their compartmentalization of a computer system.
Docker: Docker is a platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated and bundle their own software, libraries, and configuration files; they can communicate through well-defined channels.
Apache HTTP WebServer: The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under Apache License 2.0. Apache is developed and maintained by an open community of developers under the Apache Software Foundation's auspices.
Launch and start a docker container in EC2 instance
First, we launched our EC2 instance, and we connected to it using Putty. And now we are inside the system. As the system is new, we don’t have any software or program inside. We have to install docker inside our OS at first. To install the docker, we need to use yum, and we don’t have any docker configuration file, so we have to configure yum first.
We went to the path /etc/yum.repos.d/.
And we used the dnf command to download docker software into our system.
dnf config-master --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
As we can see using the command “yum list | grep docker,” that the docker software is available in our system.
Now we have to install the software, and we can do that using the command “yum install docker-ce.” And it will start to install the downloaded software.
We can check if docker is properly installed or not using the command:
Now we have to start the docker service in our system, and we can do that using the command:
docker systemctl start docker // to start
docker systemctl enable docker // to enable it on every start
To check the status of the docker service is running or now we can use the command:
systemctl status docker
Now our docker service is active and running, we can create a container inside the docker, and to create a container, we need an image of an OS, and there is a big community called docker hub. And here, we will be using a Centos Image, and we use the command:
docker pull centos
In the above picture, we have pulled a centos image, and now it is saved in our docker. Now to launch a container with that image, we have to use the command:
docker run -it --name <any name> centos:latest
Here the -i is the option to get an interactive OS, and -t is an OS terminal, and the name is given to that container using the -name option, and at last, we specified our image name. As seen in the above image, our new system has been launched, and it just took one second and one command, and we can see the IP of that system, and now the terminal belongs to the docker container launched with Centos image.
Now we can try running the date command, we get the output because the date is a pre-installed program in that OS image, but when we try to use the “ifconfig” command, we cannot get the output because we don't have the software which provides ifconfig so we searched using the command “ yum whatprovides ifconfig” and we came to know it was “net-tools.”
We installed net-tools in our system using the yum command; “yum install net-tools.”
When we try using the command “ifconfig” command, we get the output, and this system has its own network card with its own IP address.
Configuring HTTPD Server on Docker Container
After launching a docker container in our base OS, we will try to configure the HTTPD web server in the container. And to configure the container, we need httpd software, which we can install using the command.
yum install httpd
And after installing, we get a directory “/var/www/html” in our root directory. Now we create an html file inside the directory so that we can access it from the web.
As we can see in the above image, we went inside the directory and created an html file naming site.html.
Now we to start the webserver, we usually do the command “systemctl start httpd,” and it is used to change the setting files in our system and start the service. We are still launching our web server from the docker container, so a service running in the base OS container cannot use the systemctl command.
Here in the docker container, we have to start the service by using the command. And to check if it is running, we can use the command:
/usr/sbin/httpd //to startnetstat -tnlp //to check
As we can see in the above image, our service started running in port 80. Now, check if the webserver is running or use the curl command, which is a protocol to transfer data using the network.
- First, we did curl inside our Docker container to our base OS IP from the container.
- Then we use Ctrl + p + q to let the container running in the background and came to the base OS.
- As we cannot use GUI in our AWS instance, we used the same curl command from the base OS, and we saw our website in the base OS also.
We cannot access is from outside the base OS because container has been assigned a private IP by the host and only host or the container can access it so we cannot use our webserver outside the container and base OS.
So we can see we have successfully launched our web server from inside the docker container.
Setting up Python Interpreter and running Python Code on Docker Container.
Setting a python interpreter and running it is a straightforward task. We need to follow a few steps. As this container does not have any software, so we first install a python interpreter. We will install python 3 using the command.
yum install python
After installing, we wrote a small code in Python using the vi command. And we ran the python code with the command “python3 python.py”.
And to stop our container and come to base OS, we can use the command “exit.”
Finally, we can conclude the blog as we completed all the tasks we planned to install and launch the docker container in EC2 and configure the HTTPD server in the docker. And at last, installing a Python interpreter in the docker container.
Thank you for reading the blog, and please do suggest to me some ideas for improvement. Your suggestions will really motivate me.