Search code examples
node.jsdockervolume

Docker container does not run when using --mount flag


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

Solution

  • 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!