2

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
6
  • 1
    Can you show your dockerfile please? Commented Apr 14, 2021 at 9:59
  • what do you mean you want to pass it to your dockerfile? if you want to execute a command in dockerfile you use RUN keyword and if you want to startup your dockerimage with a certain command you use ENTRYPOINT. is there anything else you need? Commented Apr 14, 2021 at 10:00
  • I have added the Dockerfile in my question, @aSaffary, I want to execute the command, can you please check if the Dockerfile I have written is correct Commented Apr 14, 2021 at 10:04
  • RUN is fine, but you could also use ENTRYPOINT instead of RUN. Commented Apr 14, 2021 at 10:07
  • When, I am running the dockerfile, it is going on to running for a while at this place. and not moving forward Step 7/7 : RUN python3 smartinsights/main.py start --config config.yml ---> Running in 8a81bfe608d6 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 @ 127.0.0.1:8000 [2021-04-14 09:57:19 +0000] [9] [INFO] Starting worker [9] Commented Apr 14, 2021 at 10:12

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, @Software Engineer, After running the dockerfile with CMD command, container is running fine and giving the below logs. Setting /usr/src/app/ as project folder Running... registering ActionIncident registering ActionIncidentTag registering MemoryCount registering MemoryCloseCount registering MemoryOpenCount registering AutoCloseCount registering AgeingAnalysisData [2021-04-14 11:57:56 +0000] [7] [INFO] Goin' Fast @ 127.0.0.1:9000 [2021-04-14 11:57:56 +0000] [7] [INFO] Starting worker [7]
I'm not a python developer, so I don't know if that's good or bad
But, if it's running, please hit the tick on my answer. Also, you may need to add -p 9000:9000 after the word 'run' in docker run. That'll expose the port it's running on (it is listed in that output you posted).
1

1.create any python script

2.create the docker file using the following code

FROM python:3
WORKDIR /usr/src/app
COPY . .
CMD ["test.py"]
ENTRYPOINT ["python3"]

3.Build the docker

docker build -t hello

4.run the docker

docker run -it hello test.py

3 Comments

Can you tailor this answer to be a little more specific to this question, and explain why to do the things you suggest? (Also this ENTRYPOINT/CMD split doesn't really make any sense, and I would put the entire command from the start of the question in CMD with no ENTRYPOINT at all.)
Hi, @DavidMaze, When running the command as RUN python3 app/main.py start --config config.yml. I was getting the error, which I mentioned in the question, but now when I am using ENTRYPOINT, it is running and I can see below logs from the container. /usr/src/app/smartinsights/system/DB.py:27: SyntaxWarning: "is" with a literal. Did you mean "=="? Running... [2021-04-14 10:46:29 +0000] [10] [INFO] Goin' Fast @ 127.0.0.1:9000 [2021-04-14 10:46:29 +0000] [10] [INFO] Starting worker [10].
Entrypoint is the wrong construct for this really. The entrypoint should be set in the parent image and be appropriate for the distro in the container. CMD is much more appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.