Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将python命令传递给dockerfile_Python_Docker_Dockerfile - Fatal编程技术网

如何将python命令传递给dockerfile

如何将python命令传递给dockerfile,python,docker,dockerfile,Python,Docker,Dockerfile,我有这个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 us

我有这个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

1.创建任何python脚本

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

FROM python:3
WORKDIR /usr/src/app
COPY . .
CMD ["test.py"]
ENTRYPOINT ["python3"]
3.建造码头工人

docker build -t hello
docker run -it hello test.py
4.管理码头工人

docker build -t hello
docker run -it hello test.py

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,当它运行容器时,应该在其中运行此命令。您可以这样构建它:

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"]
docker build——标记myPythonApp。
然后像这样运行它

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

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"]
docker run-it--rm-p9000:9000 myPythonApp
也许可以在您的浏览器上访问它`http://localhost:9000/“

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

docker run-it--rm-p9000:9000-d Mythonapp
而且,如果您确定一次只运行一个容器,那么为它命名可能会有所帮助

docker run-it--rm-p9000:9000-d--name MyPythonApp MyPythonApp
这将允许您使用以下命令终止后台容器:

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

docker rm-f$(docker ps-qa)

您可以显示您的dockerfile吗?您想将其传递给您的dockerfile是什么意思?如果您想在dockerfile中执行命令,请使用RUN关键字,如果您想使用某个命令启动dockerimage,请使用ENTRYPOINT。您还需要什么吗?我在问题中添加了dockerfile,@asafary,我想要to执行命令,请检查我编写的Dockerfile是否正确运行正常,但您也可以使用ENTRYPOINT而不是RUN。当我运行Dockerfile时,它将在此处运行一段时间。不要向前移动步骤7/7:运行python3 smartinsights/main.py start--config config.yml-->在8中运行a81bfe608d6正在运行…注册操作事件注册操作事件标记注册内存计数注册内存循环计数注册内存帐户注册自动关闭计数注册年龄分析数据[2021-04-14 09:57:19+0000][9][INFO]Goin'Fast@[2021-04-14 09:57:19+0000][9][INFO]Starting worker[9]你能把这个答案调整得更具体一点,并解释为什么要做你建议的事情吗?(而且这个
ENTRYPOINT
/
CMD
split没有任何意义,我会把问题一开始的整个命令放在
CMD
中,根本没有
ENTRYPOINT
)您好,@DavidMaze,当以RUN python3 app/main.py start--config config.yml的形式运行命令时,我得到了问题中提到的错误,但现在当我使用ENTRYPOINT时,它正在运行,我可以从容器中看到以下日志。/usr/src/app/smartinsights/system/DB.py:27:SyntaxWarning:“is”带有一个字面值。您是指什么=="? 跑步[2021-04-14 10:46:29+0000][10][INFO]Goin'Fast@[2021-04-14 10:46:29+0000][10][INFO]Starting worker[10]。入口点的构造确实是错误的。入口点应该在父映像中设置,并且适合容器中的发行版。CMD更合适。您好,@Software Engineer,在使用CMD命令运行dockerfile后,容器运行良好,并给出以下日志。正在将/usr/src/app/as项目文件夹设置为正在运行。。。注册ActionEvent注册ActionIncidentTag注册MemoryCount注册MemoryCount注册MemoryCount注册AutoCloseCount注册AgeingAnalysisData[2021-04-14 11:57:56+0000][7][INFO]快速运行[2021-04-14 11:57:56+0000][7][INFO]开始工作[7]我不是python开发人员,所以我不知道这是好是坏好,谢谢你的帮助,但是,如果它正在运行,请点击我答案上的勾号。此外,您可能需要在docker run中的“run”一词后添加
-p 9000:9000
。这将公开它运行的端口(它列在您发布的输出中)。