FileNotFoundError in custon docker python image

Question:

I have a python code in which I am opening a file and then extracting a particular data. Below is that code:

def get_mode(first, last):
    with open('/srv/config/mode.conf','r') as f:
        for line in f:
            if line.startswith(first):
                try:
                    start = line.index(first) + len(first)
                    end = line.index(last, start)
                    return line[start:end]
                except ValueError:
                    return "Default Mode"

This works fine when I execute the python code. But I want to run it as a docker so I have converted it into docker image using below command:

sudo docker build -t mydocker .

Above command successfully convert the python code into docker image. Then I run the image using

sudo docker run -it mydocker

but it gives error:

Traceback (most recent call last):
  File "./my_script.py", line 68, in <module>
   main()
  File "./my_script.py", line 52, in main
    mode = get_mode("user "," :")
  File "./my_script.py", line 15, in get_publish_string
    with open('/srv/config/mode.conf','r') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/srv/config/mode.conf'

How is that possible that if the python code is running fine then why docker image is giving error.

CONTENT OF DOCKERFILE:

FROM python:3

ADD my_script.py /

ADD mode.conf /srv/config

RUN pip3 install psutil

CMD [ "python3", "./my_script.py" ]
Asked By: S Andrew

||

Answers:

You need to add the config file to your image as well.

RUN mkdir -p /srv/config
ADD mode.conf /srv/config

or something like that.

Answered By: Hannu

If you installed docker-compose by downloading a binary with curl, try removing it

rm /usr/local/bin/docker-compose #replace the path with yours if it doesn't match

Then install docker-compose with apt

sudo apt install docker-compose

Reboot

sudo reboot

And try launching

docker-compose -v

to verify that everything is working well

Answered By: SomeGenericDev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.