I never liked the idea to messy all installations in our host computer to run any kind of programming language. Mainly because they are in constant change and evolution while the projects that we work as a software engineer not necessarily follow the same pace, thus makes our life even more challenging if we’re working in different projects 🥲.
Nowadays most of the things related to Docker, Kubernetes or overall containers will focus on production purpose, but I remember years ago in the very beginnings of container, of course, using docker where the main idea were to solve the messy of different local environments…oh no, look at me using this elder sentence… well, whatever, this article is focusing on setting a simple environment where you can have your python code running using a docker and don’t needing to worry about version on your local machine.
Yes I don’t like solutions tools versions management like Pyenv, because they messy our machine with tons of dependencies and installations…
That said, what do I want?
- have a Docker container that runs Python
- being able to run scripts from a local directory without copying the scripts into the container
For that we will use Docker volumes to mount the local directory into the container. This setup allows you to edit the scripts on your local machine and have the changes immediately available inside the Docker container.
It is very simple, let me show to you, step by step, the idea is to the installation be something like this:
-
Install Docker: Ensure that Docker is installed on your machine. You can download it from Docker’s official website.
-
Create a Directory for Your Python Scripts: Create a directory on your local machine where you will store your Python scripts. For example:
<span>mkdir</span> <span>-p</span> /Users/YOUR-USER/my-scripts<span>mkdir</span> <span>-p</span> /Users/YOUR-USER/my-scriptsmkdir -p /Users/YOUR-USER/my-scripts
Enter fullscreen mode Exit fullscreen mode
- Write a Dockerfile: Create a Dockerfile that defines the Python environment. For simplicity, you can use the official Python image from Docker Hub. Create a file named
Dockerfile
in a directory of your choice (not necessarily where your scripts are):
<span># Use the official Python image from the Docker Hub</span><span>FROM</span><span> python:3.9</span><span># Set the working directory in the container</span><span>WORKDIR</span><span> /usr/src/app</span><span># Install any dependencies if necessary (optional)</span><span># COPY requirements.txt ./</span><span># RUN pip install --no-cache-dir -r requirements.txt</span><span># Command to keep the container running</span><span>CMD</span><span> ["tail", "-f", "/dev/null"]</span><span># Use the official Python image from the Docker Hub</span> <span>FROM</span><span> python:3.9</span> <span># Set the working directory in the container</span> <span>WORKDIR</span><span> /usr/src/app</span> <span># Install any dependencies if necessary (optional)</span> <span># COPY requirements.txt ./</span> <span># RUN pip install --no-cache-dir -r requirements.txt</span> <span># Command to keep the container running</span> <span>CMD</span><span> ["tail", "-f", "/dev/null"]</span># Use the official Python image from the Docker Hub FROM python:3.9 # Set the working directory in the container WORKDIR /usr/src/app # Install any dependencies if necessary (optional) # COPY requirements.txt ./ # RUN pip install --no-cache-dir -r requirements.txt # Command to keep the container running CMD ["tail", "-f", "/dev/null"]
Enter fullscreen mode Exit fullscreen mode
- Build the Docker Image: Navigate to the directory containing your Dockerfile and build the Docker image.
docker build <span>-t</span> my-python-runner <span>.</span>docker build <span>-t</span> my-python-runner <span>.</span>docker build -t my-python-runner .
Enter fullscreen mode Exit fullscreen mode
- Run the Docker Container with a Volume Mount: Use the
docker run
command with the-v
option to mount your local directory into the container.
docker run <span>-d</span> <span>--name</span> my-python-container <span>-v</span> /Users/YOUR-USER/my-scripts:/usr/src/app my-python-runnerdocker run <span>-d</span> <span>--name</span> my-python-container <span>-v</span> /Users/YOUR-USER/my-scripts:/usr/src/app my-python-runnerdocker run -d --name my-python-container -v /Users/YOUR-USER/my-scripts:/usr/src/app my-python-runner
Enter fullscreen mode Exit fullscreen mode
About the command:
-d: Runs the container in detached mode.
–name my-python-container: Names the container for easier reference.
-v /Users/YOUR-USER/my-scripts:/usr/src/app: Mounts the local directory /Users/YOUR-USER/my-scripts to the container’s /usr/src/app directory.
- Executing python code: To run different Python scripts in the running container, use the docker exec command. This command allows you to run commands inside a running container.
For example, to run hello.py:
docker <span>exec</span> <span>-it</span> my-python-container python hello.pydocker <span>exec</span> <span>-it</span> my-python-container python hello.pydocker exec -it my-python-container python hello.py
Enter fullscreen mode Exit fullscreen mode
Now you can run all python code you want!
Conclusion
Managing multiple programming environments on a host computer can get messy, especially when different projects require different setups. Docker offers a clean solution by using containers to keep everything isolated and organized.
Originally designed to simplify local development, Docker can still help us avoid installing multiple versions of Python or other tools directly on our machines. Instead, we can run Python scripts in a Docker container and keep our local environment tidy.
The recap on what we did or our nutshell version:
- Install Docker: Make sure Docker is installed on your computer.
- Create a Scripts Directory: Set up a local folder for your Python scripts.
- Write a Dockerfile: Use this simple Dockerfile to create a Python environment:
FROM python:3.9WORKDIR /usr/src/appCMD ["tail", "-f", "/dev/null"]FROM python:3.9 WORKDIR /usr/src/app CMD ["tail", "-f", "/dev/null"]FROM python:3.9 WORKDIR /usr/src/app CMD ["tail", "-f", "/dev/null"]
Enter fullscreen mode Exit fullscreen mode
- Build the Docker Image: Navigate to the folder with the Dockerfile and run:
docker build <span>-t</span> my-python-runner <span>.</span>docker build <span>-t</span> my-python-runner <span>.</span>docker build -t my-python-runner .
Enter fullscreen mode Exit fullscreen mode
- Run the Docker Container: Start the container and link your local scripts folder:
docker run <span>-d</span> <span>--name</span> my-python-container <span>-v</span> /path/to/your/scripts:/usr/src/app my-python-runnerdocker run <span>-d</span> <span>--name</span> my-python-container <span>-v</span> /path/to/your/scripts:/usr/src/app my-python-runnerdocker run -d --name my-python-container -v /path/to/your/scripts:/usr/src/app my-python-runner
Enter fullscreen mode Exit fullscreen mode
- Run Your Python Scripts: Use this command to execute scripts in the container:
docker <span>exec</span> <span>-it</span> my-python-container python your_script.pydocker <span>exec</span> <span>-it</span> my-python-container python your_script.pydocker exec -it my-python-container python your_script.py
Enter fullscreen mode Exit fullscreen mode
I love docker, besides the powerful that it offers on production you can easily manage and run your everything without worrying about installing and configuring on your local machine. This keeps your development environment clean and hassle-free. Enjoy coding!
暂无评论内容