Connecting to a Container

Learn how to execute commands in running Docker containers using the docker exec command.

Execute commands in running containers

The docker exec command is used to execute commands in running containers, and it has two modes:

  • Interactive
  • Remote execution

Interactive exec sessions connect your terminal to a shell process in the container and behave like remote SSH sessions. Remote execution mode lets you send commands to a running container and prints the output to your local terminal.

Run the following command to start an interactive exec session by creating a new shell process (sh) inside the webserver container and connecting our terminal to it. The -it flag makes it an interactive exec session, and the sh argument starts a new sh process inside the container. sh is a minimal shell program installed in the container.

Get hands-on with 1400+ tech skills courses.