How to provide a file to a Docker container at runtime and ensure it is accessible during container execution?

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 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
</code>
<code># 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 </code>
# 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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>docker build -t prostwp/automation .
docker run -it --rm --name automation prostwp/automation:latest
</code>
<code>docker build -t prostwp/automation . docker run -it --rm --name automation prostwp/automation:latest </code>
docker build -t prostwp/automation .
docker run -it --rm --name automation prostwp/automation:latest

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

New contributor

prostwp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật