Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy files and folders to a working directory in Dockerfile?

Problem

I have a simple node application with the following structure:

├── Dockerfile
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   └── stylesheets
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.hbs
    ├── index.hbs
    └── layout.hbs

In my Dockerfile, how do I copy both files and folders to a working directory that follows the same file structure as my host?

That is, if I were to list the content of my working folder as a run command, it would match:

Step 9/11 : RUN ls -la ./
 ---> Running in 15dd5125da65
total 40
drwxr-xr-x    1 root     root          4096 Aug 25 02:58 .
drwxr-xr-x    1 root     root          4096 Aug 25 00:30 ..
-rw-r--r--    1 root     root          2685 Aug 25 02:58 Dockerfile
-rw-r--r--    1 root     root          1256 Aug 25 00:17 app.js
drwxr-xr-x    2 root     root          4096 Aug 25 00:17 bin
drwxr-xr-x   79 root     root          4096 Aug 25 00:30 node_modules
-rw-r--r--    1 root     root           341 Aug 25 00:17 package.json
drwxr-xr-x    4 root     root          4096 Aug 25 00:17 public
drwxr-xr-x    2 root     root          4096 Aug 25 00:17 routes
drwxr-xr-x    2 root     root          4096 Aug 25 00:17 views
like image 279
justinpage Avatar asked Jan 02 '23 23:01

justinpage


1 Answers

Solution

In order to copy files and folders to a working directory, you can use the following in your Dockerfile:

WORKDIR /working/directory/path
COPY . .

This is an easy way to change to a working directory and copy everything from your host source.

Please Note

This will copy everything from the source path --more of a convenience when testing locally.

like image 134
justinpage Avatar answered Jan 05 '23 16:01

justinpage