Gitlab job failed when I try to build Docker image - docker

There is the following Dockerfile:
FROM node:12.22-alpine3.12 as builder
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN npm i && npm build
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
EXPOSE 80
COPY --from=builder /app/build ./
ENTRYPOINT ["nginx", "-g", "daemon off;"]
There is nothing smart - just build static JS app and put it to nginx directory. It works locally excellent. But it failed on Gitlab with the following error:
Step 8/11 : RUN rm -rf ./*
---> Running in a9cf55a3da31
Removing intermediate container a9cf55a3da31
---> 50c9a02d542d
Step 9/11 : EXPOSE 80
---> Running in edda7f7bf296
Removing intermediate container edda7f7bf296
---> be2caf89404f
Step 10/11 : COPY --from=builder /app/build ./
COPY failed: stat /var/lib/docker/overlay2/6a441679247bd4e1a321f7c6e15d4fe8dd59c5013d4e0058b7f3c9f395c20f98/merged/app/build: no such file or directory
Cleaning up file based variables
00:02
ERROR: Job failed: command terminated with exit code 1
And I don't understand what does it mean, because locally it works without any troubles at all.
UPD: my .gitlab-ci.yml:
stages:
- test
- build
test:
stage: test
image: node:12.22-alpine3.12
script:
- echo 'Test Success'
build:
image: docker:19.03.12
stage: build
services:
- docker:19.03.12-dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG.$CI_PIPELINE_ID" .
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG.$CI_PIPELINE_ID"

Related

executable not found on docker container

I'm having some issue trying to run a CMD command inside a docker container with a Go application.
This is the output I've got:
golang-api | /bin/sh: ./server: not found
golang-api exited with code 127
This is the Dockerfile
FROM golang:1.18-alpine
RUN apk add g++ && apk add make
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN make build
EXPOSE 8000
CMD ./server
And this is the Makefile responsible for the build command:
LINUX_AMD64 = GOOS=linux GOARCH=amd64 CGO_ENABLED=1 GO111MODULE=on
migrate:
cd cmd/migrations/$(FOLDER) && go run main.go
build:
cd cmd && $(LINUX_AMD64) go build -a -v -tags musl -o server
And the docker-compose.yml
version: '3.1'
services:
api:
build:
dockerfile: Dockerfile
context: .
container_name: golang-api
ports:
- "8000:8000"
restart: unless-stopped
depends_on:
- db
environment:
- POSTGRES_URL=$POSTGRES_URL
db:
image: postgres
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- '5432:5432'
Complete output:
Building api
Sending build context to Docker daemon 74.24kB
Step 1/9 : FROM golang:1.18-alpine
---> 6078a5fce1f5
Step 2/9 : RUN apk add g++ && apk add make
---> Using cache
---> 2a85b9182b80
Step 3/9 : WORKDIR /app
---> Using cache
---> baf1e6b7047c
Step 4/9 : COPY go.mod go.sum ./
---> Using cache
---> 1f2d031bc1b0
Step 5/9 : RUN go mod download
---> Using cache
---> 471d6f24e6a9
Step 6/9 : COPY . .
---> eaa86ff7cb1b
Step 7/9 : RUN make build
---> Running in d6ae6ce79222
cd cmd && GOOS=linux GOARCH=amd64 CGO_ENABLED=1 GO111MODULE=on go build -a -v -tags musl -o server
internal/goos
internal/goarch
internal/race
internal/unsafeheader
internal/goexperiment
runtime/internal/syscall
internal/cpu
sync/atomic
runtime/internal/atomic
internal/itoa
math/bits
internal/abi
runtime/internal/math
runtime/internal/sys
unicode/utf8
unicode
container/list
crypto/internal/subtle
crypto/subtle
unicode/utf16
vendor/golang.org/x/crypto/cryptobyte/asn1
internal/nettrace
vendor/golang.org/x/crypto/internal/subtle
encoding
github.com/mymachine/my-api/internal/controllers/dto
github.com/mymachine/my-api/internal/controllers/helpers
internal/bytealg
math
runtime
internal/reflectlite
sync
internal/testlog
internal/singleflight
math/rand
runtime/cgo
errors
sort
internal/oserror
strconv
path
vendor/golang.org/x/net/dns/dnsmessage
io
crypto/elliptic/internal/fiat
syscall
golang.org/x/text/internal/tag
hash
bytes
strings
hash/crc32
reflect
crypto
crypto/internal/randutil
crypto/hmac
crypto/rc4
net/http/internal/ascii
vendor/golang.org/x/crypto/hkdf
regexp/syntax
github.com/jackc/chunkreader/v2
bufio
crypto/elliptic/internal/nistec
internal/syscall/unix
time
internal/syscall/execenv
vendor/golang.org/x/text/transform
golang.org/x/crypto/pbkdf2
golang.org/x/text/transform
golang.org/x/text/runes
golang.org/x/text/width
regexp
internal/poll
io/fs
context
github.com/mymachine/my-api/internal/entities
github.com/jackc/pgconn/internal/ctxwatch
embed
os
github.com/jinzhu/inflection
github.com/jinzhu/now
internal/fmtsort
encoding/binary
crypto/ed25519/internal/edwards25519/field
crypto/md5
crypto/sha512
crypto/cipher
crypto/sha256
crypto/sha1
encoding/base64
fmt
internal/godebug
encoding/pem
crypto/ed25519/internal/edwards25519
path/filepath
vendor/golang.org/x/crypto/internal/poly1305
io/ioutil
internal/intern
vendor/golang.org/x/crypto/curve25519/internal/field
vendor/golang.org/x/sys/cpu
net/netip
crypto/aes
crypto/des
vendor/golang.org/x/crypto/chacha20
github.com/jackc/pgio
github.com/jackc/pgpassfile
os/exec
os/signal
net
vendor/golang.org/x/crypto/chacha20poly1305
math/big
encoding/hex
net/url
compress/flate
vendor/golang.org/x/crypto/curve25519
log
vendor/golang.org/x/text/unicode/norm
vendor/golang.org/x/text/unicode/bidi
vendor/golang.org/x/net/http2/hpack
mime
mime/quotedprintable
compress/gzip
net/http/internal
database/sql/driver
github.com/mymachine/my-api/internal/pkg/errors
encoding/json
github.com/jackc/pgservicefile
vendor/golang.org/x/text/secure/bidirule
golang.org/x/text/internal/language
database/sql
golang.org/x/text/unicode/norm
golang.org/x/text/unicode/bidi
vendor/golang.org/x/net/idna
os/user
crypto/rand
encoding/asn1
crypto/dsa
crypto/elliptic
crypto/ed25519
crypto/rsa
github.com/jackc/pgproto3/v2
golang.org/x/text/internal/language/compact
golang.org/x/text/secure/bidirule
github.com/jackc/pgx/v4/internal/sanitize
golang.org/x/text/language
go/token
vendor/golang.org/x/crypto/cryptobyte
crypto/x509/pkix
gorm.io/gorm/utils
gorm.io/gorm/logger
encoding/gob
go/scanner
golang.org/x/text/internal
github.com/joho/godotenv
crypto/ecdsa
go/ast
golang.org/x/text/cases
github.com/joho/godotenv/autoload
golang.org/x/text/secure/precis
gorm.io/gorm/clause
gorm.io/gorm/schema
gorm.io/gorm
net/textproto
vendor/golang.org/x/net/http/httpproxy
github.com/google/uuid
crypto/x509
github.com/jackc/pgtype
mime/multipart
vendor/golang.org/x/net/http/httpguts
crypto/tls
gorm.io/gorm/migrator
gorm.io/gorm/callbacks
net/http/httptrace
github.com/jackc/pgconn
net/http
github.com/jackc/pgconn/stmtcache
github.com/jackc/pgx/v4
github.com/jackc/pgx/v4/stdlib
gorm.io/driver/postgres
github.com/mymachine/my-api/internal/infrastructure/repository
github.com/mymachine/my-api/configs
github.com/mymachine/my-api/internal/services
github.com/mymachine/my-api/internal/services/transaction
github.com/mymachine/my-api/internal/controllers/handlers/health
github.com/gorilla/mux
github.com/mymachine/my-api/internal/controllers/handlers/transaction
github.com/mymachine/my-api/internal/router
github.com/mymachine/my-api/cmd
Removing intermediate container d6ae6ce79222
---> 621aa6a266de
Step 8/9 : EXPOSE 8000
---> Running in 3cbd17d32c37
Removing intermediate container 3cbd17d32c37
---> 754f71a7753e
Step 9/9 : CMD ./server
---> Running in dfcdc8cd45ce
Removing intermediate container dfcdc8cd45ce
---> 511dbf89a001
Successfully built 511dbf89a001
Successfully tagged my-api_api:latest
Creating my-api_db_1 ... done
Creating golang-api ... done
Attaching to my-api_db_1, golang-api
golang-api | /bin/sh: ./server: not found
The problem was solved.
Somehow,the RUN cd cmd command seemed not generating the file in the expected directory.
Trying with RUN go build cmd/main.go instead of using the makefile worked, as well pointed by #paltaa in the comments.

docker : No subsytem for mount

I'm running a CI pipeline in gitlab-runner which is running on a linux machine.
In the pipeline I'm trying to build an image.
The Error I'm getting is,
ci runtime error: rootfs_linux.go:53: mounting "/sys/fs/cgroup" to rootfs "/var/lib/docker/overlay/d6b9ba61e6eed4bcd9c23722ad6f6a9fb05b6c536dbab31f47b835c25c0a343b/merged" caused "no subsystem for mount"
The Dockerfile contains :
# Set the base image to node:12-alpine
FROM node:16.7.0-buster
# Specify where our app will live in the container
WORKDIR /app
# Copy the React App to the container
COPY . /app/
# Prepare the container for building React
RUN npm install
RUN npm install react-scripts#4.0.3 -g
# We want the production version
RUN npm run build
# Prepare nginx
FROM nginx:stable
COPY --from=0 /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d
# Fire up nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
The gitlab-ci.yml contains :
image: gitlab/dind
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
stages:
- build
- docker-build
cache:
paths:
- node_modules
yarn-build:
stage: build
image: node:latest
script:
- unset CI
- yarn install
- yarn build
artifacts:
expire_in: 1 hour # the artifacts expire from the artifacts folder after 1 hour
paths:
- build
docker-build:
stage: docker-build
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
- docker build -t $CI_REGISTRY_USER/$app_name .
- docker push $CI_REGISTRY_USER/$app_name
I'm not getting how to resolve this, I tried upgrading docker in my linux machine.

Docker entrypoint script on ECS, Permission Denied

Im looking at a problem when i deploy to AWS ECS regarding permissions.
I need to start a shell script in the entrypoint of the Dockerfile, which uses wait-for-it.sh to pause until the supporting container is ready.
The base for the project is aspnet core and is intended to facilitate database upgrades.
The error from the container is:
container_linux.go:380: starting container process caused: exec: "./docker-entrypoint.sh": permission denied
Other posts suggest its because the scripts arnt marked as executable. See below build output which confirms this is done
Step 14/21 : WORKDIR /app
---> Running in 5517aabffaa2
Removing intermediate container 5517aabffaa2
---> 9724951dd81b
Step 15/21 : COPY Data.Migrator/wait-for-it.sh wait-for-it.sh
---> dcd710cf0671
Step 16/21 : RUN chmod +x wait-for-it.sh
---> Running in 914a4e63fbcd
Removing intermediate container 914a4e63fbcd
---> d1e8da150a8c
Step 17/21 : COPY ["Data.Migrator/docker-entrypoint.sh", "docker-entrypoint.sh"]
---> 1e193d79558c
Step 18/21 : RUN ["chmod", "+x", "docker-entrypoint.sh"]
---> Running in fb737194fcc3
Removing intermediate container fb737194fcc3
---> e89b9eaa086c
Step 19/21 : COPY --from=publish /app/publish .
---> 972f1bd340ec
Step 20/21 : ENTRYPOINT ["./docker-entrypoint.sh"]
---> Running in 19469ffcfb32
Removing intermediate container 19469ffcfb32
---> 00447053022c
My service in the docker-compose is
data_migrator:
image: xxx.dkr.ecr.eu-west-1.amazonaws.com/migrator:latest
restart: 'always'
depends_on:
sql-server-db:
condition: service_healthy
environment:
MAIN_STORE_CONNECTION_STRING: "Server=sql_server_db,1433;Database=MainStore;User Id=sa;Password=Password123#;MultipleActiveResultSets=True;"
SIGNABLE_STORE_CONNECTION_STRING: "Server=sql_server_db,1433;Database=SignableStore;User Id=sa;Password=Password123#;MultipleActiveResultSets=True;"
DEPENDSUPON_HOST: sql-server-db
DEPENDSUPON_PORT: 1433
ECS_IMAGE_PULL_BEHAVIOR: always
networks:
- publicnet
Docker entrypoint script
#!/bin/bash
# Abort on any error (including if wait-for-it fails).
set -e
# Wait for the backend to be up, if we know where it is.
if [ -n "$DEPENDSUPON_HOST" ]; then
echo "Waiting for database connection..."
./wait-for-it.sh "$DEPENDSUPON_HOST:${DEPENDSUPON_PORT:-6000}"
fi
# Run the main container command.
exec "$#"
and the dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Data.Migrator/Data.Migrator.csproj", "Data.Migrator/"]
COPY ["Web.Workflow/Web.Workflow.csproj", "Web.Workflow/"]
RUN dotnet restore "Data.Migrator/Data.Migrator.csproj"
COPY . .
WORKDIR "/src/Data.Migrator"
RUN dotnet build "Data.Migrator.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Data.Migrator.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
# Copy wait-for-it.sh into our image
COPY Data.Migrator/wait-for-it.sh wait-for-it.sh
# Make it executable, in Linux
RUN chmod +x wait-for-it.sh
COPY ["Data.Migrator/docker-entrypoint.sh", "docker-entrypoint.sh"]
RUN ["chmod", "755", "docker-entrypoint.sh"]
# Make it executable, in Linux
COPY --from=publish /app/publish .
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["dotnet", "Data.Migrator.dll"]

Why the GitLab CI/CD fail with multistage Dockerfile?

Following this Spring Blog post I have a multistage Dockerfile:
FROM adoptopenjdk:11-jre-hotspot as builder
WORKDIR application
ARG JAR_FILE=./hello/build/libs/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
ADD ./dependencies/ ./
FROM adoptopenjdk:11-jre-hotspot
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/snapshot-dependencies/ ./
# COPY --from=builder application/resources/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
It works perfectly in a local environment with: docker build . --tag hello.
I want to run a GitLab CI/CD pipeline that builds this Dockerfile, so my .gitlab-ci.yml is:
image: openjdk:11
variables:
DOCKER_DRIVER: overlay
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
stages:
- build
- release
build hello:
stage: build
script:
- cd ./hello
- ./gradlew build -x test --stacktrace
artifacts:
paths:
- ./hello/build/libs/*.jar
only:
changes:
- hello/**/*
release hello:
image: docker:latest
services:
- docker:dind
stage: release
script:
- docker build -t registry.gitlab.com/myproject/hello --file ./Dockerfile .
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker push registry.gitlab.com/myproject/hello
only:
changes:
- hello/**/*
but... it fail because between the two stages of the Dockerfile the extracted (from the builder) folders can't be found.
How can I solve this and implement a working GitLab CI/CD that build the image from that Dockerfile?
EDIT:
If I add a RUN ls application command, after every COPY command, in the second phase:
FROM adoptopenjdk:11-jre-hotspot AS builder
WORKDIR application
ARG JAR_FILE=./hello/build/libs/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM adoptopenjdk:11-jre-hotspot
WORKDIR application
COPY --from=builder application/dependencies ./
RUN ls application
COPY --from=builder application/snapshot-dependencies ./
RUN ls application
# COPY --from=builder application/resources ./
COPY --from=builder application/spring-boot-loader ./
RUN ls application
COPY --from=builder application/application ./
RUN ls application
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
The GitLab CI/CD works correctly. Isn't this weird?

Docker Build Failing on Travis --> can't find app/build

I am trying to push my images from Travis CI to docker hub.
Here is my Dockerfile
FROM node:alpine as builder
WORKDIR /app
COPY ./package.json ./
RUN npm install
COPY . .
CMD npm run build
FROM nginx
EXPOSE 3000
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build /usr/share/nginx/html
My .travis.yml
sudo: required
services:
- docker
before_install:
- docker build -t aseel/react-test -f ./client/Dockerfile.dev ./client
script:
- docker run -e CI=true aseel/react-test npm run test
after_success:
- docker build -t aseel/multi-client ./client
- docker build -t aseel/multi-nginx ./nginx
- docker build -t aseel/multi-server ./server
- docker build -t aseel/multi-worker ./worker
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_ID" --password-stdin
- docker push aseel/multi-server
- docker push aseel/multi-nginx
- docker push aseel/multi-worker
- docker push aseel/multi-client
The reason the push is failing is because this command
docker build -t aseel/multi-client ./client
is failing on travis (but not locally).
More specifically, I'm getting this error on travis
COPY failed: stat
/var/lib/docker/overlay2/135282513e177be132b6978986f878ba61f3b89914b6b2f7030cbafa0d33f629/merged/app/build:
no such file or directory
Any idea why this is happening?
I needed to switch CMD to RUN -- It was a pretty silly mistake

Resources