Dockerfile COPY ".env" directory - bash

How can I use COPY in Dockerfile to copy folder of which name starts with a dot?
FROM confluentinc/cp-kafka-connect:6.0.0
# does not work
COPY ./aws/ /home/appuser/.aws
EXPOSE 8083
Directory structure:
/MyFolder
├── .aws
│ └── credentials
└── Dockerfile

COPY ./aws [DESTINATION] is "COPY, from the current directory './', the directory named 'aws' to the [DESTINATION]".
COPY ./.aws [DESTINATION] will COPY the hidden directory '.aws' from the current directory './' to [DESTINATION].
COPY ./.aws/ /home/appuser/.aws will result in /home/appuser/.aws/credentials existing in the built image.
Tip: [DESTINATION] is created by COPY if it doesn't already exist.
Note: if the directory is ignored in a .dockerignore then the COPY will not work.
Note: should never COPY credentials in an image if you intend on sharing the image rather bind-mount the credentials during the containers runtime i.e. docker run --rm -it -v ./.aws/credentials:/home/appuser/.aws/credentials:ro myimage

Related

Copy folder from Windows host OS into Docker image using COPY in Docker file

I want to copy folder D:\test from my host OS (Windows) to my docker image.
My Docker file is D:\Programs>
Docker file
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir test
COPY D:/test to /root/test
#USING 'COPY' here instead of 'ADD' because of https://stackoverflow.com/questions/24958140/what-is-the-difference-between-the-copy-and-add-commands-in-a-dockerfile
From folder D:\Programs> I run command docker build . -t test
But I get error:
COPY failed: file not found in build context or excluded by .dockerignore: stat test: file does not exist
I tried commands COPY D:/test to /root/test and COPY ./test to /root/test
I know this error occurs because the path I copy from has to be relative from the build context (the . in docker build .). It cannot be an arbitrary path on the system outside of the build context.
I thought by using . I'd be in context D:\ from my build context D:\Programs>, but I guess not. What should my COPY command look like?
I checked here already:
https://forums.docker.com/t/copy-files-from-windows-host-to-ubuntu-container/28757
Dockerfile COPY from a Windows file system to a docker container
Dockerfile: Copy directory from Windows host to docker container
UPDATE 1
I placed the test folder in D:\Programs where my Dockerfile also resides, so I now have structure:
D:\Programs
\Test
Dockerfile
I then ran the build command again where I tried COPY test to /root/test and COPY ./test to /root/test, but both fail with the same aforementioned error.
I assume you've only added to in COPY D:/test to /root/test for abbreviation but I recommend checking that anyway.
If you really need what's in D:/Programs and can't move it to a folder inside the current build context there are two general options as I see it:
changing the build context - I'd suggest setting the build context as D:/ and adding every folder in the D:/ directory other than Programs to the .dockerignore file, then you can reference the Dockerfile from some subdirectory somewhere in D:/ and copy your files at build time as you wanted
creating a bind mount in runtime - you can map the D:/Programs directory to a folder inside the container (/my-programs) at runtime then copy the files from that folder to /root/test or just use the bind mount folder (/my-programs)
If in the future it will be available to use bind mounts in build time you'd be able to use the second option while creating the docker image
The only way I know how to do this is to change the dockerfile location during build, keeping the copied folder in context.
As below:
'test' folder located at D:/test
Set dockerfile COPY command as such:
COPY test /root/test
Say dockerfile is at D:/Programs/dockerfile
Navigate to D:/ in CLI
docker build -f ./Programs/dockerfile .
The . keeps the "test" folder in the build context. As far as I know, you cannot go to a parent directory etc. with docker build, so instead you want to stay in the parent folder and go down to the dockerfile.
If you run the docker build ... from D:\Programs that folder is the docker build context. All files used in the Dockerfile must be there.
d:\files is outside d:\programs, so it will be never be found.
You need to copy files content to the places where you run the command and you can use this copy line
COPY samplefile1.txt /root/test

An issue with resolving directory names when building a docker container for an asp.net core 3.1 web app

Visual Studio 2019 generates a docker file whose COPY statement looks like the following due to the way that the application's folders are structured:
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["MyWebApp/MyWebApp.csproj", "MyWebApp/"]
COPY ["../../core/Logic/Logic.csproj", "../../core/Logic/"]
COPY ["../../core/Models/Models.csproj", "../../core/Models/"]
RUN dotnet restore "MyWebApp/MyWebApp.csproj"
COPY . .
WORKDIR "/src/MyWebApp"
RUN dotnet build "MyWebApp.csproj" -c Release -o /app/build
I keep getting the following error message when running docker compose command:
=> ERROR [build 4/9] COPY [../../core/Logic/Logic.csproj,
../../core/Logic/] 0.0s
=> ERROR [build 5/9] COPY [../../core/Models/Models.csproj, ../../core/Models/]
We cannot restructure the folders in VS 2019 as it has been set up in that way due to some reasons. The docker file resides in MyWebApp project's folder and the docker compose file is in the parent folder of MyWebApp folder where the solution resides. Even I moved the docker file adjacent to the docker compose file but it was not conclusive.
The following code snippet is the docker-compose file:
version: '3.4'
services:
mywebapp:
image: ${DOCKER_REGISTRY-}mywebapp
build:
context: .
dockerfile: MyWebApp/Dockerfile
ports:
- 8080:80
- 8443:443
What is the workaround or solution to address this problem?
If your file structure is:
docker-compose.yml
MyWebApp/MyWebApp.csproj
core/Logic/Logic.csproj
core/Models/Models.csproj
Your Dockerfile should be:
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["MyWebApp/MyWebApp.csproj", "MyWebApp/"]
COPY ["core/Logic/Logic.csproj", "../../core/Logic/"]
COPY ["core/Models/Models.csproj", "../../core/Models/"]
RUN dotnet restore "MyWebApp/MyWebApp.csproj"
COPY . .
WORKDIR "/src/MyWebApp"
RUN dotnet build "MyWebApp.csproj" -c Release -o /app/build
Why?
Because the current working directory is always the context directory. Context can be set using docker-compose's build.context field. In your example, the context is the solution root. So the Dockerfile is executed from that directory, irrespective or where it's placed.
You cannot access files outside the context directory.
Eg. if context resolves to /a/b/c, Dockerfile can't COPY or use files in /a/b/d

cannot create custom folders in docker logstash image based container

i'm using logstash image and i have some ruby scripts that are located in the same directory as my dockerfile.
my goal is to create a script folder, and copy my scripts to it.
my problem is that when i access to the container instance, the folder is not created and no ruby file exist.
this is my dockerfile
FROM docker.elastic.co/logstash/logstash:${ELK_VERSION}
USER root
WORKDIR /usr/share/logstash
RUN mkdir scripts
COPY ./scripts/*.rb scripts
thanks in advance.
EDIT 1:
this is the files structure

Windows Docker Dockerfile COPY file inside folder

I'm trying to build a Dockerfile to copy a file to container, I'm using Windows 10. This is my Dockerfile
FROM openjdk:8
COPY /target/myfile.java /
And I'm getting the error:
failed to solve with frontend dockerfile.v0: failed to build LLB: failed to compute cache key: "/target/myfile.java" not found: not found
I already tried //target//myfile.java, \\target\\myfile.java, \target\myfile.java, target/myfile.java, target\myfile.java but none of them worked.
If I put the myfile.java on the same directory of Dockerfile and use COPY myfile.java / works without problem. So the problem is to copy a file inside a folder. Any suggestion?
I tried your Dockerfile locally and it built fine with the following directory structure:
Project
│ Dockerfile
│
└───target
│ │ myfile.java
I built it from the 'Project' directory with the following command:
docker build . -t java-test
I could only reproduce the error when the Docker server couldn't find the 'myfile.java', i.e. using the following directory structure:
Project
│ Dockerfile
│
└───target
│ └───target
│ └───myfile.java
So your dockerfile looks fine, just make sure you build it from the right directory with the correct build context and the file is stored in the correct place locally

How to create a symlink of a directory inside another symlinked directory?

I succesfully deployed a Laravel website onto a web server by cloning it into a directory at the same level as public_html (called laravel) and creating a symlink to the laravel/public directory into public_html, with this command:
ln -s laravel/public public_html
This works great.
Now I want to have a "test" version of the site for development, and I wanted to do the same.
I cloned my project in a "laravel_dev" directory, same level as public_html and laravel directories.
I created a public_html/dev directory.
Now, I want to create a symlink of 'laravel_dev/public' into 'public_hmtl/dev' and here is where I am having trouble.
If I do
ln -s laravel_dev/public public_html/dev
It creates a file (not a directory) called public inside public_html/dev.
I tried making the target go to laravel/public/dev, with the same result.
I double checked that laravel_dev/public directory exists and it is not empty.
I also tried removing the dev directory inside laravel/public/dev, and the result there is that it creates a file called dev inside laravel/public but it is not a directory.
To clarify, my directory tree is something like this:
www
|___public_html(1)/dev
|___laravel/public(1*)/dev
|___laravel_dev/public
I am positioned in www directory when I am executing the mentioned commands
The (number) indicates symlink and the * indicates the "physical" directory. Using this notation here is what I want to acomplish:
www
|___public_html(1)/dev(2)
|___laravel/public(1*)/dev(2)
|___laravel_dev/public(2*)
You should just make link in the physical directory.
mkdir laravel{,_dev}
ln -s laravel public_html
ln -s ../laravel_dev laravel/dev
It gives me following directory/files tree
[Alex#Normandy so]$ tree
.
├── laravel
│   └── dev -> ../laravel_dev
├── laravel_dev
└── public_html -> laravel
Simple test:
[Alex#Normandy so]$ echo "test dev" > laravel_dev/test
[Alex#Normandy so]$ cat public_html/dev/test
test dev
Note that there might be also problem with your webserver.

Resources