0

I built a docker image using Dockerfile with Python and some libraries inside (no my project code inside). In my local work dir, there are some scripts to be run on the docker. So, here what I did

$ cd /path/to/my_workdir
$ docker run -it --name test -v `pwd`:`pwd` -w `pwd` my/code:test python src/main.py --config=test --results-dir=/home/me/Results

The command python src/main.py --config=test --results-dir=/home/me/Results is what I want to run inside the Docker container.

However, it returns,

/home/docker/miniconda3/bin/python: /home/docker/miniconda3/bin/python: cannot execute binary file

How can I fix it and run my code?

Here is my Dockerfile

FROM nvidia/cuda:10.1-cudnn7-runtime-ubuntu18.04

MAINTAINER Me <[email protected]>

RUN apt update -yq && \
    apt install -yq curl wget unzip git vim cmake sudo

RUN adduser --disabled-password --gecos '' docker && \
    adduser docker sudo && \
    echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

USER docker

WORKDIR /home/docker/

RUN chmod a+rwx /home/docker/ && \
    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
    bash Miniconda3-latest-Linux-x86_64.sh -b && rm Miniconda3-latest-Linux-x86_64.sh 

ENV PATH /home/docker/miniconda3/bin:$PATH

Run pip install absl-py==0.5.0 atomicwrites==1.2.1 attrs==18.2.0 certifi==2018.8.24 chardet==3.0.4 cycler==0.10.0 docopt==0.6.2 enum34==1.1.6 future==0.16.0 idna==2.7 imageio==2.4.1 jsonpickle==1.2 kiwisolver==1.0.1 matplotlib==3.0.0 mock==2.0.0 more-itertools==4.3.0 mpyq==0.2.5 munch==2.3.2 numpy==1.15.2 pathlib2==2.3.2 pbr==4.3.0 Pillow==5.3.0 pluggy==0.7.1 portpicker==1.2.0 probscale==0.2.3 protobuf==3.6.1 py==1.6.0 pygame==1.9.4 pyparsing==2.2.2 pysc2==3.0.0 pytest==3.8.2 python-dateutil==2.7.3 PyYAML==3.13 requests==2.19.1 s2clientprotocol==4.10.1.75800.0 sacred==0.8.1 scipy==1.1.0 six==1.11.0 sk-video==1.1.10 snakeviz==1.0.0 tensorboard-logger==0.1.0 torch==0.4.1 torchvision==0.2.1 tornado==5.1.1 urllib3==1.23

USER docker
ENTRYPOINT ["/bin/bash"]
2
  • 1
    It seems like you'd get an equivalent and simpler setup by creating an ordinary Python virtual environment and running the pip install line to set it up; you can put that list of packages into a requirements.txt file to make it repeatable. What are you gaining by using Docker here? Commented Jul 6, 2020 at 11:07
  • Yes, your comment is right. When running the command "docker run -it --name test -v pwd:pwd -w pwd my/code:test python src/main.py --config=test --results-dir=/home/me/Results", I got /home/docker/miniconda3/bin/python: /home/docker/miniconda3/bin/python: cannot execute binary file Commented Jul 6, 2020 at 11:13

2 Answers 2

1

Try making the file executable before running it.

as John mentioned to do in the dockerfile

FROM python:latest

COPY src/main.py /usr/local/share/

RUN chmod +x /usr/local/share/src/main.py      #<-**--- just add this also

# I have some doubts about the pathing 

CMD ["/usr/local/share/src/main.py", "--config=test --results-dir=/home/me/Results"]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, there are many python files, and sometimes I need to change my python files, so I think cp all my files to images is not practical. Instead, I want to run the task in command line. How can I fix my current issue?
docker run -it --name test -v pwd:pwd -w pwd my/code:test python chmod +x "src/main.py && src/main.py --config=test --results-dir=/home/me/Results" Try running two commands sequentially like this.
Thanks, it is a bit weird, by replace the python commands with pwd, it also returns /bin/pwd: /bin/pwd: cannot execute binary file
Use your suggestion. I got /bin/bash: python: No such file or directory
0

You can run a python script in docker by adding this to your docker file:

FROM python:latest

COPY src/main.py /usr/local/share/

CMD ["src/main.py", "--config=test --results-dir=/home/me/Results"]

1 Comment

Thanks, this is a good method. But how can I fix my current issue? There are many command-line arguments to be passed to python, I prefer to run the code in the command line,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.