[英]Able to bring up a node js application in a container and a redis container using docker-compose.yml but unable to access application
[英]How to start the container for the Node.js application using just Docker-compose.yml file without using the Dockerfile
我正在尝试仅使用Docker-compose
文件来实现一个简单的 Node.js 应用程序。 当我使用命令docker-compose up
启动 docker 并检查它说restarting
的正在运行的容器时,我有点困惑,但没有任何反应。
我想只使用docker-compose.yml
文件而不使用Dockerfile
来实现这个 Node.js 应用程序。 网络中的大多数示例都使用单独的Dockerfile
for Node.js 来构建映像,并且docker-compose.yml
文件已使用此映像来创建容器。
我对Dockerfile
进行了同样的尝试,它对我来说非常有效。 但是,当我删除此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);
});
});
我之前尝试删除的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
通过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"
非常感谢大家的回答和评论。 如果有人陷入其中,那么我在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": "",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.