- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to run Gunicorn on Docker?
In this article, we are going to learn about how to run Gunicorn on Docker.
Before creating the docker container with the gunicorn functionalities, we must know some of the basics of web servers and gunicorn.
Introduction to Gunicorn
Let us suppose we have a web server that shows the output of a python code to the users. This web server will execute the python script each time a request came to it, this will cause it to restart, overload, and delay (heavy delay for huge scripts) in response. So the real problem here we have is a static server but we needed a dynamic solution. Gunicorn is the dynamic solution for the above-mentioned problem statement.
The above image explains the problem statement and the below image shows the structure of the solution.
There is a very famous infrastructure that uses Nginx, gunicorn, and Django together to provide dynamic functionalities. Nginx solves the load balancing and gunicorn solves the static problem.
Deploy on the Docker Container
Creating Django projects is out of scope for this article, we can create a minimalistic app and store that python app on the docker container. This docker container will have the gunicorn installed on it and we will launch the python app on the container.
Algorithm(Steps)
Create a Python application.
Create a requirements file.
Create a dockerfile.
Perform Containerization.
Checking the server for Output.
Step 1: Creat Python application
Create a simple python application object that outputs “Hello Tutorialspoint! ”
Example
def app(environ, start_response): data = b'Hello, Tutorialspoint!
' status = '200 OK' response_headers = [ ('Content-type', 'text/plain'), ('Content-Length', str(len(data))) ] start_response(status, response_headers) return iter([data])
Save this file as myapp.py name.
Step 2: Create a requirements file
python3 gunicorn
Save this as requirements.txt.
Step 3: Create a dockerfile
This dockerfile will create a docker container image fully functioning with python and gunicorn.
Save the below file as Dockerfile with no extension.
Example
FROM python WORKDIR /app COPY ./myapp.py ./ COPY ./requirements.txt ./ RUN pip install --upgrade pip --no-cache-dir RUN pip install –r /app/requirements.txt --no-cache-dir CMD [“gunicorn”,”-w” “4”,”myapp:app”,”--bind” “0.0.0.0:8000”]
Step 4: Build docker image from dockerfile
#docker build –t unicornimage .
Step 5: Perform Containerization
Create a docker container from the unicornimage image.
Example
#docker run –itd --name unicorncontainer –p 8000:8000 unicornimage
Step 6: Check if the server is accessible on the host machine
Go to the browser and search localhost:8000 to see the output.
Similarly to create a Django project add django in the requirements.txt file with the gunicorn and you are ready to go.