# Making volumes with Docker-Compose

**URL:** <https://forums.docker.com/t/making-volumes-with-docker-compose/45657>\
**Category:** Compose\
**Created:** [February 2, 2018, 2:03pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657 "2018-02-02T14:03:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xoancosmed](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/xoancosmed/32/18745_2.png) [@xoancosmed](https://forums.docker.com/u/xoancosmed)\
**Post date:** [February 2, 2018, 2:03pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/1 "2018-02-02T14:03:30Z")

</div>

I am trying to use Docker to make an API with NodeJS. This is how the directory of my project looks like:

- Dockerfile
- docker-compose.yml
- package.json
- server.js
- online\_app/ (empty folder)

What I want to do is to have an “online\_app” folder which will contain the files of my projects. The problem is that when I try to map this folder as a volume, NodeJS fails because it don’t find the “server.js” and “package.json” file. This is the content of the files (I think that the mistake should be in the “docker-compose.yml” or in the “Dockerfile”:

_Dockerfile_

```
FROM node

RUN mkdir /app
WORKDIR /app

COPY . /app
RUN npm install

EXPOSE 8080

CMD ["npm", "start"]

```

_docker-compose.yml_

```
version: "2"

services:
  web:
    build: .
    volumes:
      - ./online_app:/app
    expose:
      - "8080"
    environment:
      - VIRTUAL_HOST=api.quick-cashier.ga,api.quickcashier.eu
      - VIRTUAL_PORT=8080
  mongo:
    image: mongo
    ports:
      - "27017:27017"
networks:
  default:
    external:
      name: proxy_net

```

_package.json_

```
{
  "name": "node_server",
  "version": "0.0.1",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2",
    "mongoose": "^5.0.3",
    "nodemon": "^1.14.12"
  }
}

```

_server.js_

```
var express = require('express');
var app = express();

var db = require('mongoose');
db.connect("mongodb://mongo:27017");

app.get('/', function(req, res){
  res.send("Hello World!<br>\nHola Mundo!<br>\n");
});

app.listen(8080, function(){
  console.log('Node app listening on port 8080!');
});
```

---

<div class="post-metadata">

**Author:** ![sdetweil](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@sdetweil](https://forums.docker.com/u/sdetweil)\
**Post date:** [February 2, 2018, 2:12pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/2 "2018-02-02T14:12:34Z")

</div>

i don’t quite understand…

the volumes

```auto
volumes:
      - ./:/app 

```

means to map the current directory (./) to the containers /app folder…  
this will overlay the /app folder in the container (the good stuff) with whatever  
is in the current directory at the time of docker-compose up

but… you have already copied . into /app during build

also, why copy the package file only then copy everything later

```auto
COPY package.json /app
RUN npm install

COPY . /app

```

just copy everything and THEN do the npm install.

---

<div class="post-metadata">

**Author:** ![xoancosmed](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/xoancosmed/32/18745_2.png) [@xoancosmed](https://forums.docker.com/u/xoancosmed)\
**Post date:** [February 2, 2018, 2:18pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/3 "2018-02-02T14:18:27Z")

</div>

Sorry, I copied the wrong “docker-compose.yml” file. I corrected it in my first post:

```
volumes:
       - ./online_app:/app

```

You are right, I should only copy the package.json ones. I have corrected it in the first post, but I still have problems with the mapping of the folder …

The thing is that I copy all these files in the Dockerfile (when I do “COPY . /app”), but when the volume is mounted, all that files are missing …

---

<div class="post-metadata">

**Author:** ![sdetweil](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@sdetweil](https://forums.docker.com/u/sdetweil)\
**Post date:** [February 2, 2018, 2:20pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/4 "2018-02-02T14:20:07Z")

</div>

right,… you overlayed the internal /app (built at image build time) files with whatever is in ./online\_app at run time…

---

<div class="post-metadata">

**Author:** ![xoancosmed](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/xoancosmed/32/18745_2.png) [@xoancosmed](https://forums.docker.com/u/xoancosmed)\
**Post date:** [February 2, 2018, 2:22pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/5 "2018-02-02T14:22:43Z")

</div>

Ahhh … now I understand. Sorry for my ignorance, but there is a way to do the reverse thing? I mean, it is possible that the content of the “/app” directory of the container is preserved instead of the content in the “online\_app” (in the host)?

Thanks for your reply

---

<div class="post-metadata">

**Author:** ![sdetweil](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@sdetweil](https://forums.docker.com/u/sdetweil)\
**Post date:** [February 2, 2018, 2:23pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/6 "2018-02-02T14:23:51Z")

</div>

don’t mount the volume, or mount it someplace else…

what does online\_app add to the node application?

---

<div class="post-metadata">

**Author:** ![xoancosmed](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/xoancosmed/32/18745_2.png) [@xoancosmed](https://forums.docker.com/u/xoancosmed)\
**Post date:** [February 2, 2018, 2:29pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/7 "2018-02-02T14:29:15Z")

</div>

I was planning to use the “online\_app” folder to keep always a copy of the original code. But as this does not work as I supposed, I think that I am going to pass …

Thanks for your help 😉

---

<div class="post-metadata">

**Author:** ![esquiregh](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/esquiregh/32/23320_2.png) [@esquiregh](https://forums.docker.com/u/esquiregh)\
**Post date:** [April 3, 2019, 2:04pm UTC](https://forums.docker.com/t/making-volumes-with-docker-compose/45657/8 "2019-04-03T14:04:03Z")

</div>

It’s good practice to copy just the package.json file and perform installation before copying the rest of the files. The reason is; if you do so, a layer will be created for your installation process and cached. So every time you build your image, it will skip the installation process unless you made some changes in the package.json file.
