Docker exec Command
docker exec: Execute a command in a running container
Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
OPTIONS Description:
-
-d: Detached mode: Run in the background
-
-i: Keep STDIN open even if not attached
-
-t: Allocate a pseudo-TTY
Example
Execute the script /root/tutorialpro.sh
inside the container mynginx
in interactive mode:
tutorialpro@tutorialpro:~$ docker exec -it mynginx /bin/sh /root/tutorialpro.sh
http://www.tutorialpro.org/
Open an interactive terminal in the container mynginx
:
tutorialpro@tutorialpro:~$ docker exec -i -t mynginx /bin/bash
root@b1a0703e41e7:/#
You can also view the running containers using the docker ps -a
command and then enter the container using the container ID.
View the running container IDs:
# docker ps -a
...
9df70f9a0714 openjdk "/usercode/script.sh…"
...
The first column, 9df70f9a0714
, is the container ID.
Execute bash in the specified container using the exec command:
# docker exec -it 9df70f9a0714 /bin/bash