Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Docker container not return control to the terminal after starting?

I have a shell script that I use to export Env variables. This script calls a python script to get certain values from a web service that I need to store before running my primary python script.

I've tried using a RUN . /bot/env/setenv.sh, but this doesn't seem to make the env variables available in the final container. I've tried putting the contents in an entrypoint.sh file that ends in calling python jbot.py, but the container never completes its setup (I assume because the script inside the entrypoint is a continuous loop?)

My entrypoint.sh looks like this:

#!/bin/bash
. /jirabot/env/setenv.sh
python jbot.py

And the setenv.sh is just:

#!/bin/bash
export SLACK_BOT_TOKEN="xoxb-token"
export BOT_ID=`python env/print_bot_id.py ${SLACK_BOT_TOKEN}`

My full Dockerfile is:

FROM python:2

COPY jirabot/ /jirabot/
RUN pip install slackclient schedule jira
WORKDIR /jirabot
#CMD [ "python", "jbot.py" ]
ENTRYPOINT [ "/jirabot/entrypoint.sh" ]

When I do docker run bot, I can verify that the application is running (the bot responds to my requests appropriately). However, all of the print() statements within jbot.py are absent from the output -- so I have two primary questions:

  1. Why does my entrypoint.sh just hang the container from returning? I do docker run bot, and I'm never returned control of the terminal. However, the bot seems to startup fine.

  2. Why do I not get any of my print statements from jbot.py when I open a second terminal and do docker logs <container>?

fwiw, my jbot.py is a while True: loop, monitoring for input.

like image 947
MrDuk Avatar asked Sep 03 '25 02:09

MrDuk


2 Answers

  1. You are not running the docker container as a daemon.

docker run -d bot

  1. In my experience, print messages don't make it to the logs without input buffering disabled in python.

python -u jbot.py

like image 77
Shadow Avatar answered Sep 05 '25 14:09

Shadow


For your first question, you should check the documentation of docker run. Shortly, you are attaching with container so you will never return to your terminal. In order to detach, you need to add option -d.

The common used command to launch a container is docker run -idt <container>.

For your second question, the information is not enough to identify the problem, sorry. Maybe you can try again after you launching a container properly.

like image 28
Sraw Avatar answered Sep 05 '25 13:09

Sraw