As the use of containerization grows in popularity, Docker has emerged as a leader in containerization technology. By using Docker, developers can easily package their applications with all the necessary dependencies and libraries, making it easier to deploy their applications across different environments. In this article, we will go over the steps involved in deploying a Python application on Docker.
Prerequisites:
Before we begin, make sure you have the following installed on your system:- Docker
- Python 3.x
- A text editor or IDE of your choice
Step 1: Creating the Python Application
For this tutorial, we will be using a simple Flask application as an example. In your preferred text editor, create a new Python file called app.py
and copy the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Save the file.
Step 2: Building the Docker Image
Now that we have our Python application, we need to create a Docker image that can run the application. In your terminal, navigate to the directory where your app.py
file is located and create a new file called Dockerfile
. Copy the following code into the Dockerfile
:
FROM python:3.8-alpine
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./app.py" ]
The above Dockerfile
specifies that we will be using the official Python 3.8 image as our base image. It also sets our working directory to /app
, copies the requirements.txt
file to the working directory, installs the dependencies, copies the entire application code to the container, and sets the command to run the app.py
file.
Next, we need to create a requirements.txt
file that specifies the dependencies required for our application. Create a new file called requirements.txt
in the same directory as app.py
and copy the following code:
Flask==2.0.2
This file specifies that our application requires Flask version 2.0.2.
Now, we are ready to build our Docker image. In your terminal, navigate to the directory where your Dockerfile
and requirements.txt
files are located and run the following command:
docker build -t my-python-app .
This command will build a Docker image with the tag my-python-app
.
Step 3: Running the Docker Container
Now that we have built our Docker image, we can run it in a container. In your terminal, run the following command:
docker run -p 5000:5000 my-python-app
This command will start a new container and map port 5000 on your local machine to port 5000 in the container.
To access your application, open your web browser and go to http://localhost:5000/
. You should see the message "Hello, World!" displayed.
Congratulations! You have successfully deployed your Python application on Docker.
Additional Tips:
- If you make changes to your application code, you will need to rebuild the Docker image and run a new container.
- You can use environment variables in your
Dockerfile
to set configuration options for your application. - You can use Docker Compose to manage multiple containers that make up your application stack.
Related Searches and Questions asked:
That's it for this post. Keep practicing and have fun. Leave your comments if any.
0 Comments