corvallo
corvallo

Reputation: 169

Gitflow failed too create docker image

Hi everyone I'm facing a strange issue in the creating of a docker image for a remix.run app, and using it inside a github job.

I have this Dockerfile

FROM node:16-alpine as deps


WORKDIR /app

ADD package.json  yarn.lock ./
RUN yarn install 


# Build the app
FROM node:16-alpine as build

ENV NODE_ENV=production

WORKDIR /app

COPY --from=deps /app/node_modules /app/node_modules

COPY . .
RUN yarn run build

# Build production image
FROM node:16-alpine as runner

ENV NODE_ENV=production
ENV PORT=80

WORKDIR /app

COPY --from=deps /app/node_modules /app/node_modules

COPY --from=build /app/build /app/build
COPY --from=build /app/public /app/public
COPY --from=build /app/api /app/api
COPY . .

EXPOSE 80
CMD ["npm", "run", "start"]

If build the image on my local machine, everything works fine, and I'm able to run the container and point at it. I made a github workflow build the same image and push it on my docker hub.

But when the github job runs it always fail with this error

Step 16/21 : COPY --from=build /app/build /app/build
COPY failed: stat app/build: file does not exist

My remix.run config is:

/**
 * @type {import('@remix-run/dev/config').AppConfig}
 */
module.exports = {
  appDirectory: "app",
  assetsBuildDirectory: "public/build",
  publicPath: "/build/",
  serverBuildDirectory: "api/_build",
  devServerPort: 8002,
  ignoredRouteFiles: [".*"],
};

Thanks in advance for any help

Upvotes: 0

Views: 233

Answers (1)

Roman Mkrtchian
Roman Mkrtchian

Reputation: 2996

You are using the config option serverBuildDirectory: "api/_build", so when you run the remix build, your server built files are in api/_build/ and not in build/ directory.

In your docker image, at the last stage you try to copy the content of the build/ directory from the previous stage named build. But that directory does not exist there. The server content is built in api/_build/ instead.

So you just don't need that line:

COPY --from=build /app/build /app/build

One possible reason I see for the fact it works for you locally:

  1. You have a local directory named build/, and which contains your local build for server files. Maybe because you built it before changing the serverBuildDirectory option. And it's probably ignored from your git repository.
  2. When you build your docker image, the stage named build copies everything from your local directory to its own environment with COPY . ., and so it gets your local build/ directory.
  3. then during the last stage that directory is copied to the final environment with COPY --from=build /app/build /app/build.
  4. When you do the same thing in a fresh environment like github actions, the build/ directory does not exist in the environment that runs the docker command so it's not copied from stage to stage. And you finally get an error trying to copy something that does not exist.

This artifact copying is probably not what you want. If you do the build in your docker image, you don't want also to copy it from your local environment.

To avoid these unwanted copies, you can add a .dockerignore file with at least the following things:

node_modules
build
api/_build
public/build

Upvotes: 2

Related Questions