我已经为此挠头一段时间了。我的 python 应用程序有以下 Dockerfile:
# Use an official Python runtime as a parent image
FROM frankwolf/rpi-python3
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
RUN chmod 777 docker-entrypoint.sh
# Install any needed packages specified in requirements.txt
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt
# Run __main__.py when the container launches
CMD ["sudo", "python3", "__main__.py", "-debug"] # Not sure if I need sudo here
docker-compose 文件:
version: "3"
services:
mongoDB:
restart: unless-stopped
volumes:
- "/data/db:/data/db"
ports:
- "27017:27017"
- "28017:28017"
image: "andresvidal/rpi3-mongodb3:latest"
mosquitto:
restart: unless-stopped
ports:
- "1883:1883"
image: "mjenz/rpi-mosquitto"
FG:
privileged: true
network_mode: "host"
depends_on:
- "mosquitto"
- "mongoDB"
volumes:
- "/home/pi:/home/pi"
#image: "arkfreestyle/fg:v1.8"
image: "test:latest"
entrypoint: /app/docker-entrypoint.sh
restart: unless-stopped
这就是 docker-entrypoint.sh 的样子:
#!/bin/sh
if [ ! -f /home/pi/.initialized ]; then
echo "Initializing..."
echo "Creating .initialized"
# Create .initialized hidden file
touch /home/pi/.initialized
else
echo "Initialized already!"
sudo python3 __main__.py -debug
fi
这是我想要做的:
(这东西已经有效了)
1) 我需要一个 docker 映像,当我在容器中运行它时,它会运行我的 python 应用程序。(这有效)
2)我需要一个运行 2 个服务 + 我的 python 应用程序的 docker-compose 文件,但是在运行我的 python 应用程序之前我需要做一些初始化工作,为此我创建了一个 shell 脚本,它是 docker-entrypoint.sh。当我第一次在一台机器上部署我的应用程序时,我只想做一次初始化工作。所以我正在创建一个 .initialized 隐藏文件,我在我的 shell 脚本中使用它作为检查。
我读到在 docker-compose 文件中使用入口点会覆盖提供给 Dockerfile 的任何旧入口点/cmd。所以这就是为什么在我的 shell 脚本的 else 部分我使用“sudo python3 main .py -debug”手动运行我的代码,这个 else 部分工作正常。
(这是主要问题)
在 if 部分,我没有在 shell 脚本中运行我的应用程序。我已经分别测试了 shell 脚本本身,if 和 else 语句都按我的预期工作,但是当我运行“sudo docker-compose up”时,当我的 shell 脚本第一次遇到 if 部分时,它会响应这两个语句,创建隐藏文件,然后运行我的应用程序。应用程序的控制台输出显示为紫色/粉红色/淡紫色,而其他两个服务以黄色和青色打印它们的日志。我不确定颜色是否重要,但在正常情况下,我的应用程序日志始终为绿色,实际上前两个回显“Initializing”和“Creating .initialized”也是绿色的!所以我想我会提到这个细节。在这两个回声之后,
为什么/如何在 shell 脚本的 if 语句中调用我的应用程序?
(这只发生在我通过 docker-compose 运行时,而不是我只使用 sh docker-entrypoint.sh 运行 shell 脚本时)
同时使用ENTRYPOINT
和CMD
有一些奇怪的效果。
这发生在您的容器上:
.initialized
文件不存在。if
情况下被执行。文件已创建。restart: unless-stopped
选项会重新启动容器。.initialized
文件现在存在,else
案例运行。python3 __main__.py -debug
被执行。顺便说一句,USER
Dockerfile 中的命令或user
Docker Compose 中的选项比sudo
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句