skip to Main Content

I have a directory and it’s structure is something like this:

.
├── docker
│  ├── docker-compose.yml
│  └── Dockerfile
├── java-app
│  ├── pom.xml
│  └── src

I want to create a docker image without changing the structure but I am getting this error:

 => [internal] load build context                                                                                                  0.0s
 => => transferring context: 2B                                                                                                    0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                      0.0s
 => ERROR [3/5] COPY java-app/pom.xml /app/                                                                     0.0s
 => ERROR [4/5] COPY java-app/src /app/src                                                                      0.0s
------
 > [3/5] COPY java-app/pom.xml /app/:
------
------
 > [4/5] COPY java-app/src /app/src:
------
Dockerfile:8
--------------------
   6 |     
   7 |     # Copy the necessary files to the working directory
   8 | >>> COPY java-app/pom.xml /app/
   9 |     COPY java-app/src /app/src
  10 |     
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref b1c85d8c-d082-47bf-843c-99a804945668a::o15zbeasfb4ieh2fetvzcczhp4a: "/java-app/pom.xml": not found

This is my Dockerfile

# Use a base image with Java 11 installed
FROM eclipse-temurin:11-jdk

# Set the working directory inside the container
WORKDIR /app

# Copy the necessary files to the working directory
COPY ../java-app/pom.xml /app/
COPY ../java-app/src /app/src

# Build the application
RUN ./mvn package -DskipTests

# Expose the port on which the application will run
EXPOSE 8080

# Start the application
CMD ["java", "-jar", "target/your-app.jar"]

What am I doing wrong here? I would appreciate some help

3

Answers


  1. Docker sends the set of files around the docker file to the server (called “context”)

    The server sees this copy only. You cannot go outside of it.

    Move the docker file.

    Login or Signup to reply.
  2. Docker will set the context folder with the folder which you run the command. You can run from outside of Dockerfile

    docker build -f docker/Dockerfile .
    

    And in the Dockerfile you can edit path 2 line with COPY

    COPY java-app/pom.xml /app/
    COPY java-app/src /app/src
    
    Login or Signup to reply.
  3. From the documentation:

    By default the docker build command will look for a Dockerfile at the root of the build context. The -f, –file, option lets you specify the path to an alternative file to use instead.

    so you could, from the java app directory, use:

    docker build -f ../docker/Dockerfile .
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search