Search code examples
node.jsdockerdocker-composedockerfiledocker-swarm

How to start the container for the Node.js application using just Docker-compose.yml file without using the Dockerfile


I am trying to implement a simple Node.js application using just the Docker-compose file. I am a bit confused as I start the docker using the command docker-compose up and check the running container it says restarting and nothing happens.

I want to implement this Node.js application using just the docker-compose.yml file and without any use of Dockerfile. Most of the examples in the net are using the separate Dockerfile for Node.js to build the image and this image has been used by the docker-compose.yml file to create the container.

I tried the same with Dockerfile and it worked perfectly for me. But when I remove this Dockerfile and just use the docker-compose.yml file then I run into a restarting issue:

Error response from daemon: Container a7fe8cdc262ae6c57e33b60b1a69084df1313e590 is restarting, wait until the container is running

docker-compose.yml file:

version: '3'

services:
  db:
    build: ./db
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_ROOT_PASSWORD: mypass
      MYSQL_USER: mysql
      MYSQL_PASSWORD: mypass
      DATABASE_HOST: myhost
  web:
    image: node:8
    volumes:
      - ./web:/usr/src/app
    working_dir: /usr/src/app
    command: bash -c "npm run start && tail -F"
    depends_on:
      - db
    restart: on-failure

My index.js file:

     //Make NodeJS to Listen to a particular Port in Localhost
     const   port        =   process.env.PORT || 9000;; 
     app.listen(port, function(){
        // Start the server and read the parameter passed by User
        console.log("Node js is Running on : "+port);
        // Get process.stdin as the standard input object.
        var standard_input = process.stdin;

        // Set input character encoding.
        standard_input.setEncoding('utf-8');

        // Prompt user to input data in console.
        console.log("Please input text in command line.");

        // When user input data and click enter key.
        standard_input.on('data', function (data) {
            console.log(" DATA ENTERED BY USER IS :"+data);
        });
    });

My previous Dockerfile for Node.js which I am trying to remove:

FROM node:8

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 9000

CMD ["npm","start"]

CMD tail -f /dev/null

Solution

  • Thanks a lot, everyone for the answers and comments. If anyone is stuck in this then I used following commands in docker-compose.yml file. This does not require any additional Dockerfile for the building of the image.

    version: '3'
    
    services:
      db:
        image: mysql:5.7
        environment:
          MYSQL_DATABASE: mydb
          MYSQL_ROOT_PASSWORD: mypass
          MYSQL_USER: mysql
          MYSQL_PASSWORD: mypass
          DATABASE_HOST: myhost
    
      web:
        container_name: web
        image: node:8
        volumes:
          - ./web:/usr/src/app
        working_dir: /usr/src/app
        depends_on:
          - db
        restart: on-failure
        command: "tail -f /dev/null && npm start"
    
      adminer:
        image: adminer
        restart: always
        ports:
          - "7778:8080"
    

    Also, an important this is to have this command in your package.json file, if yu dont have this command then you will run into some issue:

    "start": "nodemon index.js",

     "main": "index.js",
      "scripts": {
        "preinstall": "npm i nodemon -g",
        "start": "nodemon index.js",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",