0

I am working on a django docker image and I want to execute a function ussing command line, I know that I can enter to shell python docker container and run a function like this :

  • docker container ls to find the container id and use in the next command
  • docker container exec -it <container_id> python3 manage.py shell Then:
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from xx import foo 
>>> foo()  # foo() has been excecuted

However, i was wondering if i can use something similar like python flag -c which help to run small piece of python code as command line like this:

  • python3 -c "from xx import foo; foo()"

I tried to insert a cmd with linux tubes : echo 'from xx import foo; foo()' | docker container exec -it <container_id> python3 manage.py shell, However it doesn't work.

I'd appreciate any advice, thanks for your time.

1

1 Answer 1

3

You can do it like this:

docker exec -it <container id> python3 manage.py shell --command="import foo;foo()"

More information can be found in documentation.

Sign up to request clarification or add additional context in comments.

Comments

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.