skip to Main Content

I have two containers for two separate sites and one container for nginx. All I want to do is to copy build files of each site to /usr/share/nginx/html/<site_name>/ in nginx container. I want to keep separate Dockerfile for each site and have named site containers as builder_one and builder_two to copy files from these in nginx Dockerfile:

FROM nginx:latest
COPY ./conf.d/ /etc/nginx/conf.d/
RUN mkdir /usr/share/nginx/html/site_one
RUN mkdir /usr/share/nginx/html/site_two
COPY --from=builder_one /usr/src/site_one/build/ /usr/share/nginx/html/site_one/
COPY --from=builder_two /usr/src/site_two/build/ /usr/share/nginx/html/site_two/

How ever, I get an error:

------
 > [dockernginx-nginx] FROM docker.io/library/builder_two:latest:
------
failed to solve: failed to load cache key: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed

It fails when running COPY command as it tries to pull the container from Docker registry. Why? How do I copy files to nginx container correctly?


Maybe there is another way I could achieve this?

2

Answers


  1. You need to declare the image names from your previous builds in the same docker file.

    In your example:

    FROM <builder_one_image_name> as builder_one
    FROM <builder_two_image_name> as builder_two
    
    FROM nginx:latest
    COPY ./conf.d/ /etc/nginx/conf.d/
    RUN mkdir /usr/share/nginx/html/site_one
    RUN mkdir /usr/share/nginx/html/site_two
    COPY --from=builder_one /usr/src/site_one/build/ /usr/share/nginx/html/site_one/
    COPY --from=builder_two /usr/src/site_two/build/ /usr/share/nginx/html/site_two/
    
    Login or Signup to reply.
  2. You clarify in comments that you have three separate Dockerfiles, and start the first two with FROM base-image AS builder_one syntax. That alias syntax only works within a single Dockerfile; the aliases aren’t persisted as part of the image and can’t be referenced from other Dockerfiles.

    COPY --from can also take a full image name. For the specific setup you describe, you need to build each of the component images with a known tag

    docker build -t myname/builder-one ./site_one
    docker build -t myname/builder-two ./site_two
    

    and then in the final Dockerfile, use those image names

    FROM nginx:latest
    COPY ./conf.d/ /etc/nginx/conf.d/
    COPY --from=myname/builder-one /usr/src/site_one/build/ /usr/share/nginx/html/site_one/
    COPY --from=myname/builder-two /usr/src/site_two/build/ /usr/share/nginx/html/site_two/
    

    The component images don’t need FROM ... AS syntax, and you can generally remove an AS alias part of a Dockerfile’s final stage’s FROM line.

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