Docker running Python code without copying the files
Tags: dockers, python, beginners, development
Published: 2024-06-10 11:52:37

---
title: Docker running Python code without copying the files
published: true
description: Docker running Python code without copying the scripts into the container and let container running
tags: dockers, python, beginner, development
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/disaibd14ijj8ew4227b.jpg 
# Use a ratio of 100:42 for best results.
# published_at: 2024-06-10 09:07 +0000
---

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 and the projects that we work as a software engineer not necessarily follow the same evolution, thus makes our life even more challenging if we're working in different projects. Nowadays most of the things related to docker, kubernetes or containers will focus on production purpose, but I remember year ago in the very beginnings of docker where the main idea is to solve the messy of different local environments(ow god, look at me using this elder sentence), well, this article is focusing on setting a simple environment where you can have you python code running using a docker and dont needing to worry about version on your local machine. Yes I don't like solutions like Pyenv, but this is my personal point of view ok?

That said, I want to have a Docker container that runs Python 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


Does anyone know how to display a local image in markdown? I don't want to set up a webserver for that.

I try the following in markdown, but it doesn't work:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4oshw2b18i6occshl7d.png)

1. **Install Docker**: Ensure that Docker is installed on your machine. You can download it from [Docker's official website](https://www.docker.com/get-started).

2. **Create a Directory for Your Python Scripts**: Create a directory on your local machine where you will store your Python scripts. For example:
   ```sh
   mkdir -p /Users/YOUR-USER/my-scripts
   ```

3. **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):
   ```dockerfile
# 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"]

   ```

4. **Build the Docker Image**: Navigate to the directory containing your Dockerfile and build the Docker image.
   ```sh
   docker build -t my-python-runner .
   ```

5. **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.
   ```sh
   docker run -d --name my-python-container -v /Users/YOUR-USER/my-scripts:/usr/src/app my-python-runner

   ```

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.


6. **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:

```sh
docker exec -it my-python-container python hello.py
```

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:

1. **Install Docker**: Make sure Docker is installed on your computer.
2. **Create a Scripts Directory**: Set up a local folder for your Python scripts.
3. **Write a Dockerfile**: Use this simple Dockerfile to create a Python environment:

   ```dockerfile
   FROM python:3.9
   WORKDIR /usr/src/app
   CMD ["tail", "-f", "/dev/null"]
   ```

4. **Build the Docker Image**: Navigate to the folder with the Dockerfile and run:

   ```sh
   docker build -t my-python-runner .
   ```

5. **Run the Docker Container**: Start the container and link your local scripts folder:

   ```sh
   docker run -d --name my-python-container -v /path/to/your/scripts:/usr/src/app my-python-runner
   ```

6. **Run Your Python Scripts**: Use this command to execute scripts in the container:

   ```sh
   docker exec -it my-python-container python your_script.py
   ```

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!