Docker build not using cache - docker

When I run
docker build -f docker/webpack.docker services/webpack --build-arg env=production
twice in a row, Docker builds my image each time, starting from the first RUN (the COPY uses the cache).
FROM node:lts
ARG env=production
ENV NODE_ENV=$env
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production=false --non-interactive
COPY . .
RUN node --max-old-space-size=20000 node_modules/.bin/svg2fonts icons -o assets/markons -b mrkn -f markons -n Markons
RUN node --max-old-space-size=20000 node_modules/.bin/webpack --progress
How can I get it to cache those RUNs?
Output looks like:
Sending build context to Docker daemon 3.37MB
Step 1/9 : FROM node:lts
---> 0c601cba9f11
Step 2/9 : ARG env=production
---> Using cache
---> dd38b2167c75
Step 3/9 : ENV NODE_ENV=$env
---> Using cache
---> 800f5afd416c
Step 4/9 : WORKDIR /app
---> Using cache
---> d15b93dce11d
Step 5/9 : COPY package.json yarn.lock ./
---> Using cache
---> a049dd1609a8
Step 6/9 : RUN yarn install --frozen-lockfile --production=false --non-interactive
---> Using cache
---> d5e51b0d556c
Step 7/9 : COPY . .
---> 92990e326d4b
Step 8/9 : RUN node --max-old-space-size=20000 node_modules/.bin/svg2fonts icons -o assets/markons -b mrkn -f markons -n Markons
---> Running in a23878db7b0e
Wrote assets/markons/markons.css
Wrote assets/markons/markons.js
Wrote assets/markons/markons.html
Wrote assets/markons/markons-chars.json
Wrote assets/markons/markons.svg
Wrote assets/markons/markons.ttf
Wrote assets/markons/markons.woff
Wrote assets/markons/markons.woff2
Wrote assets/markons/markons.eot
Removing intermediate container a23878db7b0e
---> 3bce79d0ecf0
Step 9/9 : RUN node --max-old-space-size=20000 node_modules/.bin/webpack --progress
---> Running in b6d460488950
<s> [webpack.Progress] 0% compiling
...

See the description:
If the contents of all external files on the first COPY command are
the same, the layer cache will be used and all subsequent commands
until the next ADD or COPY command will use the layer cache.
However, if the contents of one or more external files are different,
then all subsequent commands will be executed without using the layer
cache.
So every time the content is changed two last RUN will be executed with no cache. There is no way to control caching yet. Maybe it's a better option to specify volumes?

Related

Docker and Angular prod `file does not exist` Error on stage build COPY command

i do not understand why docker cannot get my angular build folder in container.
Can you see that to help me?
If i build with docker compose command i have this error.
Below are all the steps to build my image and launch my container until the error.
WARNING: The Docker Engine you're using is running in swarm mode.
Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.
To deploy your application across the swarm, use `docker stack deploy`.
Building linking-front
Sending build context to Docker daemon 425.6MB
Step 1/9 : FROM node:16.19.0 AS build
---> b22f8aab05da
Step 2/9 : WORKDIR /usr/src/app
---> Using cache
---> 5e2431455b65
Step 3/9 : COPY package.json package-lock.json ./
---> Using cache
---> 11d677269b0e
Step 4/9 : RUN npm install
---> Using cache
---> b5544be9159b
Step 5/9 : COPY . .
---> Using cache
---> 3403bfda57ca
Step 6/9 : RUN npm run build
---> Using cache
---> ae8e7960ac33
Step 7/9 : FROM nginx:1.23.3-alpine
---> 2bc7edbc3cf2
Step 8/9 : COPY nginx.conf /etc/nginx/nginx.conf
---> Using cache
---> beca38c7be94
Step 9/9 : COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
COPY failed: stat usr/src/app/dist/linkingEducationSecurity-front: file does not exist
ERROR: Service 'linking-front' failed to build : Build failed
### STAGE 1: Build ###
FROM node:16.19.0 AS build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build
### STAGE 2: Run ###
FROM nginx:1.23.3-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
I use also docker-compose
version: '3.9'
services:
linking-front:
build: ./linkingEducationSecurity-front/
ports:
- "8080:80"
volumes:
- type: bind
source: ./linkingEducationSecurity-front/src/
target: /app/src
since in the comments you tried to do RUN cd dist && ls which gave you this output :
Step 7/10 : RUN cd dist && ls ---> Running in e8f002e82f3a linking-education-security-front
The steps and dockerfile are perfect. the COPY command from build folder is missing its spell
update this line :
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
to this :
COPY --from=build /usr/src/app/dist/linking-education-security-front /usr/share/nginx/html
and try rebuilding , this might work.

building golang using your docker image

Good day everyone. I'm in the middle of a task using our docker image to build go microservice. To reduce build time, keep the images under control.
I expect that after downloading the golang image from the docker hub, upload it to our registry and use it in all the dockerfiles of our microservices.
As soon as I started building go mircoservice, during the installation phase go gave me an error:
11:04:16 [91m / bin / sh: go: not found
11:04:16 [0mThe command '/ bin / sh -c go install -mod vendor' returned a non-zero code: 127
I tried to install go in a container, write $ PATH for go, but nothing helped. Then I was told that this image already contains go.
Here are the complete build steps:
11:04:15 Step 1/7 : FROM 192.168.0.8:5000/golang
11:04:15 ---> 49f356fa4513
11:04:15 Step 2/7 : EXPOSE 80
11:04:15 ---> Using cache
11:04:15 ---> 6e0c49e600e6
11:04:15 Step 3/7 : WORKDIR /go/src/autocomplete-trains
11:04:15 ---> Using cache
11:04:15 ---> 41cccfb1ed82
11:04:15 Step 4/7 : COPY . .
11:04:15 ---> 1a44653382a8
11:04:15 Step 5/7 : RUN chmod +x ./start.sh
11:04:15 ---> Running in c13b5cfbfd0b
11:04:16 Removing intermediate container c13b5cfbfd0b
11:04:16 ---> ab03d13e5984
11:04:16 Step 6/7 : RUN go install -mod vendor
11:04:16 ---> Running in b3727493f938
11:04:16 [91m/bin/sh: go: not found
11:04:16 [0mThe command '/bin/sh -c go install -mod vendor' returned a non-zero code: 127
11:04:17 Build step 'Execute shell' marked build as failure
Dockerfile:
FROM 192.168.0.8:5000/golang
EXPOSE 80
WORKDIR /go/src/autocomplete-trains
COPY . .
RUN chmod +x ./start.sh
RUN go install -mod vendor
ENTRYPOINT ./script.sh
Note: If FROM 192.168.0.8:5000/golang:1.15.5-alpine is changed to FROM golang then everything works (but we don't wish to rely upon dockerhub so this is not a viable solution).
The process used to push the image into our repository was:
sudo docker pull golang
sudo docker tag golang:latest 192.168.0.8:5000/golang:1.15.5-alpine
sudo docker push 192.168.0.8:5000/golang:1.15.5-alpine

.NET Core web app won't run in Docker container

I have a vanilla .NET Core 2 web app that I setup in JetBrains Rider and I immediately started working on a Docker environment for it. I followed this guide to get started:
https://docs.docker.com/engine/examples/dotnetcore/
I altered it slightly to come up with this:
FROM microsoft/dotnet:latest AS packager
RUN mkdir -p /opt/build
WORKDIR /opt/build
# Copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin
# --
# Build runtime image
FROM microsoft/dotnet:runtime AS runtime
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY --from=packager /opt/build/bin/. .
ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
The image builds and when I go to run the container I get the following output:
dan#mycomputer ~/Desktop/coreapi (master)
$ docker build -t myapp .
Sending build context to Docker daemon 25.09kB
Step 1/12 : FROM microsoft/dotnet:latest AS packager
---> e1a56dca783e
Step 2/12 : RUN mkdir -p /opt/build
---> Using cache
---> 95f9c936d0d1
Step 3/12 : WORKDIR /opt/build
---> Using cache
---> 64f26c356fd7
Step 4/12 : COPY *.csproj .
---> Using cache
---> 38a2fb7ca6bb
Step 5/12 : RUN dotnet restore
---> Using cache
---> 70dbc44d98ae
Step 6/12 : COPY . .
---> Using cache
---> b1019d53a861
Step 7/12 : RUN dotnet publish -c Release -o bin
---> Using cache
---> 8e112606633a
Step 8/12 : FROM microsoft/dotnet:runtime AS runtime
---> cc240a7fd027
Step 9/12 : RUN mkdir -p /opt/app
---> Using cache
---> 954f494febc4
Step 10/12 : WORKDIR /opt/app
---> Using cache
---> b74be941e7dc
Step 11/12 : COPY --from=packager /opt/build/bin/. .
---> Using cache
---> 4c229192d99b
Step 12/12 : ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
---> Using cache
---> fb6ef4015fba
Successfully built fb6ef4015fba
Successfully tagged myapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
dan#mycomputer ~/Desktop/coreapi (master)
$ docker run -p 5001:5001 myapp:latest
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
The app doesn't run and I get a message that says I need to install the SDK. What's up with that? Shouldn't the runtime Docker image have everything neede to run the app?
I was able to find the solution by modifying the ENTRYPOINT to run tail -f /dev/null. From there, I entered the container and saw that the name of the binary adjusts based on your project name which the Docker documentation didn't make clear to me.
I also updated my base images and this solved my problem. Here is my latest Dockerfile below:
FROM microsoft/dotnet:2.1-sdk AS packager
RUN mkdir -p /opt/build
WORKDIR /opt/build
# Copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin
# --
# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY --from=packager /opt/build/bin/. .
EXPOSE 80
ENTRYPOINT ["dotnet", "/opt/app/coreapi.dll"]

How to run go server on docker from binary

I have been trying to make a Dockerfile, that would let me build my go server as binary and then run it either from the scratch image or alpine. The server works fine locally, on macOS 10.13.5, and I made it work when it wasn't from binary on Docker.
I keep getting this error:
standard_init_linux.go:190: exec user process caused "exec format error"
I have been googling around and found something about system architecture. I am not sure how to check if that is the error and/or how to fix it.
Any hints for debugging or possible fix are much appreciated.
My Dockerfile:
FROM golang:1.10.3 as builder
WORKDIR /go/src/gitlab.com/main/server
COPY . .
RUN go get -d -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
FROM scratch
ADD main /
CMD ["/main"]
The output:
Building go
Step 1/9 : FROM golang:1.10.3 as builder
---> 4e611157870f
Step 2/9 : WORKDIR /go/src/gitlab.com/main/server
Removing intermediate container 20cd4d66008b
---> 621d9fc02dde
Step 3/9 : COPY . .
---> cab639571baf
Step 4/9 : RUN go get -d -v ./...
---> Running in 7681f9adc7b2
Removing intermediate container 7681f9adc7b2
---> 767a4c9dfb94
Step 5/9 : RUN go build -a -installsuffix cgo -o main .
---> Running in a6ec73121163
Removing intermediate container a6ec73121163
---> b9d7d1c0d2f9
Step 6/9 : FROM alpine:latest
---> 11cd0b38bc3c
Step 7/9 : WORKDIR /app
---> Using cache
---> 6d321d334b8f
Step 8/9 : COPY . .
---> 048a59fcdd8f
Step 9/9 : CMD ["/app/main"]
---> Running in d50d174644ff
Removing intermediate container d50d174644ff
---> 68f8f3c6cdf7
Successfully built 68f8f3c6cdf7
Successfully tagged main_go:latest
Creating go ... done
Attaching to go
go | standard_init_linux.go:190: exec user process caused "exec format error"
go exited with code 1
As #tgogos pointed out did I need to use what I build in the first step.
My final Dockerfile ended like this with a few further improvements: The important part is second last line though:
FROM golang:1.10.3 AS build
WORKDIR /go/src/gitlab.com/main/server
COPY . .
RUN go get github.com/golang/dep/cmd/dep && \
dep ensure && \
rm -f schema/bindata.go && \
go generate ./schema
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
FROM alpine
RUN apk add --no-cache ca-certificates
COPY --from=build /go/src/gitlab.com/main/server/main .
CMD ["/main"]

Docker Cloud autotest doesn't exit after running tests

I'm trying to automatically test PRs to my project using Docker Cloud. I've set up a build rule as follows:
Dockerfile:
FROM node:8.4.0-alpine
ENV NODE_ENV=production
WORKDIR /olimat/api
COPY package.json package-lock.json ./
RUN npm install --quiet
COPY ./public ./public
COPY ./config ./config
COPY ./src ./src
CMD npm start
Dockerfile.dev:
FROM node:8.4.0-alpine
WORKDIR /olimat/api
COPY package.json package-lock.json ./
RUN npm install --quiet
COPY ./public ./public
COPY ./config ./config
COPY ./src ./src
COPY ./db ./db
docker-compose.test.yml:
version: '3.2'
services:
sut:
build:
context: ./
dockerfile: Dockerfile.dev
command: npm test
depends_on:
- api
environment:
NODE_ENV: test
api:
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
db:
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: dev123
image: postgres:9.6.4-alpine
Running the tests locally, with docker-compose -f docker-compose.test.yml run sut Everything works fine:
On Docker Cloud, the tests run, but it seems to never return the exit code:
I've canceled after 1 hour and 46 minutes here. What's happening? How can I make the sut service container exit after the tests are run?
The complete build log:
Building in Docker Cloud's infrastructure...
Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '192.30.253.112' to the list of known hosts.
Reset branch 'master'
Your branch is up-to-date with 'origin/master'.
Pulling cache layers for index.docker.io/unemat/olimat-backend:latest...
Done!
KernelVersion: 4.4.0-93-generic
Arch: amd64
BuildTime: 2017-08-17T22:50:04.828747906+00:00
ApiVersion: 1.30
Version: 17.06.1-ce
MinAPIVersion: 1.12
GitCommit: 874a737
Os: linux
GoVersion: go1.8.3
Starting build of index.docker.io/unemat/olimat-backend:latest...
Step 1/9 : FROM node:8.4.0-alpine
---> 016382f39a51
Step 2/9 : ENV NODE_ENV production
---> Running in b0aa12f6d329
---> 8c0420481faa
Removing intermediate container b0aa12f6d329
Step 3/9 : WORKDIR /olimat/api
---> 669997c76951
Removing intermediate container b9344977ce13
Step 4/9 : COPY package.json package-lock.json ./
---> 562fb1b9d9db
Removing intermediate container 3778fb63cd12
Step 5/9 : RUN npm install --quiet
---> Running in 459a90d4ce4f
> uws#0.14.5 install /olimat/api/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0
added 261 packages in 19.34s
---> a22bd7c951bd
Removing intermediate container 459a90d4ce4f
Step 6/9 : COPY ./public ./public
---> 3555f3f71011
Removing intermediate container f6343f447c14
Step 7/9 : COPY ./config ./config
---> ffebbe0eae44
Removing intermediate container 1b6a25d1b044
Step 8/9 : COPY ./src ./src
---> ae66609e0177
Removing intermediate container a139a0a67b34
Step 9/9 : CMD npm start
---> Running in b1bc735877c5
---> fba69367a862
Removing intermediate container b1bc735877c5
Successfully built fba69367a862
Successfully tagged unemat/olimat-backend:latest
Starting Test in docker-compose.test.yml...
db uses an image, skipping
Building api
Step 1/9 : FROM node:8.4.0-alpine
---> 016382f39a51
Step 2/9 : ENV NODE_ENV production
---> Using cache
---> 8c0420481faa
Step 3/9 : WORKDIR /olimat/api
---> Using cache
---> 669997c76951
Step 4/9 : COPY package.json package-lock.json ./
---> Using cache
---> 562fb1b9d9db
Step 5/9 : RUN npm install --quiet
---> Using cache
---> a22bd7c951bd
Step 6/9 : COPY ./public ./public
---> Using cache
---> 3555f3f71011
Step 7/9 : COPY ./config ./config
---> Using cache
---> ffebbe0eae44
Step 8/9 : COPY ./src ./src
---> Using cache
---> ae66609e0177
Step 9/9 : CMD npm start
---> Using cache
---> fba69367a862
Successfully built fba69367a862
Successfully tagged bs3klcfwuijavr4uf4daf28_api:latest
Building sut
Step 1/9 : FROM node:8.4.0-alpine
---> 016382f39a51
Step 2/9 : MAINTAINER Josias Iquabius
---> Running in ed1306bea19a
---> 5956fb44e0cc
Removing intermediate container ed1306bea19a
Step 3/9 : WORKDIR /olimat/api
---> be7fd8615cd4
Removing intermediate container 2bde5cfe6bdd
Step 4/9 : COPY package.json package-lock.json ./
---> b68a99364f80
Removing intermediate container d0f4715b4774
Step 5/9 : RUN npm install --quiet
---> Running in f9f053df7774
> uws#0.14.5 install /olimat/api/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0
added 666 packages in 32.983s
---> 8f2ace5a6f9e
Removing intermediate container f9f053df7774
Step 6/9 : COPY ./public ./public
---> 0cac78c670e2
Removing intermediate container ab0f50cbc747
Step 7/9 : COPY ./config ./config
---> ce57c484d544
Removing intermediate container 126828beed7d
Step 8/9 : COPY ./src ./src
---> 7cd682b0f4d9
Removing intermediate container 819d441c2307
Step 9/9 : COPY ./db ./db
---> 244561b4bc52
Removing intermediate container 1a80d8f935b4
Successfully built 244561b4bc52
Successfully tagged bs3klcfwuijavr4uf4daf28_sut:latest
Creating network "bs3klcfwuijavr4uf4daf28_default" with the default driver
Pulling db (postgres:9.6.4-alpine)...
9.6.4-alpine: Pulling from library/postgres
Digest: sha256:5fd73de311d304caeb4f907d4f559d322805abc622e4baf5788c6a079ee5224e
Status: Downloaded newer image for postgres:9.6.4-alpine
Creating bs3klcfwuijavr4uf4daf28_db_1 ...
Creating bs3klcfwuijavr4uf4daf28_db_1
Creating bs3klcfwuijavr4uf4daf28_db_1 ... done Creating bs3klcfwuijavr4uf4daf28_api_1 ...
Creating bs3klcfwuijavr4uf4daf28_api_1
Creating bs3klcfwuijavr4uf4daf28_api_1 ... done Creating bs3klcfwuijavr4uf4daf28_sut_1 ...
Creating bs3klcfwuijavr4uf4daf28_sut_1
Creating bs3klcfwuijavr4uf4daf28_sut_1 ... done
npm info it worked if it ends with ok
npm info using npm#5.3.0
npm info using node#v8.4.0
npm info lifecycle olimat-backend#0.0.1~pretest: olimat-backend#0.0.1
npm info lifecycle olimat-backend#0.0.1~test: olimat-backend#0.0.1
> olimat-backend#0.0.1 test /olimat/api
> jest
PASS src/services/questions/questions.test.js
● Console
console.log src/models/questions.model.js:10
questions table does not exists!
info: after: questions - Method: find
PASS src/app.test.js
Test Suites: 2 passed, 2 total
Tests: 5 passed, 5 total
Snapshots: 0 total
Time: 6.505s
Ran all test suites.
Build canceled.
ERROR: Build failed with exit code 3
Build in 'master:/api' (4eeca024) canceled after 1:46:31

Resources