简体   繁体   中英

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:

# 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:

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:

#!/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. (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. 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.

I read that using entrypoint in a docker-compose file overwrites any old entrypoint/cmd given to the Dockerfile. 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.

(This is the main question)

In the if portion, I do not run my application in the shell script. 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 . 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! 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?

(This is only happens if I run through docker-compose, not if I just run the shell script with sh docker-entrypoint.sh)

Problem 1

Using ENTRYPOINT and CMD at the same time has some strange effects .

Problem 2

This happens to your container:

  1. It is started the first time. The .initialized file does not exist.
  2. The if case is executed. The file is created.
  3. The script and therefore the container ends.
  4. The restart: unless-stopped option restarts the container.
  5. The .initialized file exists now, the else case is run.
  6. python3 __main__.py -debug is executed.

BTW the USER command in the Dockerfile or the user option in Docker Compose are better options than sudo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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