How to utilize a local file in a docker container?

I am creating a docker container that has a script that will run and give some output but in order to do so it needs to use the information from the .env file. However, the catch is this file changes frequently so it cannot be inside the container when built. I am aware of mounting and volumes but new to docker and having trouble. Here is my

Dockerfile

FROM python:3.8
ADD main.py .
RUN pip install requests python-dotenv
CMD [ "python", "./main.py" ]

I have built this doing

docker build -t dataset .

Now I have the issue of needing to use the .env file and tried to follow the documentation and came up with this to run it

docker run -v /Users/alexrobey/Documents/github/datasets:/tmp datasets 

The dockerfile and main.py are on the root directory and I have created a /tmp folder that has the .env file in it. Is this the right way to do everything? I am getting an application error that makes me believe I have done something the wrong way. Any help is appreciated.

1

1 Answer

If your program can read directly environment variables you can use the --env-file argument with docker run :

docker run --env-file <path_to_env_file> ...

Or perhaps you can use --mount argument to mount your temporary file.

docker run --mount source=/<source_path>,target=<target_path> ... (cf : )

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like