skip to Main Content

I am creating a dockerfile.

WORKDIR /abc/xyz
copy xyz .
RUN pip install  a1 a2 a3

iam not not aware of all constructs of Docker. Is there any command in do copy and pip task run parallel to save time?

2

Answers


  1. Nope it runs each step as a new layer. COPY will perform a copy from the context it is running on so it’s different from RUN which runs in the the container only.

    Docker Buildkit will parallelize as much as possible though, but your WORKDIR, COPY and RUN commands above need to run in sequence for it to make sense.

    Login or Signup to reply.
  2. You can use buildkit features. Use DOCKER_BUILDKIT environment variable.

    Example Dockerfile:

    FROM alpine as a
    RUN echo "1 start"
    COPY foo .
    RUN sleep 10 && echo "1 done"
    
    
    FROM alpine as b
    RUN echo "2 start"
    COPY bar .
    RUN sleep 10 && echo "2 done"
    
    
    FROM centos
    COPY --from=a foo .
    COPY --from=b bar .
    CMD ["cat", "foo", "bar"]
    

    DOCKER_BUILDKIT=1 docker build -t test .

    Build output:

     => [internal] load build definition from Dockerfile                                                                                               0.0s
     => => transferring dockerfile: 339B                                                                                                               0.0s
     => [internal] load .dockerignore                                                                                                                  0.0s
     => => transferring context: 2B                                                                                                                    0.0s
     => [internal] load metadata for docker.io/library/alpine:latest                                                                                   0.0s
     => [internal] load metadata for docker.io/library/centos:latest                                                                                   0.0s
     => [stage-2 1/3] FROM docker.io/library/alpine                                                                                                    0.0s
     => [internal] load build context                                                                                                                  0.0s
     => => transferring context: 164B                                                                                                                  0.0s
     => CACHED [b 1/4] FROM docker.io/library/centos                                                                                                   0.0s
     => [a 2/4] RUN echo "1 start"                                                                                                                     0.5s
     => [b 2/4] RUN echo "2 start"                                                                                                                     0.5s
     => [b 3/4] COPY bar .                                                                                                                             0.1s
     => [a 3/4] COPY foo .                                                                                                                             0.1s
     => [b 4/4] RUN sleep 10 && echo "2 done"                                                                                                         10.4s
     => [a 4/4] RUN sleep 10 && echo "1 done"                                                                                                         10.4s
     => CACHED [stage-2 2/3] COPY --from=a foo .                                                                                                       0.0s 
     => CACHED [stage-2 3/3] COPY --from=b bar .                                                                                                       0.0s 
     => exporting to image                                                                                                                             0.0s
     => => exporting layers                                                                                                                            0.0s
     => => writing image sha256:1517fa1d98ab568a7b16f91c43b714bdf34406929ddf5c8a992bee5d3d4b942e                                                       0.0s
     => => naming to docker.io/library/test                                                                                                            0.0s
    

    It took 12 seconds.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search