Docker build Share data with host - docker

I hava a custom Dockerfile that setup and builds a project of mine.
But now I haven't beeing able to place that into a folder of the host. Here the script and docker file...
Command
sudo docker build --output type=local,dest=./build/server/server -f ./build/scripts/Dockerfile.server ./server
Dockerfile
FROM node:14 AS build-stage
WORKDIR /usr/src/project
RUN npm i nexe#3.3.7 -g
COPY package*.json ./
RUN npm install --only=production
COPY . .
RUN nexe server.js -t linux-x64-12.14.1
FROM scratch AS export-stage
COPY --from=build-stage /usr/src/project/server /

The docker buildkit needs to be enabled before running the build command:
export DOCKER_BUILDKIT=1
You are setting up the context wrong. The command should be:
sudo docker build --output type=local,dest=./build/server/server -f ./build/scripts/Dockerfile.server .
The dot at the end sets the build context to current directory structure.

Related

when I modify a file and rebuild on docker the changes are not applied

i have a problem on docker, i use docker during the developpement of my project to see how it works. But now, when i want to rebuild with my new code, nothing append, it's litteraly the same execution.
here is my dockerFile :
### STAGE 1: Build ###
FROM node as build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
COPY . .
RUN npm install -g #angular/cli
RUN npm install
RUN ng build --prod
### STAGE 2: Run ###
FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html
and here is the command i use to build and run my container :
docker build -t edtmaker .
docker run --name edtmaker -d -p 8888:80 edtmaker
Ok, i receive a comment who just says to clear the cache on the browser, and this worked

Docker build doesn't delete intermediate image even with '--rm' flag

I am trying to build react app in docker, here is my Dockerfile:
FROM node as build-step
LABEL stage=build-step
RUN mkdir /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
RUN npm run build
FROM nginx
COPY --from=build-step /app/build /usr/share/nginx/html
Using this command:
docker build . --rm -t react-server-manual:0.1
This works, but it is creating a few other images that's useless, how do I delete them?
What am I missing?
Unfortunately this --rm doesn't remove such intermediate images
You can run
docker build . -t react-server-manual:0.1 && \
docker image prune -f --filter label=stage=build-step
(or prune as separate command)

Docker cache-from not all layers being run if no files have changed

I've added a simple repo that recreates this issue: https://github.com/cgreening/docker-cache-problem
We have a very simple Dockerfile.
To speed up our builds we're using the --cache-from directive and using a previous build as a cache.
We're seeing some weird behaviour where if the files have not changed the lines after the COPY line are not being run.
RUN yarn && yarn build
Does not seem to get executed so when the application tries to start node_modules is missing.
FROM node
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN yarn && yarn build
ENTRYPOINT ["yarn", "start"]
We're deploying to Kubernetes but I can pull the image locally and see that the files are missing:
# docker run -it --entrypoint /bin/bash gcr.io/XXXXX
root#3561a9cdab6e:/app# ls
DEVELOPING.md Dockerfile Makefile README.md admin-tools app.dev.yaml jest.config.js package.json src tailwind.config.js tools tsconfig.json tslint.json yarn.lock
root#3561a9cdab6e:/app#
Edit:
I've managed to recreate the problem outside of our build system.
Initial build:
DOCKER_BUILDKIT=1 docker build -t gcr.io/XXX/test:a . --build-arg BUILDKIT_INLINE_CACHE=1
docker push gcr.io/XXX/test:a
All works - node_modules and build folder are there:
Clean up docker as if we starting from scratch like on the build system
docker system prune -a
Do another build:
DOCKER_BUILDKIT=1 docker build -t gcr.io/XXX/test:b . --cache-from gcr.io/XXX/test:a --build-arg BUILDKIT_INLINE_CACHE=1
docker push gcr.io/XXX/test:a
Everything is still fine.
Clean up docker as if we starting from scratch like on the build system
docker system prune -a
Do a third build:
DOCKER_BUILDKIT=1 docker build -t gcr.io/XXX/test:c . --cache-from gcr.io/XXX/test:b --build-arg BUILDKIT_INLINE_CACHE=1
Files are missing!
docker run -it --entrypoint /bin/bash gcr.io/topo-wme-dev-d725ec6e/test:c
root#d07f6f1d3b12:/app# ls
DEVELOPING.md Dockerfile Makefile README.md admin-tools app.dev.yaml coverage jest.config.js package.json src tailwind.config.js tools tsconfig.json tslint.json yarn.lock
No node_modules or build folder.

docker multiple stages - docker gradle build success but docker openjdk failed to build: COPY failed: no source files were specified

i am trying to build jar files in docker gradle, then docker openjdk copy the jar files and run it.
but hitting error
Step 10/14 : COPY --from=builder /test-command/build/libs/*.jar /app/test-command.jar
ERROR: Service 'test-command' failed to build: COPY failed: no source files were specified
docker file
FROM gradle:5.6.3-jdk8 as builder
COPY --chown=gradle:gradle . /test-command
ADD --chown=gradle . /app
WORKDIR /app
RUN gradle build
FROM ubuntu
FROM openjdk:8-alpine
WORKDIR /app
VOLUME ["/app"]
COPY --from=builder /test-command/build/libs/*.jar /app/test-command.jar
COPY --from=builder /test-command/docker/startup.sh /app/startup.sh
#RUN sh -c 'touch /app/test-command.jar'
RUN chmod +x /app/startup.sh
RUN chmod +x /app/test-command.jar
ENTRYPOINT ["/bin/sh", "/app/startup.sh"]
when the docker gradle building files, i can view them by using below docker command, and i saw the path, jar files in the container.
but once build is completed, then i did not see the container anymore.
could it be the reason why docker oepnjdk cannot find the files / source path??
docker exec -it name-of-container bash

Mounting Volume as part of a multi-stage build

How can I mount a volume to store my .m2 repo so I don't have to download the internet on every build?
My build is a Multi stage build:
FROM maven:3.5-jdk-8 as BUILD
COPY . /usr/src/app
RUN mvn --batch-mode -f /usr/src/app/pom.xml clean package
FROM openjdk:8-jdk
COPY --from=BUILD /usr/src/app/target /opt/target
WORKDIR /opt/target
CMD ["/bin/bash", "-c", "find -type f -name '*.jar' | xargs java -jar"]
You can do that with Docker >18.09 and BuildKit. You need to enable BuildKit:
export DOCKER_BUILDKIT=1
Then you need to enable experimental dockerfile frontend features, by adding as first line do Dockerfile:
# syntax=docker/dockerfile:experimental
Afterwards you can call the RUN command with cache mount. Cache mounts stay persistent during builds:
RUN --mount=type=cache,target=/root/.m2 \
mvn --batch-mode -f /usr/src/app/pom.xml clean package
Although the anwer from #Marek Obuchowicz is still valid, here is a small update.
First add this line to Dockerfile:
# syntax=docker/dockerfile:1
You can set the DOCKER_BUILDKIT inline like this:
DOCKER_BUILDKIT=1 docker build -t mytag .
I would also suggest to split the dependency resolution and packagin phase, so you can take the full advantage from Docker layer caching (if nothing changes in pom.xml it will use the cached layer with already downloaded dependencies). The full Dockerfile could look like this:
# syntax=docker/dockerfile:1
FROM maven:3.6.3-openjdk-17 AS MAVEN_BUILD
COPY ./pom.xml ./pom.xml
RUN --mount=type=cache,target=/root/.m2 mvn dependency:go-offline -B
COPY ./src ./src
RUN --mount=type=cache,target=/root/.m2 mvn package
FROM openjdk:17-slim-buster
EXPOSE 8080
COPY --from=MAVEN_BUILD /target/myapp-*.jar /app.jar
ENTRYPOINT ["java","-jar","/app.jar","-Xms512M","-Xmx2G","-Djava.security.egd=file:/dev/./urandom"]

Resources