docker-node icon indicating copy to clipboard operation
docker-node copied to clipboard

docker-compose example does not work

Open Crazy-Duck opened this issue 9 years ago • 16 comments

According to the README, it looks like I should be able to just create a docker-compose file with the following contents to get an application running:

version: "2"
services:
  node:
    image: "node:8"
    environment:
      - NODE_ENV=production
    volumes:
      - ./:/usr/src/app
    expose:
      - "8080"

The note underneath implies that simply performing a docker-compose up will start a container in which the dependencies will be installed via an npm install and that the application will be started by a npm start command. However, docker-compose up just starts a container, executes the node command in the root directory and instantly stops the container since nothing is being executed. This is normal, since the Dockerfile for the node:8 image only contains CMD[ "node" ] and no working directory.

If you want to run straight from the image you need to at least add a working_dir: /usr/src/app and an entrypoint to the docker-compose.yml. In the entrypoint you'll need to execute a bash script in which you first perform an npm install followed by an npm start.

Crazy-Duck avatar Oct 01 '17 14:10 Crazy-Duck

I had to add a command like command: "/bin/bash -c 'cd /project && npm install'" to get it working

jg-development avatar Oct 03 '17 11:10 jg-development

There are several possible workflow with Docker Compose and addressing them all would make our README huge!

LaurentGoderre avatar Oct 03 '17 12:10 LaurentGoderre

@LaurentGoderre just add a working example please. One u got not working

mossad-zika avatar Oct 03 '17 12:10 mossad-zika

The following works:

docker-compose.yml

version: "2"
services:
  node:
    image: "node:8"
    user: "node"
    working_dir: /home/node/app
    environment:
      - NODE_ENV=production
    volumes:
      - ./:/home/node/app
    expose:
      - "8081"
    command: "npm start"

package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "npm install && node node_modules/.bin/http-server -p 8081"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "angular": "^1.6.6",
    "http-server": "^0.10.0"
  }
}

LaurentGoderre avatar Oct 03 '17 14:10 LaurentGoderre

Created #534 to update the README

LaurentGoderre avatar Oct 03 '17 14:10 LaurentGoderre

Awesome, thx !

mossad-zika avatar Oct 03 '17 17:10 mossad-zika

This saved me ! It was lacking:

    working_dir: /home/node/app

msegoviadev avatar Oct 21 '17 19:10 msegoviadev

Seeing as that was the fix, and it is included in the updated example, I'm closing this

SimenB avatar Oct 22 '17 10:10 SimenB

Unfortunately I have to make it working on Windows 7 but I keep getting this error. Anyone know how to make the volumes work properly there?

In the same folder I put the docker-compose.yml and packag.json suggested by @LaurentGoderre

PS A:\docker\test> docker-compose up Creating network "test_default" with the default driver Creating test_node_1 ... Creating test_node_1 ... done Attaching to test_node_1 node_1 | npm ERR! path /home/node/app/package.json node_1 | npm ERR! code ENOENT node_1 | npm ERR! errno -2 node_1 | npm ERR! syscall open node_1 | npm ERR! enoent ENOENT: no such file or directory, open '/home/node/app/package.json' node_1 | npm ERR! enoent This is related to npm not being able to find a file. node_1 | npm ERR! enoent node_1 | node_1 | npm ERR! A complete log of this run can be found in: node_1 | npm ERR! /home/node/.npm/_logs/2018-03-05T10_57_04_874Z-debug.log test_node_1 exited with code 254

I added .env with COMPOSE_FORCE_WINDOWS_HOST=1 but nothing changed apparently

Thanks, Karoly

sabau avatar Mar 05 '18 11:03 sabau

packag.json

If this is really how you named the file, then fix the typo (package.json is the correct) and try again.

gergoerdosi avatar Mar 05 '18 11:03 gergoerdosi

sorry, the typo was in my message :)

The problem was caused by the shared folders of my docker-machine driver (vmwareworkstation), so is not really inherent to this project.

I've put a patch in my .vmx definition and now your demo projects works with little modifications. Basically I had my projects under another drive, where by default only the Users folder is available, with command line I was able to add a single other shared folder, so the (temporary) solution I've found was to edit the vmx definition to add more shares.

sabau avatar Mar 05 '18 13:03 sabau

I had the same issue.The problem in my case was windows account.Recently, I changed my windows password and docker was blocked access to the file.Reapplying shared drive resolve the issue.

biruks avatar Apr 20 '18 05:04 biruks

Try copying your current project in the C:/Users and it might work well on windows.

agharshit08 avatar Aug 02 '19 16:08 agharshit08

The problem was caused by the shared folders of my docker-machine driver (vmwareworkstation), so is not really inherent to this project.

I've put a patch in my .vmx definition and now your demo projects works with little modifications. Basically I had my projects under another drive, where by default only the Users folder is available, with command line I was able to add a single other shared folder, so the (temporary) solution I've found was to edit the vmx definition to add more shares.

This is exactly right. I ran into the same issue but for Docker Desktop for Windows.

This is an issue with Docker/Docker Compose mounting your code directory to the container's directory. The issue is in

volumes:
      - ./:/home/node/app

Although the Dockerfile has access to copy your code and build the container; Docker is failing during the mounting process. Some steps to resolve the issue:

  1. I was able to resolve this by giving Docker permissions to the drive and restarting the Docker service.

  2. Commenting out the volumes section. This will prevent the error from occurring but does not allow for synchronizing of the code to your app's directory. This isn't ideal but rebuilding with docker-compose up --build should copy your code to the container and run assuming your Dockerfile has a

WORKDIR /home/node/app
COPY . .
  1. If you are using Docker Toolbox as I believe @sabau is for Windows 7 then you can append the .vmx if the GUI doesn't support setting permissions for the drive your codebase is in. Appending the .vmx is usually a last resort in many cases but can resolve the issue.

Kenttleton avatar Dec 14 '19 18:12 Kenttleton

Commenting out the volumes section. This will prevent the error from occurring but does not allow for synchronizing of the code to your app's directory. This isn't ideal but rebuilding with docker-compose up --build should copy your code to the container

The --build solved my problem finally after 2 hours of searching for solution and triple checking everyting.

The single build (docker build -t backend .) worked fine, but the docker-compose kept failing (even if i had the WORKDIR in Dockerfile and the working_dir in the the docker-compose.yml). Now with the --build flag its working :)

samzlab avatar Jul 24 '20 13:07 samzlab

My problem was solved after correcting the volumes for the service in docker-compose.yml . In my case the current folder which was being mapped into /usr/src/app did'nt contain package.json file

smriti111 avatar Mar 26 '21 14:03 smriti111