Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Enter a Docker Container Ran in Detached Mode


    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!



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: 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.

    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.

    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.

    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.

    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.

    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.

    - 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.

    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.