gpt4 book ai didi

Docker compose 没有启动所有容器(可能与 Flask 相关?)

转载 作者:行者123 更新时间:2023-12-01 19:26:56 25 4
gpt4 key购买 nike

这是我的撰写文件:

version: '2'
services:
web:
build: ./web
ports:
- "3000:3000"
links:
- api
api:
build: ./api
links:
- db
db:
image: postgres

这是我的 web 服务的 Dockerfile:

FROM node:8.9.3

WORKDIR /app

COPY ./package.json /app
COPY ./server /app/server
COPY ./client /app/client

EXPOSE 3000

ENV PORT 3000

RUN ["npm", "install"]
ENTRYPOINT ["npm", "start"]

对于 api 项目:

FROM python:3.6

WORKDIR /api

COPY ./requirements.txt /api/requirements.txt
COPY ./src /api/src

EXPOSE 80

ENV PORT 80

RUN ["pip", "install", "-r", "requirements.txt"]
RUN ["python", "src/main.py"]

当我运行docker-compose up时,只有api服务启动。但是,如果我完全注释掉 api 服务并再次运行 docker-compose upwebdb > 服务启动。我设置 api 项目的方式有问题吗?我做错了什么?

为了完整起见,这里是 web 项目的起始文件(使用 Express)

http.createServer(app).listen(app.get("port"), function() {
console.log("Express server listening on port " + app.get("port"));
});

api 项目(使用 Flask):

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)

更新

我注释掉了我的 Flask 服务器的主线,所有 3 个服务都能够启动。缺点是……现在我没有 api 服务器。 Flask 是什么原因导致其他服务无法使用?

更新2

有些人要求查看日志。

如果我在没有 flask 的情况下开始,以下是日志:

Building web
Step 1/10 : FROM node:8.9.3
---> 2eeae8debf3d
Step 2/10 : WORKDIR /app
---> Using cache
---> 02199a27dafb
Step 3/10 : COPY ./package.json /app
---> Using cache
---> fafac64ad492
Step 4/10 : COPY ./app.js /app
---> Using cache
---> 0b1e3067451b
Step 5/10 : COPY ./controllers /app/server

---> Using cache
---> ff00fa864078
Step 6/10 : COPY ./client /app/client
---> Using cache
---> 587214c84267
Step 7/10 : EXPOSE 3000
---> Using cache
---> ddd5b795fcf5
Step 8/10 : ENV PORT 3000
---> Using cache
---> ca4c37f63468
Step 9/10 : RUN npm install
---> Using cache
---> 74b289885447
Step 10/10 : ENTRYPOINT npm start
---> Using cache
---> 9c5ec6770c47
Successfully built 9c5ec6770c47
Successfully tagged testcomposemachine_web:latest
Recreating testcomposemachine_web_1
Starting testcomposemachine_db_1
Attaching to testcomposemachine_db_1, testcomposemachine_web_1
db_1 | 2017-12-28 23:11:55.861 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2017-12-28 23:11:55.861 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2017-12-28 23:11:55.874 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2017-12-28 23:11:55.907 UTC [24] LOG: database system was shut down at 2017-12-28 22:59:27 UTC
db_1 | 2017-12-28 23:11:55.919 UTC [1] LOG: database system is ready to accept connections
web_1 |
web_1 | > docker-web@1.0.0 start /app
web_1 | > node app.js
web_1 |
web_1 | Express Web server listening on port 3000

如果我包含 flask ,这是日志:

Building api
Step 1/8 : FROM python:3.6
---> c1e459c00dc3
Step 2/8 : WORKDIR /api
---> Using cache
---> 8c9da963377c
Step 3/8 : COPY ./requirements.txt /api/requirements.txt
---> Using cache
---> 654c815801f6
Step 4/8 : COPY ./src /api/src
---> 5627279f7323
Removing intermediate container 453bf031c2f7
Step 5/8 : EXPOSE 80
---> Running in efb18f950f7d
---> 1389c63a0bcb
Removing intermediate container efb18f950f7d
Step 6/8 : ENV PORT 80
---> Running in 2ad88bed6343
---> fa85201cc165
Removing intermediate container 2ad88bed6343
Step 7/8 : RUN pip install -r requirements.txt
---> Running in e19107f5a449
Collecting flask==0.12.2 (from -r requirements.txt (line 1))
Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
Collecting itsdangerous>=0.21 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Werkzeug>=0.7 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading Werkzeug-0.13-py2.py3-none-any.whl (311kB)
Collecting click>=2.0 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting Jinja2>=2.4 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask==0.12.2->-r requirements.txt (line 1))
Downloading MarkupSafe-1.0.tar.gz
Building wheels for collected packages: itsdangerous, MarkupSafe
Running setup.py bdist_wheel for itsdangerous: started
Running setup.py bdist_wheel for itsdangerous: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/88/a7/30/e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
Successfully built itsdangerous MarkupSafe
Installing collected packages: itsdangerous, Werkzeug, click, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.13 click-6.7 flask-0.12.2 itsdangerous-0.24
---> a7be838d3a6e
Removing intermediate container e19107f5a449
Step 8/8 : RUN python src/main.py
---> Running in e86c8adf46f0
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

请注意,当包含 Flask 时,其他容器甚至无法构建?

最佳答案

您的应用程序 dockerfile 中有一个无法完成的 RUN 命令。这会阻止您的 dockerfile 完成构建。这就是为什么它会说步骤 8/8 而不会像 Web 容器那样说“成功构建”。我会将最后运行的命令移动到入口点或 CMD 中,就像您将其放在 Web docker 文件中一样,这样 docker build 命令就可以实际完成。

IE CMD ["python", "src/main.py"]ENTRYPOINT ["python", "src/main.py"]

这很可能是阻止构建 docker 镜像的原因。是因为 APP 中的最后一行您在前台启动一个永远不会关闭的网络服务器。最好的办法是将命令放入入口点,这样它实际上不会在构建过程中运行,而是在镜像启动时运行。

这是我正在谈论的一行:

步骤 8/8:运行 python src/main.py
---> 在 e86c8adf46f0 中运行
* 在http://0.0.0.0:80/上运行(按CTRL+C退出)

VS 这个是网络构建的。

第 10/10 步:入口点 npm start
---> 使用缓存
---> 9c5ec6770c47
成功构建9c5ec6770c47
已成功标记 testcomposemachine_web:latest
重新创建 testcomposemachine_web_1
启动 testcomposemachine_db_1
附加到 testcomposemachine_db_1、testcomposemachine_web_1

如果命令从未完成,则构建将永远不会完成。所以,是的,Flask API 在某种程度上阻止了 docker-compose 命令构建所有其他文件。

关于Docker compose 没有启动所有容器(可能与 Flask 相关?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48014433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com