Search code examples
dockersvelterollupjsyarnpkg-v2

Yarn build using rollup hangs in docker build process


I am trying to create a docker image for a svelte application. The svelte application I am using is just the svelte template with TypeScript initiated. I have made no changes to the application itself, my only goal is to serve the svelte application using Docker. I am using yarn berry (v2) as my package manager and rollupjs for the build process. This is my first question so please let me know if something is missing.

I am expecting the image to be built, but when the build process reaches the "yarn build" command, it never completes. It doesn't crash either, but it never completes the build step either. It seems to hang at the bundling step:

 => => # src/main.ts → public/build/bundle.js... 

The build command from package.json: "build": "rollup -c"

My Dockerfile was initially taken from here: https://sveltesociety.dev/recipes/publishing-and-deploying/dockerize-a-svelte-app/. However, I had to make some changes to it for yarn v2 and I found the following stack thread: Using Yarn 2 (Berry) for packaging application in a Docker image. Adding some modifications form that question left me with the following Dockerfile:


WORKDIR /app

COPY package.json ./
RUN yarn set version berry
COPY .yarn ./.yarn
COPY yarn.lock .yarnrc.yml ./
RUN yarn plugin import workspace-tools
RUN yarn workspaces focus -A --production

COPY . ./
RUN yarn build   # This is where it gets stuck

FROM nginx:1.19-alpine
COPY --from=build /app/public /usr/share/nginx/html

I run the command docker build -t svelte-image . to build and this is the output I get:

[+] Building 572.6s (16/18)                                                                                                                
 => [internal] load build definition from Dockerfile                                                                                  0.0s
 => => transferring dockerfile: 37B                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                     0.0s
 => => transferring context: 34B                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/nginx:1.19-alpine                                                                  1.2s
 => [internal] load metadata for docker.io/library/node:12                                                                            1.1s
 => CACHED [stage-1 1/2] FROM docker.io/library/nginx:1.19-alpine@sha256:07ab71a2c8e4ecb19a5a5abcfb3a4f175946c001c8af288b1aa766d67b0  0.0s
 => CACHED [build 1/6] FROM docker.io/library/node:12@sha256:cc4adb82efc04b74b9f96326e682ad04be2df84d23e40609802eb6d6c207abde         0.0s
 => [internal] load build context                                                                                                     0.1s
 => => transferring context: 69.58kB                                                                                                  0.0s
 => CACHED [stage-1 2/3] RUN rm /etc/nginx/nginx.conf /etc/nginx/conf.d/default.conf                                                  0.0s
 => CACHED [build  2/10] WORKDIR /app                                                                                                 0.0s
 => CACHED [build  3/10] COPY package.json ./                                                                                         0.0s
 => CACHED [build  4/10] RUN yarn set version berry                                                                                   0.0s
 => [build  5/10] COPY .yarn ./.yarn                                                                                                  0.2s
 => [build  6/10] COPY yarn.lock .yarnrc.yml ./                                                                                       0.0s
 => [build  7/10] RUN yarn plugin import workspace-tools                                                                              1.5s
 => [build  8/10] RUN yarn workspaces focus -A --production                                                                           0.5s
 => [build  9/10] COPY . ./                                                                                                           0.1s 
 => [build 10/10] RUN yarn build                                                                                                    568.9s 
 => => # src/main.ts → public/build/bundle.js... # -> Never completes

I have been trying to figure out why the build hangs, but I am unable to find the source of error. It works just fine when building locally with yarn build. Does anyone have any clue as to why it hangs in the bundling process?


Solution

  • I did not figure out what was wrong, but I managed to build the docker image and run it successfully. Here is the working Dockerfile:

    FROM node:16 AS build
    
    WORKDIR /app
    
    COPY package.json ./
    RUN yarn set version berry
    COPY .yarn ./.yarn  # 
    COPY yarn.lock .yarnrc.yml ./
    RUN yarn install
    
    # These two lines are from the original example. It did not work to use this,
    # but I leave it here to highlight the difference
    ## RUN yarn plugin import workspace-tools
    ## RUN yarn workspaces focus -A --production
    
    COPY . ./
    
    RUN yarn build
    
    FROM nginx:1.19-alpine
    COPY --from=build /app/public /usr/share/nginx/html