docker compose:为什么在这里调用我的python应用程序?

docker compose:为什么在这里调用我的python应用程序?,python,bash,shell,docker,docker-compose,Python,Bash,Shell,Docker,Docker Compose,我为这个挠头已有一段时间了。我的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

我为这个挠头已有一段时间了。我的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编写文件:

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) 我需要一个docker compose文件,它运行2个服务+我的python应用程序,但在运行python应用程序之前,我需要做一些初始化工作,为此我创建了一个shell脚本docker-entrypoint.sh。当我第一次在机器上部署应用程序时,我只想做一次初始化工作。因此,我正在创建一个.initialized隐藏文件,并将其用作shell脚本中的检查

我了解到在docker compose文件中使用entrypoint会覆盖提供给docker文件的任何旧entrypoint/cmd。这就是为什么在shell脚本的else部分中,我使用“sudopython3main.py-debug”手动运行代码的原因,else部分工作正常

(这是主要问题)

在if部分,我不在shell脚本中运行我的应用程序。我已经分别测试了shell脚本本身,if和else语句都可以正常工作,但是当我运行“sudo docker compose up”时,当shell脚本第一次命中if部分时,它会回显这两条语句,创建隐藏文件,然后运行我的应用程序。对于应用程序,控制台输出以紫色/粉色/淡紫色显示,而其他两个服务以黄色和青色打印其注销。我不确定颜色是否重要,但在正常情况下,我的应用程序日志总是绿色的,事实上前两个回音“initialized”和“Creating.initialized”也是绿色的!所以我想我应该提到这个细节。在这两次回音之后,我的应用程序神秘地开始并以紫色记录控制台输出

为什么/如何在shell脚本的if语句中调用我的应用程序?

(只有在运行docker compose时才会发生这种情况,而不是在使用sh docker entrypoint.sh运行shell脚本时才会发生)

问题1 同时使用
ENTRYPOINT
CMD
有一些好处

问题2 这会发生在您的容器上:

  • 它是第一次启动的。
    .initialized
    文件不存在
  • 如果执行了
    案例,则执行
    。文件已创建
  • 脚本和容器结束
  • 重新启动:除非停止
    选项重新启动容器
  • .initialized
    文件现在存在,运行
    else
    案例
  • python3\uuuu main\uuuuu.py-debug
    被执行

  • 顺便说一句,Docker文件中的
    USER
    命令或Docker Compose中的
    USER
    选项都比
    sudo

    更好。提示:您设置了
    重启:除非停止
    @KlausD.Yikes,否则它真的那么简单吗?我会试着摆脱它,看看会发生什么。但是,即使它重新启动,它也不会回显我的shell脚本在else条件下应该回显的内容……不是每次重新启动时的入口点都是相同的吗?只是测试了一下,你是对的!这是因为“重新启动:除非停止”,但为什么当它重新启动时,我没有看到我的“已初始化!”回音我还收到了一个“很抱歉,我们找不到该页面”的奇怪效果链接,该链接指向:非常感谢您的帮助!:我不知道为什么重启时我看不到回声,但你已经准确地指出了主要原因。