docker-node: docker-compose example does not work

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.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 9
  • Comments: 16 (4 by maintainers)

Most upvoted comments

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

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.

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"
  }
}

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.

I had to add a command like command: “/bin/bash -c ‘cd /project && npm install’” to get it working

This saved me ! It was lacking:

    working_dir: /home/node/app