Configure Webserver and Python Interpreter using Container - Docker
Anudeep Nalla
Opensource Contributer | Platform Engineer | EX-NPCI | RHCA Level III | OpenShift | CEPH | CK{S,A,AD} | 3x Microsoft Certified | AWS CSA | Rancher | Nirmata | DevOps | Ansible | Jenkins | DevSecOps | Kyverno | Rook-Ceph
What is Docker?
A Docker container is an open source software development platform. Its main benefit is to package applications in containers, allowing them to be portable to any system running a Linux or Windows operating system (OS). ... While it is a major player in the container field, Docker is only one form of container technology.
In this article, I am going to show some tasks performing on the Docker Container which will help you to understand more about container technology
Tasks:
1) Configure Webserver on the top of Docker Container
2) Setting up Python Interpreter and running Python Code on Docker Container
Installing Docker: As I am using Redhat 8, so I need to first configure yum and create docker repo for installing the Docker.
For this, go to the location, /etc/yum.repos.d and then create a file for configure yum and docker
Check the yum is properly configured or not using,†yum repolistâ€
Its working fine!
To install Docker-ce software, run
yum install docker-ce --nobest -y
I have already installed it before.
Finally, Docker installed successfully!
Start the Docker services using,
Systemctl start docker
Systemctl enable docker
--> We require ISO file for installing any operating system. In Docker, we have docker images for installing any container.
--> Go to docker hub and check for images
For Downloading any docker image, run
docker pull image_name:tag
To check the docker images
docker images
Here, I’m using centos.
1) Let us configure the Webserver on the docker container…
--> Launching a new container
For launching a new docker container, command is
docker run -it -p local_port:docker_port -v volume:/directory --name os_name image:tag
Here
[ --name ] -> we use to assign a name to our container.
[ -v ] -> We use this to mount our local directory to docker container to store our files permanently. So, if somehow the docker container, our data will never be lost.
[ -p ] -> We use this to bind local port to docker port. So, this we can expose our container to outside world.
[ -it ] -> gives an interactive terminal to container, to perform any commands.
You can see, as I run the command, it gives a new interactive terminal for OS
To check, the container is successfully launched or not
We can also use â€docker ps†in new Redhat terminal
--> Let’s configure Webserver now.
Step1: Install httpd software using
yum install httpd -y
Step2: Put your code in /var/www/html
Using AWS CloudFront with docker!
What is Aws S3 & CloudFront?
In my html page, I have used the image present in Aws s3, To display that image I have used CloudFront URL
To Know more, visit my previous article:
https://www.dhirubhai.net/pulse/create-high-availability-architecture-aws-cli-anudeep-nalla/
Step3: Start the httpd services
Here, If we try to start services using systemctl command, it will be fail because docker doesn’t provide systemctl command.
So, Here we directly run the exec file location to run httpd server.
/usr/bin/httpd
Now let’s run IP:88/doc.html in the browser
But, the httpd service is temporary, After restart we have to start again.
To make the Service permanent
Open /root/.bashrc and write this 2 commands
rm -f /var/run/httpd/*
/usr/sbin/httpd
Let’s check, after restarting the container.
I’m using docker start and docker attach command for restart.
Its Works perfect!
Now we can save this docker container as image for testing/production environment.
Our Webserver is configured, we have to commit the image to save
For this, we have command
docker commit os_name image:tag
Our image is ready to use!
2) Setting up Python Interpreter and running Python Code on Docker Container…
--> For this we can normally launch Docker container and install python3 to setup python interpreter.
Now you can write your code here.
--> But I want to create my own docker image, which launches python interpreter when I launch that image.
--> To create my own image, we have to use the concept of Dockerfile, By writing some code, we can make our docker images.
FROM centos:latest RUN yum install python3 -y
CMD ["/usr/bin/python3"]
Save this code in a file named Dockerfile
Build the image using.
docker build -t mypy:v1 .
Lets run the image
--> To run the python file in the docker container, For this I have creating one new image
FROM centos:latest RUN yum install python3 -y WORKDIR /pycode ENTRYPOINT ["/usr/bin/python3"]
CMD ["--version"]
Save the code in Dockerfile and run this command to build.
docker build -t name:tag .
Now Lets check the container is working or not
It works!
Thankyou