How to pass python command to dockerfile

I am having this python command, not sure how to pass it in dockerfile.

Command

python3 app/main.py start --config config.yml

I am writing dockerfile but not sure how to pass the above command in my docker file. In my main.py file I have given start, stop condition in form of actions.

config.yaml file

host: 127.0.0.1
port: 9000
db: elastic
elastic:
  port: 9200
  host: localhost
  user: null
  secret: null
  ssl: false
sqlserver:
  port: 1433
  host: localhost
  instance: MSSQLSERVER
  ssl: false
  user: null
  password: null
kafka:
  port: null
  host: null
api-docs: true
rocketchat:
  host:null
  port:null
auth-backend:
  - basic
  - bearer
  - oauth
name: Smartapp

Dockerfile

FROM python:3.8
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
RUN python3 app/main.py start --config config.yml

When I am running the dockerfile, it is going in an infinite loop at RUN step.

Step 7/7 : RUN python3 smartinsights/main.py start --config config.yml
 ---> Running in 8a81bfe608d6
[91m/usr/src/app/smartinsights/system/DB.py:27: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if self.database_name is 'elastic':
/usr/src/app/smartinsights/system/DB.py:29: SyntaxWarning: "is" with a literal. Did you mean "=="?
  elif self.database_name is 'sqlserver':
[0mSetting /usr/src/app/smartinsights as project folder
Running...
registering ActionIncident
registering ActionIncidentTag
registering MemoryCount
registering MemoryCloseCount
registering MemoryOpenCount
registering AutoCloseCount
registering AgeingAnalysisData
[2021-04-14 09:57:19 +0000] [9] [INFO] Goin' Fast @ http://127.0.0.1:8000
[2021-04-14 09:57:19 +0000] [9] [INFO] Starting worker [9]

Below error can also be seen at startup server

[2021-04-14 10:17:37 +0000] [9] [INFO] Goin' Fast @ http://localhost:8000
[2021-04-14 10:17:37 +0000] [9] [ERROR] Unable to start server
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sanic/server.py", line 891, in serve
    http_server = loop.run_until_complete(server_coroutine)
  File "uvloop/loop.pyx", line 1494, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1768, in create_server
OSError: [Errno 99] error while attempting to bind on address ('::1', 8000, 0, 0): cannot assign requested address
[2021-04-14 10:17:37 +0000] [9] [INFO] Server Stopped

Answer

The dockerfile ‘builds’ an image — you should/must not run your application during the build process. You want your application to run only when the container runs.

Change your dockerfile to look like this:

FROM python:3.8
WORKDIR /pyapp/
COPY app/* app/
COPY . .
RUN pip install -r requirements.txt
CMD ["python3", "app/main.py", "start", "--config", "config.yml"]

This CMD line tells docker that when it runs the container, it should run this command within it. You can build it like this:

docker build --tag myPythonApp .

And run it like this

docker run -it --rm myPythonApp

You have added some output in the comments that suggests that this container is listening on port 9000. You can expose this port on the host like this:

docker run -it --rm -p 9000:9000 myPythonApp

And maybe access it in your browser on `http://localhost:9000/”.

That command will run the container in the current shell process. When you hit ctrl+c then the process will stop and the container will exit. If you want to keep the container running in the background try this:

docker run -it --rm -p 9000:9000 -d myPythonApp

And, if you’re sure that you’ll only be running one container at a time, it may help to give it a name.

docker run -it --rm -p 9000:9000 -d --name MyPythonApp myPythonApp

That will allow you to kill a background container with:

docker rm -f MyPythonApp

Btw, if you’re in a mess, and you’re running bash, you can remove all running and stopped containers with:

docker rm -f $(docker ps -qa)