Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile copy keep subdirectory structure

I'm trying to copy a number of files and folders to a docker image build from my localhost.

The files are like this:

folder1     file1     file2 folder2     file1     file2 

I'm trying to make the copy like this:

COPY files/* /files/ 

However, all files are placed in /files/ is there a way in Docker to keep the subdirectory structure as well as copying the files into their directories?

like image 854
user1220022 Avatar asked May 13 '15 13:05

user1220022


People also ask

Does copy in Dockerfile create directory?

Docker Dockerfiles COPY InstructionThe COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest> . Multiple <src> resource may be specified but they must be relative to the source directory that is being built (the context of the build).

How do I copy in Dockerfile?

Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.


2 Answers

Remove star from COPY, with this Dockerfile:

FROM ubuntu COPY files/ /files/ RUN ls -la /files/* 

Structure is there:

$ docker build . Sending build context to Docker daemon 5.632 kB Sending build context to Docker daemon  Step 0 : FROM ubuntu  ---> d0955f21bf24 Step 1 : COPY files/ /files/  ---> 5cc4ae8708a6 Removing intermediate container c6f7f7ec8ccf Step 2 : RUN ls -la /files/*  ---> Running in 08ab9a1e042f /files/folder1: total 8 drwxr-xr-x 2 root root 4096 May 13 16:04 . drwxr-xr-x 4 root root 4096 May 13 16:05 .. -rw-r--r-- 1 root root    0 May 13 16:04 file1 -rw-r--r-- 1 root root    0 May 13 16:04 file2  /files/folder2: total 8 drwxr-xr-x 2 root root 4096 May 13 16:04 . drwxr-xr-x 4 root root 4096 May 13 16:05 .. -rw-r--r-- 1 root root    0 May 13 16:04 file1 -rw-r--r-- 1 root root    0 May 13 16:04 file2  ---> 03ff0a5d0e4b Removing intermediate container 08ab9a1e042f Successfully built 03ff0a5d0e4b 
like image 76
ISanych Avatar answered Sep 23 '22 03:09

ISanych


Alternatively you can use a "." instead of *, as this will take all the files in the working directory, include the folders and subfolders:

FROM ubuntu COPY . / RUN ls -la / 
like image 26
Sparkz0629 Avatar answered Sep 19 '22 03:09

Sparkz0629