【问题标题】:How to start the container for the Node.js application using just Docker-compose.yml file without using the Dockerfile如何在不使用 Dockerfile 的情况下仅使用 Docker-compose.yml 文件启动 Node.js 应用程序的容器
【发布时间】:2019-10-24 12:50:16
【问题描述】:

我正在尝试仅使用 Docker-compose 文件来实现一个简单的 Node.js 应用程序。当我使用命令 docker-compose up 启动 docker 并检查它显示为 restarting 的正在运行的容器时,我有点困惑,但没有任何反应。

我想只使用docker-compose.yml 文件而不使用Dockerfile 来实现这个Node.js 应用程序。网络中的大多数示例都是使用单独的Dockerfile for Node.js 来构建图像,并且该图像已被docker-compose.yml 文件用于创建容器。

我对@9​​87654328@ 进行了同样的尝试,它对我来说非常有效。但是当我删除这个Dockerfile 并只使用docker-compose.yml 文件时,我遇到了restarting 问题:

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

docker-compose.yml文件:

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

我的index.js 文件:

     //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);
        });
    });

我之前尝试删除的 Node.js Dockerfile

FROM node:8

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 9000

CMD ["npm","start"]

CMD tail -f /dev/null

【问题讨论】:

  • 似乎直接在主机上运行 Node 会容易得多。您可以在主机上使用 Node 并在容器中使用数据库,这对您来说更有用。
  • 你怎么从tail -f /dev/null变成tail -F

标签: node.js docker docker-compose dockerfile docker-swarm


【解决方案1】:

通过docker-compose文件启动节点应用。

version: "3.7"
networks:
  main-network;

services:

 app:
   container_name: app
   image: node:latest
   restart: always
   volumes:
    - ./app:/home/node/app
   working_dir: /home/node/app
   ports:
    - 3000:3000
   networks:
    - main-network
   command: "npm start"
   depends_on:
    - db
   logging:
     driver: "json-file"
     options:
       max-file: "4"
       max-size: "100m"

【讨论】:

  • 非常感谢。我能够运行容器,但我正在进入新问题,它说端口正在使用中events.js:183 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE :::9000
  • 试试这个“netstat -tulnp | grep 3000”或者使用“kill pid”来杀死端口3000上的进程
  • 实际上我试过了,它对我有用:command: "tail -f /dev/null && npm start"
【解决方案2】:

非常感谢大家的回答和cmets。如果有人陷入困境,那么我在docker-compose.yml 文件中使用了以下命令。 这不需要任何额外的Dockerfile 来构建图像。

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"

另外,重要的是在你的package.json文件中有这个命令,如果你没有这个命令那么你会遇到一些问题:

"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": "",

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多