如何将python命令传递给dockerfile

2024-09-29 21:31:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个python命令,不知道如何在dockerfile中传递它

命令

python3 app/main.py start --config config.yml

我正在编写dockerfile,但不确定如何在docker文件中传递上述命令。在main.py文件中,我以动作的形式给出了开始、停止条件

config.yaml文件

host: 127.0.0.1
port: 9000
db: elastic
elastic:
  port: 9200
  host: localhost
  user: null
  secret: null
  ssl: false
sqlserver:
  port: 1433
  host: localhost
  instance: MSSQLSERVER
  ssl: false
  user: null
  password: null
kafka:
  port: null
  host: null
api-docs: true
rocketchat:
  host:null
  port:null
auth-backend:
  - basic
  - bearer
  - oauth
name: Smartapp

Dockerfile

FROM python:3.8
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
RUN python3 app/main.py start --config config.yml

当我运行dockerfile时,它在运行步骤以无限循环的方式运行

Step 7/7 : RUN python3 smartinsights/main.py start --config config.yml
 ---> Running in 8a81bfe608d6
[91m/usr/src/app/smartinsights/system/DB.py:27: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if self.database_name is 'elastic':
/usr/src/app/smartinsights/system/DB.py:29: SyntaxWarning: "is" with a literal. Did you mean "=="?
  elif self.database_name is 'sqlserver':
[0mSetting /usr/src/app/smartinsights as project folder
Running...
registering ActionIncident
registering ActionIncidentTag
registering MemoryCount
registering MemoryCloseCount
registering MemoryOpenCount
registering AutoCloseCount
registering AgeingAnalysisData
[2021-04-14 09:57:19 +0000] [9] [INFO] Goin' Fast @ http://127.0.0.1:8000
[2021-04-14 09:57:19 +0000] [9] [INFO] Starting worker [9]

在启动服务器上也可以看到以下错误

[2021-04-14 10:17:37 +0000] [9] [INFO] Goin' Fast @ http://localhost:8000
[2021-04-14 10:17:37 +0000] [9] [ERROR] Unable to start server
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sanic/server.py", line 891, in serve
    http_server = loop.run_until_complete(server_coroutine)
  File "uvloop/loop.pyx", line 1494, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1768, in create_server
OSError: [Errno 99] error while attempting to bind on address ('::1', 8000, 0, 0): cannot assign requested address
[2021-04-14 10:17:37 +0000] [9] [INFO] Server Stopped

Tags: inpysrcconfigapphostserveris
2条回答

dockerfile“生成”了一个映像,在生成过程中您应该/不应该运行应用程序。您希望应用程序仅在容器运行时运行

将dockerfile更改为如下所示:

FROM python:3.8
WORKDIR /pyapp/
COPY app/* app/
COPY . .
RUN pip install -r requirements.txt
CMD ["python3", "app/main.py", "start", " config", "config.yml"]

此CMD行告诉docker,当它运行容器时,应该在其中运行此命令。您可以这样构建它:

docker build  tag myPythonApp .

然后像这样运行它

docker run -it  rm myPythonApp

您在注释中添加了一些输出,表明此容器正在侦听端口9000。您可以在主机上公开此端口,如下所示:

docker run -it  rm -p 9000:9000 myPythonApp

也许可以在您的浏览器上访问它`http://localhost:9000/“

该命令将在当前shell进程中运行容器。当您点击ctrl+c时,进程将停止,容器将退出。如果您想让容器在后台运行,请尝试以下操作:

docker run -it  rm -p 9000:9000 -d myPythonApp

而且,如果您确定一次只运行一个容器,那么为它命名可能会有所帮助

docker run -it  rm -p 9000:9000 -d  name MyPythonApp myPythonApp

这将允许您使用以下命令终止后台容器:

docker rm -f MyPythonApp

顺便说一句,如果您处于混乱状态,并且正在运行bash,则可以使用以下命令删除所有正在运行和停止的容器:

docker rm -f $(docker ps -qa)

1.创建任何python脚本

2.使用以下代码创建docker文件

FROM python:3
WORKDIR /usr/src/app
COPY . .
CMD ["test.py"]
ENTRYPOINT ["python3"]

3.建造码头工人

docker build -t hello

4.管理码头工人

docker run -it hello test.py

相关问题 更多 >

    热门问题