简体   繁体   中英

Error while docker image build with below dockerfile

Dockerfile

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG DEPENDENCY=target/dependency
COPY ./target/dependency/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

Error :

    Sending build context to Docker daemon  36.83MB
Step 1/8 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/8 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> e9748d9baac5
Step 3/8 : USER spring:spring
 ---> Using cache
 ---> 25b83c6d3e7a
Step 4/8 : ARG DEPENDENCY=target/dependency
 ---> Using cache
 ---> 24f648b8f9ef
Step 5/8 : COPY ./target/dependency/BOOT-INF/lib /app/lib
COPY failed: stat /var/lib/docker/tmp/docker-builder226394005/target/dependency/BOOT-INF/lib: no such file or directory

1). ARG DEPENDENCY=target/dependency -> this is not working that is why I did not change the same for other COPY lines in Dockerfile

2). I have a directory created as target/dependency/BOOT-INF/lib which contains all dependencies still, it says in error: no such file or directory.

Please help with some solution and with a proper explanation regarding Dockerfile. Any advice on Dockerizing Springboot application with Postgres DB is most welcomed.

UPDATED Dockerfile:

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
RUN adduser spring root
COPY . /home/app
WORKDIR /home/app
RUN chmod -R 775 /home/app
RUN mkdir -p /home/app/lib 
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /home/app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

UPDATED Error:

   Sending build context to Docker daemon  36.83MB
Step 1/12 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/12 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> e9748d9baac5
Step 3/12 : RUN adduser spring root
 ---> Using cache
 ---> 444285d4e8d1
Step 4/12 : COPY . /home/app
 ---> 0bf717362f3e
Step 5/12 : WORKDIR /home/app
 ---> Running in 9f967425a899
Removing intermediate container 9f967425a899
 ---> bcb4e22f9faa
Step 6/12 : RUN chmod -R 775 /home/app
 ---> Running in 3110a9dd9059
Removing intermediate container 3110a9dd9059
 ---> c9ad2d9f411d
Step 7/12 : RUN mkdir -p /home/app/lib
 ---> Running in 48a172151d8b
Removing intermediate container 48a172151d8b
 ---> 11dbe4b4e21f
Step 8/12 : ARG DEPENDENCY=target/dependency
 ---> Running in 31a21dfd103f
Removing intermediate container 31a21dfd103f
 ---> ac340462402c
Step 9/12 : COPY ${DEPENDENCY}/BOOT-INF/lib /home/app/lib
COPY failed: stat /var/lib/docker/tmp/docker-builder273428680/target/dependency/BOOT-INF/lib: no such file or directory

In fact the issue cause is not so complex:

COPY ./target/dependency/BOOT-INF/lib /app/lib

COPY failed: stat /var/lib/docker/tmp/docker-builder226394005/target/dependency/BOOT-INF/lib: no such file or directory

What does it mean?
If these directories effectively exist on your host, it means that the target directory is not a direct child directory of the build context.
What is the build context ?

The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

When you execute: docker build -t myTag. , the context is the current directory.
So just adjust the build context to be a parent directory of the target directory and it should be correct.
For example with the layout:

- docker
      - Dockerfile
- src
- target

You should keep the context as the base directory and set the DockerFile path in that way:

docker build -t myTag -f docker/Dockerfile . 

As discussed in the comment, the copy command work base on build context, so that's is why the build as target is outside of build context.

If Dockerfile inside target folders then the copy commands should be

copy dependency/BOOT-INF/lin /home/app/

Is the /app folder in your container already exist? If not, just create one before copy files to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM