【问题标题】:docker-compose: Why is my python application being invoked here?docker-compose:为什么在这里调用我的 python 应用程序?
【发布时间】: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 ma​​in.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


【解决方案1】:

问题 1

同时使用ENTRYPOINTCMD有一些strange effects

问题 2

这发生在您的容器上:

  1. 第一次启动。 .initialized 文件不存在。
  2. if 案例被执行。文件已创建。
  3. 脚本和容器结束。
  4. restart: unless-stopped 选项重新启动容器。
  5. .initialized 文件现已存在,else 案例正在运行。
  6. python3 __main__.py -debug 被执行。

顺便说一句,Dockerfile 中的 USER 命令或 Docker Compose 中的 user 选项比 sudo 更好。

【讨论】:

  • 刚刚测试过,你是对的!这是因为“重新启动:除非停止”,但是当它重新启动时我没有看到我的“已经初始化!”回声? :o
  • 我还收到一个“抱歉,我们找不到那个页面”的奇怪效果链接,它指向:docs.docker.com/engine/reference/…
  • 非常感谢您的帮助! :D 我不知道为什么我在重启时看不到回声,但你已经完美地指出了主要原因。
猜你喜欢
  • 2018-08-20
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 2022-10-13
  • 2020-04-23
  • 2011-10-17
  • 1970-01-01
相关资源
最近更新 更多