I’m working on a project where I need to provide a text file to a Docker container at runtime. This file must be accessible and used by the container during its execution. The bottom line is that the python code processes the file that appears in the downloads folder, after which the code manipulates and deletes it and waits for a new one to appear(I use macOs)
Here is a brief description of what I am trying to achieve:
Provide the file during docker startup:
The file pasted_text.txt located in my local macOS downloads directory (/Users/myusername/Downloads/pasted_text.txt ).
I want to mount this file in a container so that it can be accessed at /usr/src/app/Downloads/pasted_text.txt inside the container.
The container should run a Python script that reads the contents pasted_text.txt and prints it out.
Here’s what I’ve tried so far:
Using Dockerfile to copy a file:
I understand that I can use the copy instructions in the Dockerfile, but this will require rebuilding the image every time the file is changed.
Mounting a file using the –mount option:
I tried to use the –mount option in the docker startup command to bind and mount the file, but ran into problems related to the container not recognizing the file.
# Use Python 3.12 base image
FROM python:3.12-slim
# Set the working directory
WORKDIR /usr/src/app
# Install dependencies
RUN apt-get update && apt-get install -y
curl
build-essential
libevdev-dev
xvfb
python3-tk
python3-dev
&& apt-get clean
&& rm -rf /var/lib/apt/lists/*
# Install Node.js and npm
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &&
apt-get install -y nodejs &&
apt-get clean && rm -rf /var/lib/apt/lists/*
# Copy dependency files
COPY requirements.txt ./
COPY package.json ./
COPY package-lock.json ./
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Install Node.js dependencies
RUN npm install
# Copy the rest of the project files
COPY . .
# Run the script with xvfb
CMD xvfb-run --auto-servernum --server-args='-screen 0 1024x768x24' python automation_execute_file.py
docker build -t prostwp/automation .
docker run -it --rm --name automation prostwp/automation:latest
docker run -it --rm --name automation
--mount type=bind,source=/Users/myusername/Downloads/pasted_text.txt,target=/usr/src/app/Downloads/pasted_text.txt
prostwp/automation:latest
prostwp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4