Unable to build and image from existing Dockerfile.

Answered

Hi, 

I am trying out PyCharm and my mind is blown at how good an IDE it is. I have never used an IDE for the last 3 years of my professional career, having stuck to vim and tmux for my coding workflow. 

I recently decided to give PyCharm a try and it was really worth giving it a shot. I am happy with the test-driven development support, the git merge tool and the autocomplete/refactor features in PyCharm. 

The one thing that I am not able to get to work till now is the Docker build and run. 

I have an existing project that has a Dockerfile. The docker image is built from the docker file and then the container is run. Here is my Dockerfile: 


FROM python:3.6-alpine3.6

RUN apk --update add bash

SHELL ["/bin/bash", "-c"]

RUN pip install pipenv
RUN pip install gunicorn

RUN mkdir /app

COPY ./module1 /app/module1
COPY ./module2 /app/module2

WORKDIR /app

RUN pip install -e module1
RUN pip install -e module2

EXPOSE 8080

ENTRYPOINT /app/segment/resources/run.gunicorn.sh

My docker build command is as follows, which I run from the root of my project directory. It's important that I run the docker build command from that directory. 

docker build -t test:0.1 -f test/resources/Dockerfile .

And then I run the container as follows:

docker container run -t -d -p 80:8080 -e "SERVER_HOST=0.0.0.0" -e "SERVER_PORT=8080" --name "test_container" test:0.1

I tried to configure the docker run configuration in PyCharm which is similar to the two commands above. Here is a screenshot:

Here is the command preview with the complete command generated by PyCharm: 

docker build -t test:0.1 .
&& docker run
-p 80:8080
--env SERVER_HOST=0.0.0.0
--env SERVER_PORT=8080
--name test_container
-d
-t
test:0.1

So the Dockerfile starts to bootstrap an image from python-alpine and copies the code from host to guest. But the copy never works in PyCharm as it fails with the following error: 

Step 7/13 : COPY ./shared /app/shared



Error: ResponseItem.ErrorDetail[code=<null>,message=COPY failed: stat /var/lib/docker/tmp/docker-builder134533513/module1: no such file or directory]
Failed to deploy 'test Dockerfile: test/resources/Dockerfile': COPY failed: stat /var/lib/docker/tmp/docker-builder134533513/module1: no such file or directory

I believe that there is no way we can change the location for the docker build command, which causes the COPY command to fail. Can I fix this? Is there something that I am doing fundamentally wrong? 

I tried to find a solution online but couldn't find anything specific to my issue. It's worth mentioning that I am not trying to use a python environment inside a Docker container. 

10 comments
Comment actions Permalink
Official comment

Hello

At the moment the Docker plugin does not support custom context folder and always assumes that the context folder is the one that contains the Dockerfile. 

In your case it is the same as running `cd test/resources && docker build -t test:0.1 . && ... ` (similar to what is shown in the preview)

And this correctly fails because only the context folder and its subfolders had been tar-red and sent to docker engine, and this archive does not contain ./module1

As a workaround please move the dockerfile to the location that already contains the ./module1 child.

We do have a youtrack for custom context support, please vote for https://youtrack.jetbrains.com/issue/IDEA-153443  

Hope that helps

Regards, 

Michael 

 

 

 

Comment actions Permalink

Thank you for your comment. I can understand and wait for the feature to be implemented in the next release of PyCharm. I will also temporarily move my Dockerfile to the root of the project. Unfortunately, I won't, be able to commit this as it is an organization level decision for all apps/modules to have their own respective Dockerfile inside their own directories. 

Alternatively, I will see if I can invoke a small script (which moves all the modules inside `test/resources`) that Dockerfile can run on the host before the build process is started, and then run a postscript after the image is built (remove the modules from within test/resources). 

Thank you very much for your time. 

0
Comment actions Permalink

@Michael, 

I also saw a linked issue to the youtrack you linked. It is the same issue but it says that it's fixed and available in `master` for PyCharm 2017.3. Is that version not released yet? 

0
Comment actions Permalink

@Anubhav1691 

No, it is the different story. 

In 2017.3 we have indeed implemented an ability to use any Dockerfile as a target for run-config. But the context folder is still always the one containing the dockerfile.

(Internally plugin does not call any CLI and uses 3rd party library, so changing the context is a bit more challenging than just calling `ProcessBuilder#directory(File)` standard java method). So unfortunately this is still not implemented yet. 

Regards

 

 

0
Comment actions Permalink

@Michael

Thank you for giving a clear and crisp explanation. It all makes sense to me now. I have added my vote to the issue. Hopefully, I can test the beta version when the feature is added. 

Thanks a lot. 

0
Comment actions Permalink

The same issue occurs using a .dockerignore file excluding all files by default and specifying files/folders to keep, even if the build context is correct. Removing the .dockerignore or deleting its content results in a successful build.

Example .dockerignore resulting in "COPY failed ... no such file":

```
*
!client/output/release
!server/output/release
!dockerfile
```

1
Comment actions Permalink

Fix is planned for 2018.1. Stay tuned!

0
Comment actions Permalink

Seems it is still not fixed in 2019.1.

I am still getting this error with a .dockerignore as mentioned above.

0
Comment actions Permalink

Hi Andy,

You are probably facing this issue https://youtrack.jetbrains.com/issue/IDEA-200303? This one is not yet fixed.

0
Comment actions Permalink

I also can Dockerfile build manually from the project directory, but PyCharm can't retrieve a file right next to the dockerfile.

I'm using PyCharm 2020.1 and I'm using a .dockerignore as a whitelist

0

Please sign in to leave a comment.