Yarn build using rollup hangs in docker build process - docker

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?

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

Related

error An unexpected error occurred: "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz: getaddrinfo EAI_AGAIN registry.yarnpkg.com"

I was following the guide in Docker official guide, Guides->Get started->Part 2: Containerize an application.
git clone https://github.com/docker/getting-started.git
then I created a Dockerfile with content as below:
# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
I tried to build it, but an error happened
docker build -t getting-started .
Error information are as follows :
[+] Building 101.8s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1 13.7s
=> CACHED docker-image://docker.io/docker/dockerfile:1#sha256:9ba7531bd80fb0a858632727cf7a112fbfd19b17e94c4e84ced81e24ef1a0dbc 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> [internal] load .dockerignore 0.0s
=> [internal] load metadata for docker.io/library/node:18-alpine 11.9s
=> [1/4] FROM docker.io/library/node:18-alpine#sha256:b3f383c13d71066a4e2380d42f4563ac14ac76b3035a5bea4db84209e14665d5 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2.49kB 0.0s
=> CACHED [2/4] WORKDIR /app 0.0s
=> CACHED [3/4] COPY . . 0.0s
=> ERROR [4/4] RUN yarn install --production 20.8s
------
> [4/4] RUN yarn install --production:
#10 0.456 yarn install v1.22.19
#10 0.516 [1/4] Resolving packages...
#10 0.689 [2/4] Fetching packages...
#10 5.792 error An unexpected error occurred: "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz: getaddrinfo EAI_AGAIN registry.yarnpkg.com".
#10 5.792 info If you think this is a bug, please open a bug report with the information provided in "/app/yarn-error.log".
#10 5.792 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
------
executor failed running [/bin/sh -c yarn install --production]: exit code: 1
Here is my enviroment :
WIN10 Family Edition 22H2
Docker Desktop 4.15.0 (93002)
I've tried to add command
yarn config set registry http://registry.npm.taobao.org/
in the Dockerfile, but this still happened. The modified Dockerfile is as below:
# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn config set registry http://registry.npm.taobao.org/ && yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
Still didn't fix the error. And I can't find the log file /app/yarn-error.log

Error when trying to build my golang application in docker while using the mysql driver

I have a simple application that uses github.com/go-sql-driver/mysql to connect to a MySQL database and execute simple queries. This all works fine on my local machine, however when I try to build it using docker build I get the following output:
[+] Building 4.1s (9/10)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 104B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/golang:onbuild 1.3s
=> [auth] library/golang:pull token for registry-1.docker.io 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 5.63kB 0.0s
=> CACHED [1/2] FROM docker.io/library/golang:onbuild#sha256:c0ec19d49014d604e4f62266afd490016b11ceec103f0b7ef44 0.0s
=> [2/2] COPY . /go/src/app 0.1s
=> [3/2] RUN go-wrapper download 2.0s
=> ERROR [4/2] RUN go-wrapper install 0.6s
------
> [4/2] RUN go-wrapper install:
#8 0.465 + exec go install -v
#8 0.535 github.com/joho/godotenv
#8 0.536 github.com/go-sql-driver/mysql
#8 0.581 # github.com/go-sql-driver/mysql
#8 0.581 ../github.com/go-sql-driver/mysql/driver.go:88: undefined: driver.Connector
#8 0.581 ../github.com/go-sql-driver/mysql/driver.go:99: undefined: driver.Connector
#8 0.581 ../github.com/go-sql-driver/mysql/nulltime.go:36: undefined: sql.NullTime
------
executor failed running [/bin/sh -c go-wrapper install]: exit code: 2
My go version is up to date and I am using the following dockerfile:
FROM golang:onbuild
To my knowledge this should go get all the packages it requires. I've also tried it this way:
FROM golang:onbuild
RUN go get "github.com/go-sql-driver/mysql"
This had the same output.
Note that in my code I import the package like this:
import _ "github.com/go-sql-driver/mysql"
I also use other packages from github, these seem to work fine.
The Docker community has generally been steering away from the Dockerfile ONBUILD directive, since it makes it very confusing what will actually happen in derived images (see the various comments around "is that really the entire Dockerfile?"). If you search Docker Hub for the golang:onbuild image you'll discover that this is Go 1.7 or 1.8; Go modules were introduced in Go 1.11.
You'll need to update to a newer base image, and that means writing out the Dockerfile steps by hand. For a typical Go application this would look like
FROM golang:1.18 AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY ./ ./
RUN go build -o myapp .
FROM ubuntu:20.04
COPY --from=build /app/myapp /usr/local/bin
CMD ["myapp"]
(In the final stage you may need to RUN apt-get update && apt-get install ... a MySQL client library or other tools.)

How do you get a directory listing to output during build in Docker 20.10.3? (March 2022) [duplicate]

This question already has answers here:
Why is docker build not showing any output from commands?
(6 answers)
Closed 11 months ago.
The answers here don't seem to work. The answer here also doesn't work. I suspect something has changed about Docker's build engine since then.
My Dockerfile:
FROM node:16.14.2-alpine
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn
COPY dist .
EXPOSE $SEEDSERV_PORT
RUN pwd
RUN echo "output"
RUN ls -alh
RUN contents="$(ls -1 /usr/src/app)" && echo $contents
# CMD ["node","server.js"]
ENTRYPOINT ["tail", "-f", "/dev/null"]
Which gives this output from build:
✗ docker build --progress auto --build-arg SEEDSERV_PORT=9999 -f build/api/Dockerfile .
[+] Building 2.1s (14/14) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/node:16.14.2-alpine 1.9s
=> [internal] load build context 0.0s
=> => transferring context: 122B 0.0s
=> [1/9] FROM docker.io/library/node:16.14.2-alpine#sha256:da7ef512955c906b6fa84a02295a56d0172b2eb57e09286ec7abc02cfbb4c726 0.0s
=> CACHED [2/9] WORKDIR /usr/src/app 0.0s
=> CACHED [3/9] COPY package.json yarn.lock ./ 0.0s
=> CACHED [4/9] RUN yarn 0.0s
=> CACHED [5/9] COPY dist . 0.0s
=> CACHED [6/9] RUN pwd 0.0s
=> CACHED [7/9] RUN echo "output" 0.0s
=> CACHED [8/9] RUN ls -alh 0.0s
=> CACHED [9/9] RUN contents="$(ls -1 /usr/src/app)" && echo $contents 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:d1dd7ac452ecacc803eed2bb1deff654c3296a5576b6f418dbd07c5f2e644f1a 0.0s
Adding --progress plain gives slightly different output but not what I'm looking for, e.g.:
#11 [7/9] RUN echo "output"
#11 sha256:634e07d201926b0f70289515fcf4a7303cac3658aeddebfa9552fc3054ed4ace
#11 CACHED
How can I get a directory listing during build in 20.10.3? I can exec into the running container but that's a lot more work.
If your build is cached, there's no output from the run to show. You need to include --no-cache to run the command again for any output to display, and also include --progress plain to output to the console.

Docker build for vue project module not found error

I am trying to build a vue app docker image but I keep getting this error
Module not found: Error: Can't resolve './src/main.js' in '/app'
I am using the a multi-stage dockerfile
FROM node:15.4 as build
WORKDIR /app
COPY ./frontend/package.json .
RUN npm install
COPY . .
RUN npm run build
FROM nginx:1.19
COPY ./frontend/nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=build /app/dist /usr/share/nginx/html
Here's the script section of the package.json file
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
This is a detailed log of the build process
[+] Building 26.9s (14/16)
=> [internal] load build definition from frontend.dockerfile 0.0s
=> => transferring dockerfile: 293B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 34B 0.0s
=> [internal] load metadata for docker.io/library/nginx:1.19 2.5s
=> [internal] load metadata for docker.io/library/node:15.4 2.4s
=> [auth] library/node:pull token for registry-1.docker.io 0.0s
=> [auth] library/nginx:pull token for registry-1.docker.io 0.0s
=> [build 1/6] FROM docker.io/library/node:15.4#sha256:a76eb778d162f8fd96138d9ca7dbd14b8916c201775a97d2f2aa22e9f13eb105 0.0s
=> [stage-1 1/3] FROM docker.io/library/nginx:1.19#sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2 0.0s
=> [internal] load build context 8.5s
=> => transferring context: 166.03MB 8.4s
=> CACHED [build 2/6] WORKDIR /app 0.0s
=> CACHED [build 3/6] COPY ./frontend/package.json . 0.0s
=> CACHED [build 4/6] RUN npm install 0.0s
=> [build 5/6] COPY . . 7.4s
=> ERROR [build 6/6] RUN npm run build 8.3s
------
> [build 6/6] RUN npm run build:
#14 2.913
#14 2.913 > frontend#0.1.0 build
#14 2.913 > vue-cli-service build
#14 2.913
#14 4.668 All browser targets in the browserslist configuration have supported ES module.
I move the frontend folder from the project to stand alone and I am able to build the image and run a container based on it successfully.
What could possibly be wrong here?
I try to give you some help. First of all I assume your nginx.conf file is located in the upper nginx folder. I see two in your repo. If this is the case you are copying one layer to deep. I suggest to change your file as follows with this order:
FROM node:alpine as build
WORKDIR /app
COPY ./package*.json ./
RUN npm install
COPY "frontend/target/dist" ./
FROM nginx:alpine as production
COPY ./frontend/nginx/nginx.conf /etc/nginx/conf.d/nginx.conf
RUN rm -rf /usr/share/nginx/html/*
COPY --from=build /app /usr/share/nginx/html
What I changed was the layer of the folders you are trying to add and the asterix (*) in package.json. If you are defining a frontend before, you are also searching it in the copied part, which is not existing in the nginx image. Also as you see you only need /app instead of /app/dist. Additional your /etc... config path was wrongly referenced. Give it a shot now. It should work like this with the given information I had.
Add in your .dockerignore file only those lines:
node_modules
dist

How do I use java 11 with Docker?

In my DockerFile I have my FROM line like so :
FROM openjdk:11-jdk-alpine as build
Previously it was on java 8 and everything was working fine.
Now I get this error :
C:\dev\shape-shop-back-end>docker build .
[+] Building 2.0s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 1.31kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CANCELED [internal] load metadata for docker.io/library/openjdk:11-jre-alpine 1.8s
=> ERROR [internal] load metadata for docker.io/library/openjdk:11-jdk-alpine 1.8s
=> [auth] library/openjdk:pull token for registry-1.docker.io 0.0s
------
> [internal] load metadata for docker.io/library/openjdk:11-jdk-alpine:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: docker.io/library/openjdk:11-jdk-alpine: not found
My docker file :
#### Stage 1: Build the application
FROM openjdk:11-jdk-alpine as build
# Set the current working directory inside the image
WORKDIR /app
# Copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn
# Copy the pom.xml file
COPY pom.xml .
# Build all the dependencies in preparation to go offline.
# This is a separate step so the dependencies will be cached unless
# the pom.xml file has changed.
RUN ./mvnw dependency:go-offline -B
# Copy the project source
COPY src src
# Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
#### Stage 2: A minimal docker image with command to run the app
FROM openjdk:11-jre-alpine
ARG DEPENDENCY=/app/target/dependency
# Copy project dependencies from the build stage
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.shapeshop.App"]
#ARG JAR_FILE=target/*.jar
#COPY ${JAR_FILE} app.jar
#ENTRYPOINT ["java","-jar","/app.jar"]
#
## Expose port 80 to the Docker host, so we can access it
## from the outside.
#EXPOSE 8080
Am I missing something?
If I do a FROM without the "alpine" like so :
FROM openjdk:11 as build
then it worked for me
This may help:
https://hub.docker.com/_/openjdk
"The OpenJDK port for Alpine is not in a supported release by OpenJDK, since it is not in the mainline code base. It is only available as early access builds of OpenJDK Project Portola. See also this comment. So this image follows what is available from the OpenJDK project's maintainers.
What this means is that Alpine based images are only released for early access release versions of OpenJDK. Once a particular release becomes a "General-Availability" release, the Alpine version is dropped from the "Supported Tags"; they are still available to pull, but will no longer be updated."
I don't use java images, this may suffice instead:
https://hub.docker.com/r/adoptopenjdk/openjdk11/

Resources