Kidnapping Machine Learning in Docker Containers

I am a Student, who finds beauty in simple things. I like to teach sometimes.
Search for a command to run...

I am a Student, who finds beauty in simple things. I like to teach sometimes.
No comments yet. Be the first to comment.
There is a distinct heaviness that descends when life proceeds smoothly on the surface. Externally, everything may be stable, yet the desire to die can persist not because of tragedy, but because of a realization regarding the future. If the destinat...
"I must not fear abstraction. Abstraction is the mind-killer. Abstraction is the little-death that brings total obliteration. I will face my abstraction. I will permit it to pass over me and through me. And when it has gone past I will turn the inner...
Ever wondered how the Python or JavaScript code you write actually makes your computer's fans spin up? How do abstract commands like print("Hello, World!") get turned into physical actions? The magic lies in a fundamental, deeply interconnected relat...
I'll speed through setting up an ASIC synthesis flow for the Ibex RISC-V core using entirely open-source tools. Tools Python 3.12.8 (for environment management) Yosys (logic synthesis) sv2v (SystemVerilog to Verilog conversion) OpenSTA (static ti...
Imagine this scene: A dimly lit room, humming with the quiet thrum of advanced technology. Three alien scientists are hunched over a console, staring intently at a string of data flashing across a screen: 0101010100... Alien Scientist #1: "It isn't r...
Docker has become an essential tool for developers, especially in the field of machine learning. It allows you to create isolated environments, ensuring that your projects run consistently across different systems. In this blog, we'll walk through the process of installing Docker on Linux, creating a Docker container using a Dockerfile, and setting up a machine learning project with pyenv, venv, and docker-compose.
Before we start, ensure that your Linux system is up-to-date:
sudo apt update && sudo apt upgrade -y
Install Required Packages:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Add Docker’s Official GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add Docker Repository:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker Engine:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
Verify Docker Installation:
sudo docker --version
Manage Docker as a Non-root User:
sudo usermod -aG docker $USER
newgrp docker
Now, you can run Docker commands without sudo.
A Dockerfile is a script that contains instructions on how to build a Docker image. For a machine learning project, we’ll install pyenv for Python version management and venv for virtual environments.
Create a Project Directory:
mkdir ml-project
cd ml-project
Create a Dockerfile:
touch Dockerfile
Edit the Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYENV_ROOT=/root/.pyenv \
PATH="/root/.pyenv/shims:/root/.pyenv/bin:$PATH"
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
curl \
git \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
wget \
&& rm -rf /var/lib/apt/lists/*
# Install pyenv
RUN curl https://pyenv.run | bash
# Install a specific Python version using pyenv
RUN pyenv install 3.9.7 && pyenv global 3.9.7
# Create a virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install Python dependencies
COPY requirements.txt .
RUN pip install --upgrade pip && pip install -r requirements.txt
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . .
# Command to run on container start
CMD ["python", "your_script.py"]
Create a requirements.txt File:
touch requirements.txt
Add your Python dependencies to this file, e.g.:
numpy
pandas
scikit-learn
tensorflow
Build the Image:
docker build -t ml-project .
Run the Container:
docker run -it --rm ml-project
This will start the container and run the script specified in the CMD instruction.
Docker Compose is a tool for defining and running multi-container Docker applications. It’s particularly useful for machine learning projects where you might need to run multiple services (e.g., a Jupyter Notebook server, a database, etc.).
docker-compose.yml FileCreate a docker-compose.yml File:
touch docker-compose.yml
Edit the docker-compose.yml File:
services:
ml-service:
image: ml-project
build: .
volumes:
- .:/app
ports:
- "8888:8888"
command: jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root
This configuration will:
Build the Docker image using the Dockerfile.
Mount the current directory to /app inside the container.
Expose port 8888 for Jupyter Notebook.
Run Docker Compose:
docker-compose up
You can now access the Jupyter Notebook by navigating to http://localhost:8888 in your browser.
In this blog, we walked through the process of setting up Docker for a machine learning project on Linux. We installed Docker, created a Dockerfile with pyenv and venv, built and ran the container, and used Docker Compose for orchestration. This setup ensures that your machine learning projects are reproducible and can be easily shared with others.
Docker is a powerful tool that can significantly streamline your development workflow, especially in the field of machine learning. By containerizing your projects, you can avoid the common "it works on my machine" problem and focus on building great models.