How I Used Docker to Create a Python Dev Environment

Kenny Brast
5 min readMar 7, 2023

--

Why is Docker so special?

Docker is one of the most popular open-source containerization platforms. It offers greater security, easy management, and increased deployment speed. It facilitates developers to great extent by making the process of application conversion to containers, hassle-free.

Why do we use Docker for Python?

Supporting various languages, including Python, Docker gives you a unified image format to distribute your applications across different host systems and cloud services. You can deliver your application in one piece with all the required dependencies (included in an image) ready to run.

The basic objectives

This article will cover the creation of a Python Boto3 Development Environment using Docker.

We will build Docker file to support the Python AWS SDK (Boto3) using the latest Python image from Docker Hub. Three GitHub repositoroes will be cloned locally. Three Docker containers will be created, with each container having a bind mount to one of the three repo directories. Each container will be logged in to verify access to each repo directory.

The tools of choice are Visual Studio Code (VSCode) and Docker Desktop.

Here are the steps to accomplish what we’ve described

1. Create a Dockerfile for Boto3 using the official Python image

First, create a new directory for your project and create a file named Dockerfile inside it. The contents of the file should be as follows:

FROM python:latest
RUN pip install boto3

This will create a Docker image based on the latest Python image and install the Boto3 library inside it.

2. Build the Docker image

Next, navigate to the directory where you saved the Dockerfile and run the following command to build the image:

docker build -t my-boto3-image .

Image creation output as shown in VSCode:

New image creation verified in Docker Desktop:

This will build the Docker image and tag it with the name my-boto3-image.

3. Download three GitHub repositories to local host

Now, clone three GitHub repositories of your choosing to your local machine using the following commands:

git clone https://github.com/username/repo1.git
git clone https://github.com/username/repo2.git
git clone https://github.com/username/repo3.git

Replace username and repo1, repo2, repo3 with the actual usernames and repository names. Here are the actual commands run from VSCode:

4. Create three containers with bind mount to a local repository

Next, create three Docker containers and bind mount each of the three repositories to them. You can do this using the following commands:

docker run -dt --name container1 -v /path/to/repo1:/app my-boto3-image /bin/bash
docker run -dt --name container2 -v /path/to/repo2:/app my-boto3-image /bin/bash
docker run -dt --name container3 -v /path/to/repo3:/app my-boto3-image /bin/bash

Replace /path/to/repo1, /path/to/repo2, and /path/to/repo3 with the actual paths to the cloned repositories on your local machine:

docker run -dt --name container1 -v ~/docker/kbapp/Pythonista:/app my-boto3-image /bin/bash
docker run -dt --name container2 -v ~/docker/kbapp/red_getting_bread_python:/app my-boto3-image /bin/bash
docker run -dt --name container3 -v ~/docker/kbapp/Python_cert_exp:/app my-boto3-image /bin/bash

The running containers can be verified from Docker Desktop:

5. Log into each container and verify access to each repository directory

Finally, log into each container and verify that you can access the respective repository directory using the following commands:

docker exec -it container1 /bin/bash
cd /app
ls

docker exec -it container2 /bin/bash
cd /app
ls

docker exec -it container3 /bin/bash
cd /app
ls

These commands will log you into each container, navigate to the /app directory (which is the bind-mounted directory containing the cloned repository), and list the contents of the directory. If you see the contents of each repository, it means you have successfully bind-mounted them to the containers and can access them from within the containers. Here are the commands in VSCode to launch & access each container to verify the repo:

Best practices for containerizing Python applications with Docker:

  1. Use explicit and deterministic Docker base image tags for containerized Python applications.
  2. Separate dependencies from source code.
  3. Use Python WSGI for production.
  4. Run containers with least possible privilege (and never as root).
  5. Handle unhealthy states of your application.
  6. Find and fix security vulnerabilities Python Docker application images.

Conclusion

Docker is a useful containerization tool for spinning up isolated, reproducible application environments. It continues to be a popular development tool for Python developers. The three main development burdens which Docker can help overcome are packaging, distributing and running applications. Thanks for reading!

Click here to connect with me on LinkedIn.

Best of luck on your continuing journey to Level Up In Tech!

--

--

Kenny Brast
Kenny Brast

No responses yet