【发布时间】:2019-03-22 11:30:34
【问题描述】:
我一直在为此挠头。我的 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) 当我在容器中运行我的 python 应用程序时,我需要一个 docker 镜像。 (这行得通)
2) 我需要一个 docker-compose 文件,它运行 2 个服务 + 我的 python 应用程序,但是在运行我的 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 脚本时发生)
【问题讨论】:
-
提示:你设置了
restart: unless-stopped。 -
@KlausD.Yikes,真的那么简单吗?我会尝试摆脱它并检查会发生什么。但即使它重新启动,它也不会回显我的 shell 脚本在 else 条件下应该回显的内容......每次重新启动时的入口点不都一样吗?
标签: python bash shell docker docker-compose