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.
I had to add a command like
command: "/bin/bash -c 'cd /project && npm install'"
to get it working
There are several possible workflow with Docker Compose and addressing them all would make our README huge!
@LaurentGoderre just add a working example please. One u got not working
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"
}
}
Created #534 to update the README
Awesome, thx !
This saved me !
It was lacking:
working_dir: /home/node/app
Seeing as that was the fix, and it is included in the updated example, I'm closing this
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
packag.json
If this is really how you named the file, then fix the typo (package.json is the correct) and try again.
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.
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.
Try copying your current project in the C:/Users and it might work well on windows.
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:
I was able to resolve this by giving Docker permissions to the drive and restarting the Docker service.
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 . .
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 :)
Most helpful comment
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.