python-3.xdocker

Docker: files missing after build


I'm trying to build a docker container that runs a Python script. I want the code to be cloned from git when I build the image. I'm using this docker file as a base and added the following BEFORE the first line:

FROM debian:buster-slim AS intermediate

RUN apt-get update
RUN apt-get install -y git

ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa

RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan [git hostname] >> /root/.ssh/known_hosts

RUN git clone git@...../myApp.git

... then added the following directly after the first line:

# Copy only the repo from the intermediate image
COPY --from=intermediate /myApp /myApp

... then at the end I added this to install some dependencies:

RUN set -ex; \
    apt-get update; \
    apt-get install -y gcc g++ unixodbc-dev libpq-dev; \
    \
    pip install pyodbc; \
    pip install paramiko; \
    pip install psycopg2

And I changed the command to run to:

CMD ["python3 /myApp/main.py"]

If, at the end of the dockerfile before the CMD, I add the command "RUN ls -l /myApp" it lists all the files I would expect during the build. But when I use "docker run" to run the image, it gives me the following error:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: "python3 /myApp/main.py": stat python3 /myApp/main.py: no such file or directory": unknown.

My build command is: docker build --file ./Dockerfile --tag my_app --build-arg SSH_PRIVATE_KEY="$(cat sshkey)" .

Then run with docker run my_app

There is probably some docker fundamental that I am misunderstanding, but I can't seem to figure out what it is.


Solution

  • This is hard to answer without your command line or your docker-compose.yml (if any). A recurrent mistake is to map a volume from the host into the container at a non empty location, in this case, your container files are hidden by the content of the host folder.

    The last CMD should be like this:

    CMD ["python3", "/myApp/main.py"]