Docker exec Command

Docker exec Command

docker exec : Execute commands in a running container

Docker exec Syntax

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

OPTIONS Description:

  • -d :Separate mode: running in the background
  • -i : Keep STDIN open even if not attached
  • -t : assign a pseudo terminal

Docker exec Examples

Execute the container /root/apidemos.sh script in interactive mode in the container mynginx:

apidemos@apidemos:~$ docker exec -it mynginx /bin/sh /root/apidemos.sh
http://wwws.apidemos.com/

Open a terminal in interactive mode in the container mynginx:

apidemos@apidemos:~$ docker exec -i -t  mynginx /bin/bash
root@b1a0703e41e7:/#

You can also view the containers that are already running with the docker ps -a command, and then use the container ID to access the container.

To view the already running container IDs:

# docker ps -a 
...
9df70f9a0714        openjdk             "/usercode/script.sh…" 
...

The first column of 9df70f9a0714 is the container ID.

The exec command executes bash:

# docker exec -it 9df70f9a0714 /bin/bash
Like(1)