Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker cant find file in parent folder?

I have a problem where docker build can't find my files and I've tried many folder references but I cant get my head around the folder structure. No matter what its wrong.

This is my project (slimmed down) structure:

enter image description here

This is my docker file:

# pull official base image
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env

# set working directory
WORKDIR /app

# add app csprojfiles
COPY ./myapi.sln .

and this is the error:

failed to compute cache key: "/myapi.sln" not found: not found

I've tried COPY ../myapi.sln . and simply myapi.sln but its not found no matter what. Im running command docker build web.api/ from the parent myapi folder in command prompt.

like image 741
u314 Avatar asked Sep 05 '25 03:09

u314


2 Answers

Your path is wrong. By writing COPY ./myapi.sln . you assume that myapi.sln is in the same folder than the Dockerfile which is not the case.

Best practice is to put the Dockerfile at the root of your project so you can easily access all your files.

In anyway, you can't access a parent directory with absolute or relative path.

From the documentation:

The path must be inside the context of the build; you cannot COPY ../something/something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

like image 59
Jeremy Avatar answered Sep 07 '25 21:09

Jeremy


I had the same problem and I solved it by using docker-compose.

Let's assume we have the following directory structure:

├───Project
│   ├───docker
│   │   └───frontend.dockerfile
│   └───frontend-app
│       ├───index.html
│       └───package.json
└───────docker-compose.yml

In the frontend.dockerfile, we use the node:16 base image, set the working directory to /app, and copy the package.json file to the working directory. We then run npm install to install the dependencies and copy the application files to the working directory. Finally, we set the command to npm start.

frontend.dockerfile

FROM node:16

WORKDIR /app

COPY ./frontend-app/package.json .

RUN npm install

COPY /frontend-app .

CMD ["npm", "start"]

In the docker-compose.yml file, we define a service called frontend that uses the frontend.dockerfile for building. We map port 3000 from the container to port 3000 on the host and mount the frontend-app directory as a volume. We set the command to npm run install to install the dependencies.

docker-compose.yml

version: '3'

services:
  frontend:
    build:
      context: .
      dockerfile: ./docker/frontend.dockerfile
    ports:
      - "3000:3000"
    volumes:
      - ./frontend-app:/app
    command: npm run install

In the build section of the frontend service, we specify the path to the frontend.dockerfile and set the context to the root of the project directory.

like image 42
Abdes Avatar answered Sep 07 '25 23:09

Abdes