Enter a Docker Container Ran in Detached Mode

Jacob Carter
InstructorJacob Carter
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

When you run a docker container in detached mode you may want to re-enter that container later. With docker exec you can do that. This short video will show you how!

Instructor: [00:01] First, we're going to run a Docker container in a detached state. To do that, we'll just type docker run -d, and then the image ID of the Docker image that we want to spin a container off of. Now that we have that container running, we can grab the container ID.

[00:17] Using that container ID, we can reenter that container, so that we can operate from within. To do that, we'll type the command docker exec -it, then the container ID, and then the command that we want to enter into that container with. By typing bash, we will start a Bash session within that container.

[00:37] Now that we're in the container, we can use commands inside of it. For example, I'll list out all the files that are inside this Docker container. These files just represent a mock Node application.

[00:48] When we want to exit out of that Docker container, we simply press control-P-Q, and we will exit that Docker container. That control is going to be control whether you're on a Windows, a Linux, or a Mac. On a Mac, you do not hit the command key. It is always control-P-Q.

[01:06] Now, let's walk through each part of this command. The exec command is what you use to run a command against a Docker container. Specifying a container ID lets Docker know what container you want to run the command against.

[01:21] The command that we want to run against it is bash. That says that we want to start a Bash session of that container. The two flags are really important to give us the terminal-like behavior that we see when we run docker exec -it.

-[01:36] i says that we want access to the SD in stream, which means when I run a command against Bash, I want to see what's returned as a result. The gives us a pseudo TTY. What that basically means is that it's going to give you the driver that you need in order to have terminal-like behavior inside that Docker container.

[01:56] docker exec -it, container ID, bash is a combination of commands and options that give us a terminal-like interface that we can interact with our Docker containers against.

egghead
egghead
~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today