简体   繁体   English

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

[英]docker-compose: Why is my python application being invoked here?

I've been scratching my head for a while with this. 我已经为此挠了一下头。 I have the following Dockerfile for my python application: 我的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 file: 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

And this what docker-entrypoint.sh looks like: 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

Here's what I am trying to do: 这是我想做的事情:

(This stuff already works) (这个东西已经可以用了)

1) I need a docker image which runs my python application when I run it in a container. 1)我需要一个在容器中运行python应用程序时运行的docker映像。 (this works) (这有效)

2) I need a docker-compose file which runs 2 services + my python application, BUT before running my python application I need to do some initialization work, for this I created a shell script which is docker-entrypoint.sh. 2)我需要一个运行2个服务+我的python应用程序的docker-compose文件,但是在运行python应用程序之前,我需要做一些初始化工作,为此,我创建了一个shell脚本docker-entrypoint.sh。 I want to do this initialization work ONLY ONCE when I deploy my application on a machine for the first time . 我只想在第一次将应用程序部署到计算机上时进行一次初始化工作。 So I'm creating a .initialized hidden file which I'm using as a check in my shell script. 所以我正在创建一个.initialized隐藏文件,用作我的shell脚本中的检查。

I read that using entrypoint in a docker-compose file overwrites any old entrypoint/cmd given to the Dockerfile. 我读到在docker-compose文件中使用入口点会覆盖提供给Dockerfile的所有旧入口点/ cmd。 So that's why in the else portion of my shell script I'm manually running my code using "sudo python3 main .py -debug", this else portion works fine. 因此,这就是为什么在我的shell脚本的else部分中,我使用“ sudo python3 main .py -debug”手动运行代码时,else部分可以正常工作的原因。

(This is the main question) (这是主要问题)

In the if portion, I do not run my application in the shell script. 在if部分中,我没有在shell脚本中运行我的应用程序。 I've tested the shell script itself separately, both if and else statements work as I expect, but when I run "sudo docker-compose up", the first time when my shell script hits the if portion it echoes the two statements, creates the hidden file and THEN RUNS MY APPLICATION . 我已经分别测试了shell脚本本身,if和else语句均按预期工作,但是当我运行“ sudo docker-compose up”时,我的shell脚本第一次遇到if部分时,它会回显这两个语句,隐藏文件然后运行我的应用程序 The console output appears in purple/pink/mauve for the application, while the other two services print their logs out in yellow and cyan. 对于应用程序,控制台输出以紫色/粉红色/淡紫色显示,而其他两个服务以黄色和青色显示其日志。 I'm not sure if the colors matter, but in the normal condition my application logs are always green, in fact the first two echoes "Initializing" and "Creating .initialized" are also green! 我不确定颜色是否重要,但是在正常情况下,我的应用程序日志始终为绿色,实际上前两个回声“ Initializing”和“ Creating .initialized”也为绿色! so I thought I'd mention this detail. 所以我想提到这个细节。 After those two echoes, my application mysteriously begins and logs console output in purple... 在这两个回声之后,我的应用程序开始神秘地启动,并以紫色记录控制台输出...

Why/how is my application being invoked in the if statement of the shell script? 为什么/如何在shell脚本的if语句中调用我的应用程序?

(This is only happens if I run through docker-compose, not if I just run the shell script with sh docker-entrypoint.sh) (这只会在我通过docker-compose运行时发生,而不是仅在我使用sh docker-entrypoint.sh运行shell脚本时发生)

Problem 1 问题1

Using ENTRYPOINT and CMD at the same time has some strange effects . 同时使用ENTRYPOINTCMD会有一些奇怪的效果

Problem 2 问题2

This happens to your container: 这发生在您的容器上:

  1. It is started the first time. 它是第一次启动。 The .initialized file does not exist. .initialized文件不存在。
  2. The if case is executed. if执行case。 The file is created. 文件已创建。
  3. The script and therefore the container ends. 脚本并因此结束容器。
  4. The restart: unless-stopped option restarts the container. restart: unless-stopped选项重新启动容器。
  5. The .initialized file exists now, the else case is run. .initialized文件现在存在, else运行案例。
  6. python3 __main__.py -debug is executed. python3 __main__.py -debug被执行。

BTW the USER command in the Dockerfile or the user option in Docker Compose are better options than sudo . 顺便说一句,Dockerfile中的USER命令或Docker Compose中的user选项比sudo更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM