Home > Net >  Can't build docker compose
Can't build docker compose

Time:12-05

This is my code for Dockerfile:

FROM python:3.8
WORKDIR /py-api-yahoo-finance
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY api_yahoo .

And this is my docker-compose.yml file:

version: "3.8"
services:
  py-api-yahoo-finance:
    build: .
    ports:
      - "5000:5000"
    container_name: api_yahoo
    command: python manage.py runserver 0.0.0.0:5000

I try to build the image by the command: docker-compose build and then I encounter the following error:

failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount974641243/Dockerfile: no such file or directory
ERROR: Service 'py-api-yahoo-finance' failed to build : Build failed

I tried to follow all the tutorials I found online but none of the answers helped. I would appreciate your help, thanks.

CodePudding user response:

It looks like the issue might be with the path specified in the Dockerfile. In the WORKDIR command, you're specifying /py-api-yahoo-finance as the working directory, but in the COPY command you're copying files from the current directory (.) to the working directory.

This means that the requirements.txt file and the api_yahoo directory won't be found in the working directory, and the build will fail.

To fix this, you can either specify the full path to the requirements.txt file and api_yahoo directory in the COPY command, or you can move the Dockerfile to the directory containing the requirements.txt file and api_yahoo directory and run the docker-compose command from there. This way, the COPY command will be able to find the files in the current directory.

Here's an example of how your Dockerfile and docker-compose.yml files could look after making these changes:

Dockerfile:

FROM python:3.8
WORKDIR /py-api-yahoo-finance
COPY requirements.txt /py-api-yahoo-finance/requirements.txt
RUN pip3 install -r requirements.txt
COPY api_yahoo /py-api-yahoo-finance/api_yahoo

docker-compose.yml:

version: "3.8"
services:
  py-api-yahoo-finance:
    build: .
    ports:
      - "5000:5000"
    container_name: api_yahoo
    command: python manage.py runserver 0.0.0.0:5000

Make sure to run the docker-compose build command from the directory containing the Dockerfile, requirements.txt file, and api_yahoo directory. This should fix the issue and allow the image to build successfully.

EDIT

It's possible that there is an issue with the path specified in the docker-compose.yml file. In the build section, you're specifying the current directory (.) as the path to the Dockerfile, but it's possible that the Dockerfile isn't in the current directory when you run the docker-compose command.

To fix this, you can specify the full path to the Dockerfile in the build section of the docker-compose.yml file. This way, docker-compose will be able to find the Dockerfile and build the image successfully.

Here's an example of how your docker-compose.yml file could look after making this change:

version: "3.8"
services:
py-api-yahoo-finance:
build: /path/to/Dockerfile
ports:
- "5000:5000"
container_name: api_yahoo
command: python manage.py runserver 0.0.0.0:5000

Make sure to specify the correct path to the Dockerfile in the build section. This should fix the issue and allow you to build the image successfully.

If you continue to have issues, it may be helpful to check the permissions on the Dockerfile, requirements.txt file, and api_yahoo directory to make sure they are readable by the user running the docker-compose command. You can use the ls -l command to check the permissions on these files and directories, and use the chmod command to change the permissions if necessary.

For example, if the Dockerfile has permissions set to -rw-rw-rw-, you can use the following command to make it readable by everyone:

chmod a r Dockerfile

This will add read permission for all users on the Dockerfile, and you should be able to build the image successfully.

  • Related