[Docker] How to Exec into a Running Docker Container

Sometimes, we need to run another process inside an already-running container for debugging purposes.

To interact with a running Docker container, follow these steps:

Step 1 : Open a terminal and execute the following command to enter the container interactively:

Explanation of the command:

  • -i: Starts an interactive session.
  • -t: Allocates a pseudo-TTY.
  • /bash: Specifies the shell to use inside the container.

Step 3: Once inside the container, verify your environment by running:

Step 4: To exit the interactive session, use:

Press Ctrl + D to exit.

Step 5: Alternatively, execute commands directly without entering the shell:

Step 6: For interactive mode with environment variable setting, use:

Step 7: Inside the container, verify the environment variable:

Step 8: Exit the interactive session again with:

Press Ctrl + D to exit.

These steps allow you to effectively manage and interact with Docker containers from your host system