[tutorial_ml:06] Containerizing with Docker for an Integrated Development Environment

Section 006

Rafa Felix
3 min readDec 8, 2023

Introduction

Containerising your Python project with Docker offers a seamless and consistent development environment. In this section, we’ll cover how to wrap your Python package in a Docker container, install it natively within the container, and use Jupyter Lab for interactive testing and experimentation.

Creating a Dockerfile: Begin by creating a Dockerfile in the root of your project. This file defines the environment and commands to build your Docker image.

Define the Base Image: Start with a base image that includes Python. For example

FROM python:3.8-slim

Install Dependencies: Copy your requirements file and install the dependencies:

COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt

Install Your Package: Copy your project files into the container and install your package:

COPY . /app/
WORKDIR /app
RUN pip install -e .

Set Up Jupyter Lab (Optional): If you want to include Jupyter Lab for interactive testing:

RUN pip install jupyterlab
CMD [“jupyter”, “lab”, “ — ip=0.0.0.0”, “ — allow-root”, “ — no-browser”]

Create your docker image: The -t tutorial-project tags your image, making it easier to reference.

docker build -t tutorial-project .

Running the Docker Container: Run your container with the following command:

docker run -p 8888:8888 tutorial-project

Accessing Jupyter Lab: If you’ve set up Jupyter Lab, it will be accessible at http://localhost:8888. The terminal output will provide a token to log in.

The usage of docker will guarantee yoy, feature such as
Consistency: Docker ensures that your project runs in the same environment, regardless of where it’s deployed, reducing the “it works on my machine” problem.
Isolation: Running your project in a container isolates it from the host system, minimising conflicts between different projects’ dependencies.
Reproducible: Docker containers can be shared and run by other team members or in production environments, ensuring everyone is working with the same setup.

By containerisation your Python project with Docker, you create a consistent, isolated, and reproducible development environment. Incorporating tools like Jupyter Lab within your Docker setup further enhances the development experience, providing a powerful platform for interactive testing and exploration.

--

--

Rafa Felix
Rafa Felix

Written by Rafa Felix

Machine Learning | Computer Vision | NLP | Vision and Language

No responses yet