Table of Contents
- Introduction
- Prerequisites
- Docker Installation
- Creating a Docker Container
- Running Python Code in a Docker Container
- Sharing Files between Host and Container
- Conclusion
Introduction
In this tutorial, we will learn how to use Python with Docker to isolate our development environment. Docker allows us to package our code and its dependencies into a container, which can be run consistently across different platforms. This provides a hassle-free way to distribute and share our Python applications.
By the end of this tutorial, you will be able to:
- Install Docker on your machine
- Create a Docker container for Python development
- Run Python code inside a Docker container
- Share files between your host machine and the Docker container
Let’s get started!
Prerequisites
Before proceeding, make sure you have the following:
- Basic understanding of Python programming language
- Docker installed on your machine
Docker Installation
To install Docker, follow the instructions specific to your operating system:
- Windows: Visit the official Docker website (https://www.docker.com/products/docker-desktop) and download Docker Desktop for Windows.
- Mac: Visit the official Docker website (https://www.docker.com/products/docker-desktop) and download Docker Desktop for Mac.
- Linux: Docker installation instructions can vary depending on the distribution you are using. Refer to the official Docker documentation (https://docs.docker.com/engine/install/) for detailed instructions.
Once Docker is installed, ensure it is running by opening a terminal and running the following command:
bash
docker version
If Docker is properly installed, you should see the version information displayed.
Creating a Docker Container
Now that Docker is installed, let’s create a Docker container for Python development.
-
Open a terminal or command prompt.
- Create a new directory for your project and navigate into it:
mkdir python-docker-example cd python-docker-example
- Create a new file named
Dockerfile
(without any file extension) in the project directory. This file will define the configuration for our Docker container:touch Dockerfile
- Open the
Dockerfile
in a text editor and add the following content:# Use the official Python base image FROM python:3.9 # Set the working directory inside the container WORKDIR /app # Copy the requirements file COPY requirements.txt . # Install the Python dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy the rest of the application's code COPY . . # Specify the command to run when the container starts CMD [ "python", "app.py" ]
Let’s understand what each of these lines does:
-
FROM python:3.9
: This line specifies the base image for our Docker container. In this case, we are using the official Python image with version 3.9. -
WORKDIR /app
: Sets the working directory inside the container to/app
. This is the directory where our application code will be copied. -
COPY requirements.txt .
: Copies therequirements.txt
file from the host machine to the container’s working directory. -
RUN pip install --no-cache-dir -r requirements.txt
: Installs the Python dependencies specified inrequirements.txt
inside the container. -
COPY . .
: Copies the rest of the application’s code from the host machine to the container’s working directory. -
CMD [ "python", "app.py" ]
: Specifies the command to run when the container starts. In this case, it will run theapp.py
file using the Python interpreter.
-
Save and close the
Dockerfile
. - Create a new file named
requirements.txt
in the project directory. This file should contain the necessary Python dependencies for your application. For example, if your application uses Flask, you can add the following line torequirements.txt
:flask==2.0.1
- Create a simple Python file named
app.py
in the project directory. This file will serve as our example Python code. You can add any Python code you want into this file.
Congratulations! You have created the necessary files for Docker containerization. In the next section, we will build and run the Docker container.
Note: Make sure the
Dockerfile
,requirements.txt
, andapp.py
files are located in the same directory.
Running Python Code in a Docker Container
Now that we have set up the Docker container, let’s build and run it.
-
Open a terminal or command prompt.
- Navigate to the project directory:
cd python-docker-example
- Build the Docker image using the
docker build
command. The-t
flag allows us to tag our image with a name (e.g.,python-docker
):docker build -t python-docker .
- Once the image is built successfully, we can run a Docker container based on that image using the
docker run
command:docker run -it python-docker
If everything goes well, you should see the output of your Python code running inside the Docker container.
Congratulations! You have successfully run your Python code inside a Docker container. In the next section, we will learn how to share files between the host machine and the Docker container.
Sharing Files between Host and Container
Often, we need to share files between our host machine (your computer) and the Docker container. Docker provides a way to mount directories from the host machine as volumes inside the container.
Let’s learn how to do that.
-
Stop the running Docker container by pressing
Ctrl+C
in the terminal. - Modify the
docker run
command to mount a directory from the host machine as a volume inside the container. Replace<host_directory>
with the absolute path to a directory on your host machine (e.g.,/Users/yourname/project-files
):docker run -it -v <host_directory>:/app python-docker
- Run the
docker run
command again. Now, any changes made to files in the<host_directory>
on your host machine will be reflected inside the Docker container, and vice versa.
This allows for easy development and testing within the container while still being able to use your favorite text editors or IDEs on your host machine.
Conclusion
In this tutorial, we have learned how to use Python with Docker for environment isolation. We started by installing Docker and setting up a Docker container for Python development. We then ran our Python code inside the Docker container and explored how to share files between the host machine and the container.
By leveraging Docker, we can ensure consistent and reliable execution of our Python code across different environments and operating systems. Docker provides a powerful tool for isolating our development environment and simplifying the distribution of our Python applications.
Happy Dockerizing!