How to copy files from server to docker container - docker

I am trying to create a Windows Nano Server container and copy some files from server to the new container. Below is my dockerfile code:
FROM microsoft/nanoserver
MAINTAINER test#gmail.com
COPY C:/files/ C:/files/
When I run it, I am getting below error:
Copy failed:createFile
\?\c:\programdata\Docker\tmp\docker-builder78565487\files: The system
canot find the path specified.
How do I declare source location in COPY command, so it uses absolute path and copy files to c:\files location on my container.

You can not copy files outside of context of the build. Move files which needs to be copied in the same folder where dockerfile is.
https://docs.docker.com/engine/reference/builder/#copy

Related

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:
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.
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.

where is the tmp/setup folder created when doing ADD cmd in docker?

My dockerfile has this line, it sends the txt file to /tmp/setup folder, but i dont see it anywhere in the current docker folder where Dockerfile sits.
ADD requirements.txt /tmp/setup/requirements.txt
how do i look inside this folder? where is "tmp/setup" located?
This line(ADD requirements.txt /tmp/setup/requirements.txt) will copy the file requirements.txt from the current folder to the folder /tmp/setup in the filesystem of Docker image when you building your image.
You can find this file a folder in the container that run from the built image, not in the current Dockerfile folder.

Troubleshoot directory path error in COPY command in docker file

I am using COPY command in my docker file on top of ubuntu 16.04. I am getting error as no such file or directory eventhough the directory is present. In the below docker file I want to copy the directory "auth" present inside workspace directory to the docker image (at path /home/ubuntu) and then build the image.
FROM ubuntu:16.04
RUN apt-get update
COPY /home/ubuntu/authentication/workspace /home/ubuntu
WORKDIR /home/ubuntu/auth
a Dockerfile COPY command can only refer to files under the context - the current location of the Dockerfile, aka .
so you have a few options now:
if it is possible to copy the /home/ubuntu/authentication/workspace/ directory content to somewhere inside your project before the build (so now it will be included in your Dockerfile context and you can access it via COPY ./path/to/content /home/ubuntu) it can be great. but sometimes you dont want it.
instead of copying the directory, bind it to your container via a volume:
when you run the container, add a -v option:
docker run [....] -v /home/ubuntu/authentication/workspace:/home/ubuntu [...]
mind that a volume is designed so any change you made inside the container dir(/home/ubuntu) will affect the bound directory on your host side (/home/ubuntu/authentication/workspace) and vice versa.
i found a something over here: this guy is forcing the Dockerfile to accept his context- he is sitting inside the /home/ubuntu/authentication/workspace/ directory, and running there
docker build . -f /path/to/Dockerfile
so now inside his Dockerfile he can refer to /home/ubuntu/authentication/workspace as his context (.)

How to create docker image using already configured application folder

My basic requirement is to create docker image and deploy it to docker registry.
I have a pre-configured application folder(/home/myfolder) in my jenkins server(to do this configuration I have used ansible script). Then I need to create docker image from that folder and deploy it to docker registry.
What's the best way to do this? Please help me with this as I'm new to docker.
please find my Dockerfile below
#Download base image ubuntu 16.04
FROM ubuntu
WORKDIR /dockerprojects
#copy the zip file to docker folder
COPY /wso2telcohub-4.0.0-SNAPSHOT /home/myfolder/dockerprojects/IGW/dockerCI
COPY cp /wso2is-km-5.6.0 /home/myfolder/dockerprojects/IGW/dockerCI
CMD [“bash”]
There are a bunch of things in that Dockerfile that potentially can go sideways. I will comment on them one by one here:
#Download base image ubuntu 16.04
FROM ubuntu
If you intend to use the ubuntu:16.04 image, you need to specify it. Without a specific tag, the FROM instruction will look for the latest tag, in this case the find the image ubuntu:latest for you.
WORKDIR /dockerprojects
This command sets the workdir inside the docker image, so that when the container starts, the sessions PWD will be set to /dockerprojects. This is important because all other commands durring the build and when the container is started will be relative to this location in the file structure.
#copy the zip file to docker folder
COPY /wso2telcohub-4.0.0-SNAPSHOT /home/myfolder/dockerprojects/IGW/dockerCI
This command will copy the file /wso2telcohub-4.0.0-SNAPSHOT from the "host machine", the machine where the docker image is being built, into the image at the location /home/myfolder/dockerprojects/IGW/dockerCI. If the location does not already exist in the image, then it will create a file named dockerCI at the location /home/myfolder/dockerprojects/IGW/. I don't think that this is what you want.
Also, your comment states that this is a zip file, but it seems to be missing an extension like .zip or .gz - I believe that you are not referencing the file correctly.
COPY cp /wso2is-km-5.6.0 /home/myfolder/dockerprojects/IGW/dockerCI
This instruction will not execute. For the COPY instruction you don't need to use a "cp" command. If you removed "cp" from the line however, it would try to copy the file or directory /wso2is-km-5.6.0 from the host machine (that's a file in the root of the filesystem) to the location /home/myfolder/dockerprojects/IGW/dockerCI inside the resulting image.
CMD [“bash”]
The CMD instruction simply sets the image to start a new bash shell when started - which will make the container exit immediatly when the bash command completes.
I have a feeling that the source location of the files that you want to put in the image is not in the root of the host machine. But probably at /home/myfolder/dockerprojects/ on the host that you mention. I have asked you to clearify the location of the files that you want in the image in a comment on your question.
Update
The error that you are getting 'no such file or directory' means that the source file that you are referencing in the COPY instruction, does not exist.
The COPY instruction works like this:
COPY <sourcepath> <targetpath>
Where the <sourcepath> is the path of the file on the machine where the image is being built. This path is relative to the Dockerfile (or the build context if specified), unless it starts with a /, then it is relative to the root of the filesystem on the host machine. And the targetpath is the desired path inside the resulting image.
Let's say that I have the following folder structure:
/home/myfolder/dockerprojects/
├── Dockerfile
├── wso2telcohub-4.0.0-SNAPSHOT.zip
├── wso2is-km-5.6.0/
│ ├── anotherfile.txt
And I wanted all the files in the path /home/myfolder/dockerprojects/ to be put inside the docker image, below the path /app. I would do this with a Dockerfile like:
FROM ubuntu:16.04
WORKDIR /app
COPY . /app/
Or each file individually like this:
FROM ubuntu:16.04
WORKDIR /app
COPY ./wso2telcohub-4.0.0-SNAPSHOT.zip /app/wso2telcohub-4.0.0-SNAPSHOT.zip
COPY ./wso2is-km-5.6.0 /app/wso2is-km-5.6.0
That would leave me with the following in the docker image:
/app/
├── Dockerfile
├── wso2telcohub-4.0.0-SNAPSHOT.zip
├── wso2is-km-5.6.0/
│ ├── anotherfile.txt

Docker: Ignore some files which are needed in build context

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

Resources