Reputation: 67
I created a Python application that receives images from an email message and stores it locally under a folder called Images. It also writes some config data to dict.txt. When I run the application without Docker (using Pycharm IDE), this works. However, this expected behaviour does not occur when I use docker.
ImageLink: Directory structure (Pycharm IDE)
Directory structure (container):
IntermediateService
├─── Dockerfile
├─── requirements.txt
└─── src
└─── emailAccountA.py
└─── emailAccountB.py
└─── main.py
└─── dict.txt
└─── Images
Dockerfile:
# set base image (host OS)
FROM python:3.8.5
# set the working directory in the container
WORKDIR /code
# copy the dependencies file to the working directory
COPY requirements.txt .
# install dependencies
RUN pip --default-timeout=100000 install -r requirements.txt
# copy the content of the local src directory to the working directory
COPY src/ .
# command to run on container start
CMD [ "python", "./main.py" ]
I run the following docker commands:
docker build -t intermediateservice .
docker run intermediateservice
The container builds and runs without any issue, just does not write to dict.txt and save the downloaded images to the Images folder (both textfile and folder are always empty). What needs to be done to resolve this issue?
Upvotes: 1
Views: 613
Reputation: 305
Containers are stateless so all data is lost when the container stops. Additionally, writing to a file inside the container isn't very practical.
In this case, it's best to mount a host folder into the container when launching docker run
. For example, this mounts an images
sub-directory into the container's /code/images
directory:
-v $PWD/images:/code/images
($PWD
isn't valid on Windows, so you need the full path, e.g. /c/myproject/images/
).
I wrote a Docker for Web Developers book and video course because I couldn't find good tutorials for beginners which showed how to create local development environments. It'll help. There's a 50% Black Friday sale now on.
Upvotes: 1
Reputation: 1399
As @john-gordon said it writes to the filesystem inside the container, which is lost when the container exits. You need to mount your folder inside the container when running it:
docker run -v $(pwd)/Images:/code/Images intermediateservice
Upvotes: 2