I want to copy the contents of a parent directory (relative to the position of the Dockerfile) into my image.
This is the folder structure:
app-root/
docker/
php81aws/
some-folder
Dockerfile
start-container
supervisord.conf
app_folders
app_files
I'm calling docker build as follows:
app-root#> docker build -t laravel -f docker/php81aws/Dockerfile .
Or from docker compose with:
services:
laravel:
build:
context: ./
dockerfile: docker/php81aws/Dockerfile
Therefore, the context should be in the app-root directory.
In the dockerfile, I'm using COPY like so:
COPY docker/php81aws/start-container /usr/local/bin/start-container
COPY docker/php81aws/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN chmod +x /usr/local/bin/start-container
COPY --chown=www:www . /var/www/
It always gives me this error:
failed to compute cache key: "/start-container" not found: not found
I tried to use COPY ./docker/php81aws/start-container and COPY start-container but the error is always the same. Of course copying the parent directory also fails.
You mention in a comment that your top-level app-root directory has a .dockerignore file that excludes the entire docker directory. While the Dockerfile will still be available, nothing else in that tree can be COPYed into the image, and if you COPY ./ ./ to copy the entire build context into the image, that directory won't be present.
Deleting this line from the .dockerignore file should fix your issue.
In general you want the .dockerignore file to include anything that's part of your host build-and-test environment, but should not by default be included in an image, possibly because the Dockerfile is going to rebuild it. In a Node context, for example, you almost always want to exclude the host's node_modules directory, or in a Java context often the Gradle build or Maven target directories. Anything you do want to include in the image needs to not be listed in .dockerignore.
Related
I'd like to instruct Docker to COPY my certificates from the local /etc/ folder on my Ubuntu machine.
I get the error:
COPY failed: file not found in build context or excluded by
.dockerignore: stat etc/.auth_keys/fullchain.pem: file does not exist
I have not excluded in .dockerignore
How can I do it?
Dockerfile:
FROM nginx:1.21.3-alpine
RUN rm /etc/nginx/conf.d/default.conf
RUN mkdir /etc/nginx/ssl
COPY nginx.conf /etc/nginx/conf.d
COPY ./etc/.auth_keys/fullchain.pem /etc/nginx/ssl/
COPY ./etc/.auth_keys/privkey.pem /etc/nginx/ssl/
WORKDIR /usr/src/app
I have also tried without the dot --> same error
COPY /etc/.auth_keys/fullchain.pem /etc/nginx/ssl/
COPY /etc/.auth_keys/privkey.pem /etc/nginx/ssl/
By placing the folder .auth_keys next to the Dockerfile --> works, but not desireable
COPY /.auth_keys/fullchain.pem /etc/nginx/ssl/
COPY /.auth_keys/privkey.pem /etc/nginx/ssl/
The docker context is the directory the Dockerfile is located in. If you want to build an image that is one of the restrictions you have to face.
In this documentation you can see how contexts can be switched, but to keep it simple just consider the same directory to be the context. Note; this also doesn't work with symbolic links.
So your observation was correct and you need to place the files you need to copy in the same directory.
Alternatively, if you don't need to copy them but still have them available at runtime you could opt for a mount. I can imagine this not working in your case because you likely need the files at startup of the container.
#JustLudo's answer is correct, in this case. However, for those who have the correct files in the build directory and still seeing this issue; remove any trailing comments.
Coming from a C and javascript background, one may be forgiven for assuming that trailing comments are ignored (e.g. COPY my_file /etc/important/ # very important!), but they are not! The error message won't point this out, as of my version of docker (20.10.11).
For example, the above erroneous line will give an error:
COPY failed: file not found in build context or excluded by .dockerignore: stat etc/important/: file does not exist
... i.e. no mention that it is the trailing # important! that is tripping things up.
It's also important to note that, as mentioned into the docs:
If you use STDIN or specify a URL pointing to a plain text file, the system places the contents into a file called Dockerfile, and any -f, --file option is ignored. In this scenario, there is no context.
That is, if you're running build like this:
docker build -t dh/myimage - < Dockerfile_test
Any COPY or ADD, having no context, will throw the error mentioned or another similar:
failed to compute cache key: "xyz" not found: not found
If you face this error and you're piping your Dockerfile, then I advise to use -f to target a custom Dockerfile.
docker build -t dh/myimage -f Dockerfile_test .
(. set the context to the current directory)
Here is a test you can do yourself :
In an empty directory, create a Dockerfile_test file, with this content
FROM nginx:1.21.3-alpine
COPY test_file /my_test_file
Then create a dummy file:
touch test_file
Run build piping the test Dockerfile, see how it fails because it has no context:
docker build -t dh/myimage - < Dockerfile_test
[..]
failed to compute cache key: "/test_file" not found: not found
[..]
Now run build with -f, see how the same Dockerfile works because it has context:
docker build -t dh/myimage -f Dockerfile_test .
[..]
=> [2/2] COPY test_file /my_test_file
=> exporting to image
[..]
Check your docker-compos.yml, it might be changing the context directory.
I had a similar problem, with the only clarification: I was running Dockerfile with docker-compos.yml
This is what my Dockerfile looked like when I got the error:
FROM alpine:3.17.0
ARG DB_NAME \
DB_USER \
DB_PASS
RUN apk update && apk upgrade && apk add --no-cache \
php \
...
EXPOSE 9000
COPY ./conf/www.conf /etc/php/7.3/fpm/pool.d #<--- an error was here
COPY ./tools /var/www/ #<--- and here
ENTRYPOINT ["sh", "/var/www/start.sh"]
This is part of my docker-compose.yml where I described my service.
wordpress:
container_name: wordpress
build:
context: . #<--- the problem was here
dockerfile: requirements/wordpress/Dockerfile
args:
DB_NAME: ${DB_NAME}
DB_USER: ${DB_USER}
DB_PASS: ${DB_PASS}
ports:
- "9000:9000"
depends_on:
- mariadb
restart: unless-stopped
networks:
- inception
volumes:
- wp:/var/www/
My docker-compos.yml was changing the context directory. Then I wrote a new path in the Dockerfile and it all worked.
COPY ./requirements/wordpress/conf/www.conf /etc/php/7.3/fpm/pool.d
COPY ./requirements/wordpress/tools /var/www/
project structure
FWIW this same error shows up when running gcloud builds submit if the files are included in .gitignore
Have you tried doing a simlink with ln -s to the /etc/certs/ folder in the docker build directory?
Alternatively you could have one image that has the certificates and in your image you just COPY FROM the docker image having the certs.
I had the same error. I resolved it by adding this to my Docker build command:
docker build --no-cache -f ./example-folder/example-folder/Dockerfile
This repoints Docker to the home directory. Even if your Dockerfile seems to run (i.e. the system seems to locate it and starts running it), I found I needed to have the home directory pre-defined above, for any copying to happen.
Inside my Dockerfile, I had the file copying like this:
COPY ./example-folder/example-folder /home/example-folder/example-folder
I merely had quoted the source file while building a windows container, e.g.,
COPY "file with space.txt" c:/some_dir/new_name.txt
Docker doesn't like the quotes.
I'm trying to up a docker from an existing project to run our integration test.
The solution file has directory navigation to a folder outside my context configured in docker-compose (build:context). This is my docker-compose.yml file
services:
integration:
container_name: backend_integration
build:
context: .
dockerfile: buildscripts/backend-integration.Dockerfile
ports:
- 8080:80
I'm running docker-compose up integration
My dockerfile :
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
COPY ./Directory.Build.targets .
COPY ./src/services/{projectName}/project.sln .
COPY ./src/services/{projectName}/{projectName}/projectName.csproj ./{projectName}/
COPY ./src/services/{projectName}/{projectName}Test/projectNameTest.csproj ./{projectName}Test/
COPY ./src/services/{projectName}/{projectName}/nuget.config .
RUN dotnet restore --configfile nuget.config // This is where DBCommom and DataEntities were called
My solution file (sln) has these references
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "projectNameTest", "projectNameTest\projectNameTest.csproj", "{3038F569-2095-4B0D-9531-EF28424E47FB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBCommon", "..\..\..\..\..\pkg\Common\DBCommon\DBCommon.csproj", "{4B4D0CB1-D023-4985-A871-204C43FB2F0A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataEntities", "..\..\..\..\..\pkg\Common\DataEntities\DataEntities.csproj", "{6BAF95C4-667F-4AC4-99EC-EB99DC1DF3B7}"
EndProject
PS: the follow error
/usr/share/dotnet/sdk/2.2.207/NuGet.targets(246,5): error MSB3202: The project file "/pkg/Common/DBCommon/DBCommon.csproj" was not found. [/app/projectName.sln]
/usr/share/dotnet/sdk/2.2.207/NuGet.targets(246,5): error MSB3202: The project file "/pkg/Common/DataEntities/DataEntities.csproj" was not found. [/app/projectName.sln]
ERROR: Service 'integration' failed to build: The command '/bin/sh -c dotnet restore --configfile nuget.config' returned a non-zero code: 1
Quick answer. You cannot.
A little longer answer:
For Docker, it is always Dockerfile and below that Docker can access.
The COPY commands in the Dockerfile are relative to the current folder.
You might have to re-arrange your project structure. Move your Dockerfile at the root, probably next to the solution file, and adjust all the project references relative to that folder.
You should also focus on .dockerignore file and mention all the files/folders that you don't want copied. This'll help you reduce the context that gets copied inside the docker image. (generally the first line of docker build command).
I'm trying to put a dockerfile in a subdirectory of my main startup project AppMain. AppMain has a dependency project called AppDependency. When the dockerfile is in the root directory of AppMain, all works correctly, but when it's nested in a subdirectory of the AppMain, it fails with
4>Step 5/20 : COPY ["AppMain/AppMain.csproj", "AppMain/"]
4>COPY failed: stat /var/lib/docker/tmp/docker-builder453314675/AppMain/AppMain.csproj: no such file or directory
So that makes sense to me since it's nested in a subdirectory, so I simply added a "../" in front of the COPY commands but that results in the following context exception:
4>COPY failed: Forbidden path outside the build context: ../AppMain/AppMain.csproj ()
Alright so that makes sense as well because the docker documentation states that the context starts where the dockerfile is located, but this leads me to ask the real question.
If it cannot copy outside of the where the dockerfile is located and it works when its located in the root of AppMain, then why doesn't it fail when copying the AppReference project which obviously is above where the docker file is located and outside the context since it would have to go up one directory and then down to the AppReference project?
Is there a way to achieve having a dockerfile nested in a subdirectory?
The entire docker file is listed below.
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["AppMain/AppMain.csproj", "AppMain/"]
COPY ["AppReference/AppReference.csproj", "AppReference/"]
RUN dotnet restore "AppMain/AppMain.csproj"
COPY . .
WORKDIR "/src/AppMain"
RUN dotnet build "AppMain.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "AppMain.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "AppMain.dll"]
When use docker build -t xxx . to build in the subdirectory, only the contents in . will submit to docker daemon for docker build. The things in parent folder will not be able to submit to docker engine to build.
So, you need to move the execution directory back to the parent folder with cd .., then use next to build, it will then send the . which now is the contents of parent folder to engine(But you need now specify dockerfile, like this)
docker build -t xxx -f ./YOUR_Subdirectory/Dockerfile .
It is really working.
What it is ?
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
I have a folder : /docker on my project root and it contains certain files like Apache configs which I need to copy to the correct folders during teh build.
Project-root
------docker folder
-----------apache.conf
Now
DOCKERFILE
In my dockerfile i have a copy command which copies the whole of root to the container.
`Issue`
I don't want the docker folder to be copied to the built image.
I added docker to the .dockerignore. But this leads to the problem that these files are not sent to the build context and then the build command fails.
WORKAROUND
Use the RUN command in the dockerfile to remove the docker folder instead of using .dockerignore
QUESTION
Is there any better solution?
DOCKER FILe:
FROM <baseimage>
WORKDIR /var/www/html/
COPY . /var/www/html/MYFOLDER
COPY ./docker/apache.conf /etc/httpd/conf/apache.conf
COPY ./docker/sites-enabled /etc/httpd/sites-enabled
I have a simple Dockerfile:
FROM php:7.1-apache
LABEL maintainer="rburton#agsource.com"
COPY C:/Users/rburton/code/MyAgsourceAPI /var/www
It is the last line that is giving me problems. I am copying from a Windows structure to a docker container (Linux I assume). When I build this image I get:
...
Step 3/3 : COPY C:/Users/rburton/code/MyAgsourceAPI /var/www
COPY failed: stat /var/lib/docker/tmp/dockerbuilder720374851/C:/Users/rburton/code/MyAgsourceAPI: no such file or directory
First, something is preventing the recognition that this is an absolute path and naturally if the path is pre-pended with /var/lib/docker/tmp/dockerbuilder720374851 then the file will not be found. Second, I have tried / and \ but all with the same result. Also the drive letter I suspect is confusing to docker. So the question is how do I copy files and folders (along with the contents) from a Windows folder to a docker container?
First, change your Dockerfile to:
FROM php:7.1-apache
LABEL maintainer="rburton#agsource.com"
COPY MyAgsourceAPI /var/www
Then, to go your code directory: cd Users/rburton/code.
Within that directory, run:
docker build -t <image_name> .
Another tip that might be helpful, I've seen same issue while running build from correct context, and issue remained until I've used all small caps on src folder that I wanted to copy from. eg:
COPY MyAgsourceAPI /var/www -> COPY myagsourceapi /var/www
The root of the path is relative to the Dockerfile and not your Windows filesystem.
If for example your filesystem is layed out like this:
+-+-MyProject
|
+---Dockerfile
|
+-+-build
|
+---MyAgsourceAPI
You can use:
COPY /build/MyAgsourceAPI /var/www
Note that "MyProject" (or anything above it) is excluded from the path.