Docker Container Failed to Run

The Dockerfile for my application is as follows

               # Tells the Docker which base image to start.
               FROM node

               # Adds files from the host file system into the Docker container.  
               ADD . /app

               # Sets the current working directory for subsequent instructions
               WORKDIR /app

                RUN npm install
                RUN npm install -g bower
                RUN bower install --allow-root
                RUN npm install -g nodemon

                #expose a port to allow external access
                EXPOSE 9000 9030 35729

                # Start mean application
                CMD ["nodemon", "server.js"]

The docker-compose.yml file is as follows

  • Launching more than one container per instance on Elastic Beanstalk
  • How to pass securely SSH Keys to Docker Build?
  • Mounting linux container on windows host
  • Docker stops immediately upon running - EOF error
  • How to escape Fish shell variable to use as awk pattern?
  • Docker installation
  •                           web:
                                     build: .
                                     links:
                                                - db
                                      ports:
                                               - "9000:9000"
                                               - "9030:9030"
                                               - "35729:35729"
                                       db:
                                               image: mongo:latest
                                               ports: 
                                                      - "27017:27017"
    

    And the error generated while running is as follows:-

       web_1  | [nodemon] 1.11.0
       web_1  | [nodemon] to restart at any time, enter `rs`
       web_1  | [nodemon] watching: *.*
       web_1  | [nodemon] starting `node server.js`
       web_1  | Server running at http://127.0.0.1:9000
       web_1  | Server running at https://127.0.0.1:9030
       web_1  | 
       web_1  | /app/node_modules/mongodb/lib/server.js:261
       web_1  |         process.nextTick(function() { throw err; })
       web_1  |                                       ^
      web_1  | MongoError: failed to connect to server [localhost:27017] on first connect
       web_1  |     at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:313:35)
      web_1  |     at emitOne (events.js:96:13)
      web_1  |     at Pool.emit (events.js:188:7)
      web_1  |     at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:271:12)
     web_1  |     at Connection.g (events.js:291:16)
     web_1  |     at emitTwo (events.js:106:13)
     web_1  |     at Connection.emit (events.js:191:7)
     web_1  |     at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:165:49)
    web_1  |     at Socket.g (events.js:291:16)
    web_1  |     at emitOne (events.js:96:13)
    web_1  |     at Socket.emit (events.js:188:7)
    web_1  |     at emitErrorNT (net.js:1281:8)
    web_1  |     at _combinedTickCallback (internal/process/next_tick.js:74:11)
    web_1  |     at process._tickCallback (internal/process/next_tick.js:98:9)
    web_1  | [nodemon] app crashed - waiting for file changes before starting...
    

    I have uploaded the image for my application at DockerHub as crissi/airlineInsurance.

  • Deploying Golang web app static files with Docker container
  • Dockerized HAProxy and VIRTUAL_HOST for many sites
  • Running a script from a mongodb docker-container
  • Why is process.env._ undefined on some versions of nodejs in some circumstances?
  • Inspect mysql docker container over ssh tunnel
  • Setting up logging with PHP official docker image
  • One Solution collect form web for “Docker Container Failed to Run”

    In docker you can’t connect to an other container via localhost because each container is independend and has its own IP. You should use container_name:port. In your example it should be db:27017 to connect from your NodeJS application in ‘web’ to the MongoDB in ‘db’.

    So it’s not the problem of your Dockerfile. It’s the connection URL from your NodeJS application that points to localhost instead of db.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.