0

I just want to create a docker container that pulls from the official Node.js image using the guidelines found here.

The only change I want to make is I would like to mount my host directory to my container so that I can create new files on the host and have them update in the container.

I have tried every suggestion here: -v flag, --mount flag etc.

But when I use these flags with the run command, no container actually runs.

I run the following:

docker run -p 49160:8080 -d myname/node-web-app --mount source=/Users/myname/desktop/dockyard/enviro

It spits out a container ID:

7302055670c231fb41d04d6475d42405cbee834e37e0827a68d7c396a918d3ec

But when I run docker-ps the container list is empty.

When i check docker-ps -a I can see that it was exited with code 9:

CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS                         PORTS                    NAMES
dbf7973608a0        myname/node-web-app            "docker-entrypoint.s…"   4 seconds ago       Exited (9) 2 seconds ago                                quirky_sammet

I have searched for an explanation of code 9, and I cannot find anything.

Would really appreciate any help that can be provided.

UPDATE

Tried: docker run -p 49160:8080 -d myimage -v /Users/myname/desktop/dockyard/enviro:/usr/src/main

Container exits with code 0. docker logs simply returns v11.15.0

I understand that this means the container is exiting because of no process, BUT if i run docker run -p 49160:8080 -d myimage without the -v flag, container runs perfectly fine.

So not sure why -v flag would cause exit (0).

Dockerfile as per Node.js tutorial:

FROM node:11

# Create app directory
WORKDIR /usr/src/main

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]
Ryan Skene
  • 864
  • 1
  • 12
  • 29

1 Answers1

1

You may find it easier to docker run --interactive --tty than docker run --detach while you're debugging.

I think your mount syntax is borked. I think you need the source and the target otherwise Docker Engine does not know where to map the directory within the container.

I'm less familiar with the --mount syntax so please try the following to map your local directory (/Users/myname/desktop/dockyard/enviro) to the container's directory (/Users/myname)

--volume=/Users/myname/desktop/dockyard/enviro:/Users/myname

When containers exit, you should be able to pull logs using, e.g.:

docker logs dbf7973608a0

Update

What you provided should work; it does for me.

I create a simple Express server on :8080 and map in a host directory to the container.

Created index.js:

const express = require('express')
const app = express()
const port = 8080

app.get('/', (req, res) => res.send('Happy Birthday Freddie!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

And package.json:

{
    "name": "test",
    "version": "0.0.1",
    "scripts": {
        "start": "node ./index.js"
    },
    "dependencies": {
        "express": "~4.17.1"
    }
}

Then built|ran it:

docker build --tag=56822320 .
docker run \
--interactive \
--tty \
--publish=8080:8080 \
--volume=${PWD}:/test \
56822320

And it works:

curl localhost:8080
Happy Birthday Freddie!

To prove that the mapping is working:

docker run \
--interactive \
--tty \
--publish=8080:8080 \
--volume=${PWD}:/test \
56822320 /bin/sh
# ls /test
Dockerfile  index.js  package.json

HTH!

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • Thanks for response. Tried `docker run -p 49160:8080 -d myimage -v /Users/myname/desktop/dockyard/enviro:/usr/src/main` and now exiting with code 0. Logs simply return `v11.15.0`. If I run `docker run -p 49160:8080 -d myimage` it works fine. So not sure why the volume flag would exit code 0. – Ryan Skene Jun 30 '19 at 14:26
  • Please add more details to your question and I'll try to repro this. IIUC you'll need to run some Node.JS process to keep the container running. Exit code 0 is a completion without error. Perhaps that's what's missing? Your Dockerfile would be insightful. – DazWilkin Jun 30 '19 at 14:41
  • so that worked thank u very much. ill try to figure out what exactly was the difference between the two. One thing I noticed: if i use `--volume=${PWD}:/usr/src/main` instead of `--volume=${PWD}:/test` it throws `Error: Cannot find module 'express'`. why would express be available to one path and not the other? – Ryan Skene Jun 30 '19 at 20:19
  • You're welcome! In your example you have `WORKDIR /usr/src/main` and then `COPY package*.json ./`. The `./` in that copy refers to the `WORKDIR` set previously i.e. `/usr/src/main`. The `${PWD}` in the `--volume` map your current directory to `/usr/src/main` too. So there's likely some redundancy in there. It's good practice to not attempt to copy packages into the container but to have the container do the `npm install`. – DazWilkin Jun 30 '19 at 21:46