docker / docker-py

A Python library for the Docker Engine API
https://docker-py.readthedocs.io/
Apache License 2.0
6.84k stars 1.68k forks source link

Dockerfile 'copy' not handled correctly #974

Open uggla opened 8 years ago

uggla commented 8 years ago

Based on documentation example, I modified the Dockerfile content. However the following code does not find python-redfish.src.tar.gz

from io import BytesIO
from docker import Client
dockerfile = '''
FROM debian:jessie
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
        apt-get install -y apt-utils && \
        apt-get install -y python-pip
COPY python-redfish.src.tar.gz /python-redfish.src.tar.gz
CMD ["/bin/sh"]
'''
f = BytesIO(dockerfile.encode('utf-8'))
cli = Client(base_url='unix://var/run/docker.sock')
response = [line for line in cli.build(fileobj=f, rm=True, tag='essai')]
print(response)

It rises the following error,

...
'{"errorDetail":{"message":"lstat python-redfish.src.tar.gz: no such file or directory"},"error":"lstat python-redfish.src.tar.gz: no such file or directory"}\r\n']

Using the same content as the script into a Dockerfile

(python2)[uggla@ugglalaptop docker-py_debug]$ ll
total 1712
-rw-rw-r-- 1 uggla uggla     199 Mar  3 23:03 dockerbuild.py
-rw-rw-r-- 1 uggla uggla     232 Mar  3 22:56 Dockerfile
-rw-rw-r-- 1 uggla uggla     484 Mar  3 22:52 example.py
-rw-rw-r-- 1 uggla uggla 1738436 Mar  3 22:04 python-redfish.src.tar.gz
(python2)[uggla@ugglalaptop docker-py_debug]$ cat Dockerfile
FROM debian:jessie
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
        apt-get install -y apt-utils && \
        apt-get install -y python-pip
COPY python-redfish.src.tar.gz /python-redfish.src.tar.gz
CMD ["/bin/sh"]

It builds correctly

(python2)[uggla@ugglalaptop docker-py_debug]$ docker build .
Sending build context to Docker daemon 1.742 MB
Step 1 : FROM debian:jessie
 ---> f50f9524513f
Step 2 : ENV DEBIAN_FRONTEND noninteractive
 ---> Using cache
 ---> b3edf5bbfd90
Step 3 : RUN apt-get update &&         apt-get install -y apt-utils &&         apt-get install -y python-pip
 ---> Using cache
 ---> b00aa7d0ac65
Step 4 : COPY python-redfish.src.tar.gz /python-redfish.src.tar.gz
 ---> 26116a613291
Removing intermediate container 786dca1499bb
Step 5 : CMD /bin/sh
 ---> Running in ac8eb5f0fcff
 ---> dc66ea06ef5c
Removing intermediate container ac8eb5f0fcff
Successfully built dc66ea06ef5c

This code as well

# coding=utf-8
from docker import Client

cli = Client(base_url='unix://var/run/docker.sock')

response = [line for line in cli.build(
    path='.',
    tag='essai',
    rm=True)]

print(response)

Investigating a bit more, I found that the context seems not managed correctly as soon as we use the fileobj parameter.

The files used : docker-py_debug.tar.gz

shin- commented 8 years ago

You need to use custom_context. See #209 and the docs

uggla commented 8 years ago

Thanks @shin- for the prompt answer ! Sorry the doc is not so clear on that point.

So I need to pass a tar file which include my files + the Dockerfile into fileobj and set custom_context=True.

Do I understand well ? If yes, is it still possible to specify a specific Dockerfile name (ex : Dockerfile.debian)

abtreece commented 7 years ago

Also looking for some clarification on this. Trying to pass a Dockerfile with fileobj using docker-py version 1.10.6. It fails with a error: lstat testfile: no such file or directory but that file is definitely in the directory. I've tried to implement the custom_context param, but that errors with an unexpected EOF

I'm trying to implement with a simple script similar to the fileobj script above. Is this a bug or am i missing something in the documentation?

chuikoffru commented 2 years ago

I faced with the same problem

    dockerfile = f"""
    FROM node:18-buster-slim as build-stage
    WORKDIR /app
    ADD /absolute/path/to/my/app.zip /app
    RUN yarn && yarn run build

    FROM nginx:stable-alpine as production-stage
    WORKDIR /usr/share/nginx/html
    COPY --from=build-stage /app/dist ./
    EXPOSE 80
    CMD ["nginx", "-g", "daemon off;"]
    """

    df = io.BytesIO(dockerfile.encode('utf-8'))

    image = await run_in_threadpool(
        client.images.build, 
        fileobj=df,
        tag=f"{current_user.username}/{body.name}",
        rm=True,
        custom_context=False
    )

I have got

docker.errors.BuildError: ADD failed: file not found in build context or excluded by .dockerignore: stat absolute/path/to/my/app.zip: file does not exist

if custom_context = True, then

raise BuildError(last_event or 'Unknown', result_stream) docker.errors.BuildError: {'message': 'unexpected EOF'}

Help to fix please