0

I have a project that contains a Dockerfile and a docker-compose.yml.

Dockerfile:

FROM mhart/alpine-node:11 AS builder
WORKDIR /app
COPY . .
RUN yarn install
RUN yarn build
WORKDIR /app
COPY --from=builder /app/build .
CMD ["serve", "-p", "80", "-s", "."]

docker-compose.yml:

version: "3"

services:
  front:
    build: .
    ports:
      - 8080:80
    restart: always

When I try to run it like below.

docker-compose up -d

I get the following message:

Building front
Step 1/8 : FROM mhart/alpine-node:11 AS builder
 ---> 5ff13b69f215
Step 2/8 : WORKDIR /app
 ---> Using cache
 ---> 1da7c0d4dfac
Step 3/8 : COPY . .
 ---> Using cache
 ---> 8d82bb9c0ecf
Step 4/8 : RUN yarn install
 ---> Using cache
 ---> 1e557a2dbe03
Step 5/8 : RUN yarn build
 ---> Using cache
 ---> ea2d3f56ff39
Step 6/8 : WORKDIR /app
 ---> Using cache
 ---> 1edb82c45c49
Step 7/8 : COPY --from=builder /app/build .
ERROR: Service 'front' failed to build: invalid from flag value builder: pull access denied for builder, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

I saw another question on Stackoverflow, I think it may be close to my problem.
invalid from flag value build: pull access denied for build, repository does not exist or may require 'docker login'

But I only have a build stage, not two stage like in the question from the link.

Hamza Ince
  • 604
  • 17
  • 46

1 Answers1

1

Your docker build is failing because you are missing a FROM command for your runtime environment. This appears to be a multi-stage docker build, and you are attempting to copy files from a previous build stage named builder but you are still in the first (and only) stage of your build (which is called builder).

You have two options. Either one should work.

  1. Introduce a second runtime stage to your build:

Add another FROM command with the image that you want as your runtime. I just used mhart/alpine-node:11 again here but you may want a different runtime environment.

FROM mhart/alpine-node:11 AS builder
WORKDIR /app
COPY . .
RUN yarn install
RUN yarn build

FROM mhart/alpine-node:11 AS runtime
RUN yarn global add serve
WORKDIR /app
COPY --from=builder /app/build .
CMD ["serve", "-p", "80", "-s", "."]
  1. Make it a non multi-stage build:

Just remove the COPY command and change your workdir to the build directory:

FROM mhart/alpine-node:11 AS builder
RUN yarn global add serve
WORKDIR /app
COPY . .
RUN yarn install
RUN yarn build
WORKDIR /app/build
CMD ["serve", "-p", "80", "-s", "."]
kthompso
  • 1,823
  • 6
  • 15
  • Hello both of your answer, help me building the docker, but I get the error: ERROR: for front_student_front_1 Cannot start service front: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "serve": executable file not found in $PATH: unknown. I tried to search for this error, but to no avail – Hamza Ince Apr 16 '21 at 13:04
  • @Hamza Ince Ok cool, so it's building now but your 'CMD' is off. Do you know what 'serve' is supposed to be? Is it a binary that is output by your build process? If so you probably just need to change 'CMD' to use './serve' instead of 'serve'. Are you following a guide or something? – kthompso Apr 16 '21 at 13:14
  • Ok I found it, I had to install serve with yarn, I hope you don't mind if I edit your answer. – Hamza Ince Apr 16 '21 at 13:44