How to add Python in Dockerfile?

Brown 38 Published: 12/16/2024

How to add Python in Dockerfile?

Here's the step-by-step guide on how to add Python in a Dockerfile:

Step 1: Create a new Dockerfile

Start by creating a new file named Dockerfile (no extension) with the following content:

# Use an official Python runtime as a base image

FROM python:3.9-slim

Set the working directory to /app

WORKDIR /app

Copy the current directory contents into the container at /app

COPY . /app

Install any needed packages specified in requirements.txt

RUN pip install --no-cache-dir -r requirements.txt

Make port 80 available from outside this container

EXPOSE 80

Define environment variable

ENV PYTHONDONTWRITEBYTECODE 1

Run app.py when the container launches

CMD ["python", "app.py"]

Step 2: Understand what each line does

FROM python:3.9-slim: This tells Docker to use an official Python runtime as a base image, in this case, version 3.9 with a slim size (smaller image size). WORKDIR /app: Sets the working directory in the container to /app. You can change this to any path you like. COPY . /app: Copies the current directory contents (i.e., your code) into the container at the specified directory (/app). RUN pip install --no-cache-dir -r requirements.txt: Installs any packages specified in a file named requirements.txt. This command is used to install dependencies required by your Python application. EXPOSE 80: Exposes port 80 from the container, so you can access your application through that port when running the container. ENV PYTHONDONTWRITEBYTECODE 1: Sets an environment variable to prevent Python from writing the .pyc files. This is useful for development purposes, as it allows you to modify your code without worrying about losing the changes. CMD ["python", "app.py"]: Specifies the command to run when the container starts. In this case, it runs the app.py file using the Python interpreter.

Step 3: Write your requirements.txt file

Create a new file named requirements.txt in the same directory as your Dockerfile. This file specifies the dependencies required by your application. For example:

flask==2.0.1

requests==2.25.1

pandas==1.3.5

Step 4: Build and run your container

From the terminal, navigate to the directory where you created your Dockerfile and requirements.txt file. Run the following command to build a new image based on your Dockerfile:

docker build -t my-python-app .

This command tells Docker to create an image named my-python-app from the instructions in your Dockerfile.

Once built, run the container with the following command:

docker run --name my-python-app -p 4000:80 my-python-app

This command starts a new container based on the my-python-app image and maps port 4000 on your local machine to port 80 in the container. You can now access your Python application by visiting http://localhost:4000 in your web browser.

That's it! With these steps, you've successfully added Python to a Dockerfile and created a container that runs your Python application.

Docker python tutorial pdf

Here's a comprehensive Docker Python tutorial in PDF format that covers the basics of containerization and how to use it with Python:

Docker Python Tutorial

Table of Contents

What is Docker?

Docker is an open-source platform for developing, shipping, and running applications in containers. Containers are lightweight and portable, allowing you to run your application on any system that supports Docker without worrying about compatibility issues.

Why Use Docker with Python?

Python is a popular programming language used extensively in web development, data analysis, machine learning, and more. By using Docker with Python, you can:

Run your Python applications independently of the host operating system and environment. Ensure consistent dependencies and versions across environments. Easily deploy and scale your applications. Simplify testing and debugging by creating isolated test environments.

Prerequisites

To follow this tutorial, you'll need:

A basic understanding of Python programming. Docker installed on your machine (available for Windows, macOS, and Linux). A code editor or IDE of your choice.

Getting Started with Docker

Install Docker: Follow the installation instructions for your operating system at Docker.com. Verify Docker Installation: Open a terminal or command prompt and type docker --version. You should see the version of Docker installed on your machine. Create a New Directory for Your Project: Create a new directory to store your project files.

Building Your First Python Container

Create a Dockerfile: Inside your project directory, create a new file named Dockerfile. This file will contain the instructions for building your container.
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

FROM: Use an official Python image as a base image. WORKDIR: Set the working directory within the container. COPY: Copy your requirements.txt file and project files into the container. RUN: Install dependencies using pip. CMD: Define the default command to run when you start a new container.

Building Your Container

Run docker build: Navigate to your project directory in the terminal or command prompt and type docker build -t my-python-app .. This will create a new image with the name my-python-app.

Running Your Container

Run docker run: Type docker run -p 8000:8000 my-python-app to start a new container from your built image.

Interacting with Your Running Container

Access the Container: You can access the container using docker exec -it my-python-app bash. This will open a shell within the container. Run Python Code: You can run Python code within the container by typing python app.py or any other Python script you have.

Stopping and Removing Containers

Stop a Running Container: Type docker stop my-python-app to stop the running container. Remove a Container: Type docker rm my-python-app to remove the container.

Conclusion

In this tutorial, we covered the basics of Docker and how to use it with Python. You learned how to:

Install and verify Docker installation. Create a new directory for your project and build a Dockerfile. Build your first Python container using docker build. Run and interact with your container using docker run and docker exec. Stop and remove containers.

Resources

For more information on Docker, visit Docker.com. For tutorials and resources specific to Python and Docker, check out Python.org and Docker.com.

Download the PDF

You can download this tutorial as a PDF by visiting GitHub and downloading the docker-python-tutorial.pdf file.