简体   繁体   English

如何在不使用 Dockerfile 的情况下仅使用 Docker-compose.yml 文件启动 Node.js 应用程序的容器

[英]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.我正在尝试仅使用Docker-compose文件来实现一个简单的 Node.js 应用程序。 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.当我使用命令docker-compose up启动 docker 并检查它说restarting的正在运行的容器时,我有点困惑,但没有任何反应。

I want to implement this Node.js application using just the docker-compose.yml file and without any use of Dockerfile .我想只使用docker-compose.yml文件而不使用Dockerfile来实现这个 Node.js 应用程序。 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.网络中的大多数示例都使用单独的Dockerfile for Node.js 来构建映像,并且docker-compose.yml文件已使用此映像来创建容器。

I tried the same with Dockerfile and it worked perfectly for me.我对Dockerfile进行了同样的尝试,它对我来说非常有效。 But when I remove this Dockerfile and just use the docker-compose.yml file then I run into a restarting issue:但是,当我删除此Dockerfile并仅使用docker-compose.yml文件时,我遇到了restarting问题:

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

docker-compose.yml file: 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

My index.js file:我的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);
        });
    });

My previous Dockerfile for Node.js which I am trying to remove:我之前尝试删除的Dockerfile的 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

Start node application through docker-compose file.通过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"

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.如果有人陷入其中,那么我在docker-compose.yml文件中使用了以下命令。 This does not require any additional Dockerfile for the building of the image.这不需要任何额外的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"

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:此外,重要的是在您的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": "",

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 能够使用 docker-compose.yml 在容器和 redis 容器中启动 node js 应用程序,但无法访问应用程序 - Able to bring up a node js application in a container and a redis container using docker-compose.yml but unable to access application Docker compose is using the env which was built by Dockerfile, instead of using env_file located in docker-compose.yml directory because of webpack - Docker compose is using the env which was built by Dockerfile, instead of using env_file located in docker-compose.yml directory because of webpack 无法解析 dockerfile - 没有这样的文件或目录 - docker-compose.yml - Failed to resolve dockerfile - no such file or directory - docker-compose.yml Node.js 未在 Docker 中运行,我的 docker-compose.yml 是否正确? - Node.js is not running in Docker is my docker-compose.yml correct? 在没有docker-compose.yml文件的单个docker文件中运行react和node - running react and node in a single docker file without docker-compose.yml file 如何使用 Docker Compose 设置 Node.js 开发环境 - How setup a Node.js development environment using Docker Compose 为什么我的节点容器会忽略我在docker-compose.yml中设置的环境变量? - Why does my node container ignore the environment variable I set in docker-compose.yml? 使用环境变量的 Node.js 和 MongoDB 容器之间的连接问题。 在 Docker 编写 - Problems in connection between Node.js and MongoDB container using environment var. in Docker Compose Node.js in docker-compose容器无法使用绝对路径访问资源 - Node.js in docker-compose container can't access resource using absolute path 基于 docker-compose.yml 文件的节点环境变量的切换命令 - switching command based on node env variable for the docker-compose.yml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM