Docker is telling me it cant find the specified file. Is my context wrong? My folder structure is as follows.
root
- docker
- docker-compose-service.yml
- service
- DockerFile
Nuget.config
Inside my docker-compose-service.yml file I have
build:
context: .
dockerfile: ../service/DockerFile
I run from the root folder the following command
docker-compose -f docker/docker-compose-service.yml up --build
It can find the Dockerfile as it runs the code but fails on a line inside the dockerFile which is
COPY NuGet.config .
The error is
failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder408021827/NuGet.config: no such file or directory
What am I doing wrong? I played around with the context and dockerFile location but then it couldn't find the DockerFile
Hmm so if I run the command from the docker folder and change the docker-compose-service.yml file to be
context: ../
dockerfile: service/DockerFile
That seems to work
Usually, from what I can see in projects, context's are build in a different way. You should reorganize your project like this:
root
- docker-compose.yml
- service
- Dockerfile
- Nuget.config
The build in the compose file would look like this afterwards:
build: ./service
And you run it like:
docker-compose up --build
The path specified in your docker file sets the context as the parent folder of the compose file.
In order to make it work, you need to either:
Change the context to .
Change the path to NuGet.config to also contain the current folder name: root/NuGet.config
I would recommend the first approach as from your question I don't see any reason you would want to refer to the parent folder.
Related
I'm trying to run a discord.py bot on a docker container. But when I'm running the container, docker says that I'm "missing a module". The Dockerfile its not copying all the files/folders from the source code.
This is my directory:
These are the contents of my docker-compose.yml:
version: '3'
services:
bot:
build: .
restart: always
volumes:
- ./.env:/usr/src/app/.env
This is my Dockerfile:
FROM python:bullseye
WORKDIR /usr/app/src
COPY bot bot
CMD ["python", "-m", "bot"]
When I run # sudo docker compose up It fails with the following log:
Checking the docker image files, it seems like its copying all the contents inside of the bot folder, but its not copying the folder itself.
The code works fine if I run it outside of the container, so is not related to this discord bot code.
How can I fix this?
This is my first docker container I'm new really with this.
The correct syntax should be:
COPY bot bot/
By design, COPY always copies the contents of the directory if the source is a directory, and by adding the trailing / to the destination you tell docker that the destination is a directory, so it will create it for you if needed.
See the full documentation.
Im trying to run a storybook container for react.
Storybook is making new files in app/frontend/src/stories in the build process.
I would like to add the files I have in my /frontend/src/stories folder and modify these files and i would like first add the files and then the modifications to take effect in the container app/frontend/src/stories folder.
How can i do that ?
Here is my docker-compose.yml file
nginx_storybook:
build:
context: .
dockerfile: ./compose/production/storybook/Dockerfile
restart: always
volumes:
- staticfiles:/app/static
- mediafiles:/app/media
- /app/frontend/node_modules
- ./frontend/src/components:/app/frontend/src/components
- ./frontend/src/stories:/app/frontend/src/stories
The volume configuration doesnt work : no files in my ./frontend/src/stories appear in the container /app/frontend/src/stories folder and reciprocally...
I used to do -v myfolder:containerfolder and i used to work with docker, but i dont know how to do with docker-compose
I seems the user used in the container doesn't have enough permissions to see the files and edit them
Try adding the following line at the end of your Dockerfile
USER <some UID> # USER 5000
then in the host call the following command
sudo chown -R <the same UID> ./frontend/src/stories
I have a docker-compose setup something like:
/
- sources/
- docker-compose.yml
- Dockerfile
- .dockerignore
- many more files
The Dockerfile contains instructions including a COPY command of the sources.
Because of all the different tools, including multiple docker setups, I'd like to organise it a bit, by either moving all files to a folder:
/
- sources/
- docker/
- many more files
or leaving just the docker-compose.yml file outside of this folder:
/
- sources/
- docker/
- many more files
I'd like to do this because:
It cleans up the project folder
I currently have multiple docker setups in the project folder, moving them to seperate folders allows for a more clear and/or precise setup (e.g. multiple dockerignore files)
Currently I am running into some issues which do make sense, such as:
COPY failed: Forbidden path outside the build context: ../sources/
Is it possible to achieve this setup? Thanks!
Inside the Dockerfile, you cannot access files that are outside the build context. In your case the build context is the directory containing the Dockerfile.
You can change the build context inside the composefile.
Below is an example where the composefile is at the root and Dockerfile is under docker folder:
version: '3'
services:
test:
build:
context: .
dockerfile: docker/Dockerfile
In this case, inside the Dockerfile the file paths should be set relative to the context.
COPY sources sources
For dockerignore:
As specified in the docs for .dockerignore file:
Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context
Thus you need to add the dockerignore file to the root of the context.
You can't use that within the Dockerfile however you should be able to make it work using a .env file and pulling it in from there.
https://docs.docker.com/compose/env-file/
You could try something like:
.env
SOURCE_PATH=../sources/
Dockerfile
COPY ${SOURCE_PATH}/myfile /some/destination
Here the structure of my project(debug_project) folder.
This is my Docker compose command.
After executing the command, I get this error.
Here is my docker-compose-dev.yml file.
This is the link I have followed.
How to resolve this error?
It is assumed that the Dockerfile is present relative to the folder specified in the context field.
So you have two choices either move the Dockerfile inside the app directory or have an absolute path to the Dockerfile.
Something like
foo:
build:
context: app
dockerfile: ${PWD}/Dockerfile-dev
The context specifies the path to the directory containing the docker file. You need to move the Dockerfile-dev into that directory.
https://docs.docker.com/compose/compose-file/#build
Either a path to a directory containing a Dockerfile, or a url to a git repository.
When the value supplied is a relative path, it is interpreted as relative to the location of the Compose file. This directory is also the build context that is sent to the Docker daemon.
I have the following project structure:
.
..
project/
docker/cli/Dockerfile
docker-compose.yml
In docker-compose.yml I have the following configuration:
cli:
build: docker/cli
And somewhere in my Dockerfile:
COPY . /app
Now the problem is that when I do docker-compose build cli docker copies the contents of docker/cli/ in /app in my image. Which makes sense because that is the relative path to my docker/cli/Dockerfile. Is there a way however to tell in my docker-compose.yml config that the path should be different (namely the root dir of my project where the actual project files are)?
You can use the context attribute of a build instruction inside the docker-compose file.
version: '2'
services:
cli:
build:
context: .
dockerfile: docker/cli/Dockerfile
This should work as desired. The context is what is sent to the docker daemon for build. You will also want a .dockerignore file in your project directory though, to ensure only what you need is sent.