[英]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.