Installing Apache and Python inside Docker
Introduction
In this article i am going you to show the way how to install apache web server and python inside docker container.
Before going further this is my setup I using RedHat(RHEL) 8 as a docker host and the my container is going to be ubuntu:latest (i am going to use the latest tag so the version of ubuntu image may happen to change but this not going to affect our result that much).
Installing apache web server
Step 1:- Pulling ubuntu
Before proceeding any further use docker pull ubuntu
command to pull the latest ubuntu image from the docker hub
when we didn’t specify any tag its always use tag name latest in our case any version of ubuntu is fine
Step 2:- Starting Ubuntu
use docker run --name myweb -it ubuntu:latest
command
run:- the command used to launch container from docker image
— name:- option used to give user friendly name to container
it:- made from two option i and t tell docker to give terminal an switch user control inside docker
now we are inside docker
Step 3:- Installing apache web server
Now use apt-get install apache2
command apache web server
apt-get:- one of the most common used to install package in ubuntu
apache2:- name of apache web server package for ubuntu
in our case this command fail due to repository data is still not update(this command not necessary always fail) use apt-get update
command to rebuild repository data
now run apt-get install apache2
command again
enter y
for yes is just asking whether you want to install or not again
Step 4:- Starting apache web server
Docker image generally didn’t come with systmectl command (command used to manage process) but we didn’t need it for now.
we can directly start apache web server if we known the name of the program that responsible for starting apache web server and we can find that program in cd /usr/sbin
go that directory if you use ls -l you find three program named apache
apachectl:- is a link to apache2ctl
apache2ctl:- is simple bash script that call apache2 binary various function based on the parameter passed
apache2:- is a precompiled binary that responsible for doing all the task
in our case we are going to use apache2ctl for all the task
now let use apache2ctl start
command to start the webserver
note:- of you are not inside /usr/sbin folder use /usr/sbin/apache2ctl
command
to confirm whether our webserver is running or not use netstat -tnlp
command
(if its show netstat no command found use apt-get install net-tools
command to install netstat)
now we have apache web server running without any problem
to verify it use curl --url 0.0.0.0:80
command
if its show curl no command found use apt-get install curl
command to install curl
this is the html code of the welcome page of the apache web server it means our web server is running fine
Step 5:- A problem in whole setup
There is a single problem in this whole setup the whole web server is limited to docker it means we can’t use our site anywhere not when in docker host (RHEL) to verify this use ctrl +p + q
to again docker host this shortcut put container in background
now use netstat -tnlp
command to find open port inside docker host
even if you use every address present in the list there in no apache web server running in any address
Step 6:- Understanding problem
The problem is docker is container is running in close environment it means no outside process is able to contact it we can solve this problem by using ip of docker container
Step 7:- Finding IP of container
now we are not currently inside the container use docker attach myweb
command to go inside the container
now type ifconfig
command to show the current ip of container
(if it said ifconfig no command found use apt-get install net-tools
to install ifconfig)
now my ip is 172.17.0.2
now open any browser in docker host and go to 172.17.2.0:80 to see the welcome page of the apache web server
Step 8:- Stop and Starting container again
When ever your host restart again your all running container stop and you have to start again this so let do this scenario use docker stop myweb
command to stop docker
to confirm use docker ps
command this show all the currently running container
use docker start myweb
command to start container again
Step 9:- Finding IP again
Well sometimes it become very problematic to attach the docker run the command and detach from docker again and again for bypassing this thing we can use docker exec myweb ifconfig
command run the command in container and its gives output back to host
exec:- this run command inside the docker and give back output to the user (you can only run one command at a time)
myweb:- container name
ifconfig:- command name
for by chance my ip is same now if you go back to the ip again its gives error
because our service is not permanent (means as soon as we start container its web server also)
Step 10:- Making web server permanent
There is problem with apache2ctl start is this command that it only start web server but did not do anything about temp file created by apache web server this temp file said webserver is running but that not the actually case.
That problem is not come we use graceful parameter instead of start
now use docker attach myweb
command to use container
now use vim /root/.bashrc
command
if its show no command vim found you can use vi /root/.bashrc
if it vi not found you can use apt-get install vim
command to install vim
now write /usr/sbin/apache2ctl graceful
any where in the file
press esc key the write :wq to save the work
Now stop and start docker again to verify that web server is running perfectly
Installing Python inside container
Well this is very easy just use apt-get install python3
y:- yes when we pass y option its automatically consider as yes and dids’t ask for confirmation
Committing changes
To commit change use docker commit myweb ser:1.0
command
use docker images command to see all images
now you can see our myweb become image name ser you can use this image to lunch a may container you want
Conclusion
Thank for everyone to reading my article till end if you have any doubt please comment if you have any suggestion please mail all comment both positive and negative is more than welcomed
Contact Detail
LinkeDin [https://www.linkedin.com/in/suyash-garg-50245b1b7]
Additional Tags
#webserver #docker #ubuntu #vimaldaga #righteducation #educationredefine #rightmentor #worldrecordholder #linuxworld #makingindiafutureready #righeudcation #arthbylw