Docker Community Forums

Share and learn in the Docker community.

Run a command in running application in docker from shell


I’m trying to forward a simple command to an application that’s running in a docker container.
I can run the command successfully using docker attach container and then entering the command that I need to run. The problem is I want to do it automatically using a shell script.
I can’t figure out how to run the command using docker exec
All articles that popped up while looking for solutions were showing how to run a program or script inside the container. I’m trying to pass that command to a java application that’s running in the container.

Is it somehow possible to run a command using docker exec in an already running/active TTY?
What am I missing?

Any help would be appreciated.

Just out of curriousity: how would you archive what your try to do without the container context?
You can use docker exec with a terminal, pipe in content using STDIN and pipe out content using STDOUT. Basicly what you can do in the local terminal as well. Every docker exec call will execute an independent process in a running container.

Your container will exit as the command you gave it will end. Use the following options to keep it live:

-i Keep STDIN open even if not attached.
-t Allocate a pseudo-TTY.
So your new run command is:

docker run -it -d shykes/pybuilder bin/bash
If you would like to attach to an already running container:

docker exec -it CONTAINER_ID /bin/bash
In these examples /bin/bash is used as the command.