Skip to content

Latest commit

 

History

History
112 lines (70 loc) · 2.14 KB

32_attach_container.md

File metadata and controls

112 lines (70 loc) · 2.14 KB

Attach to a Running Container

Docker Documentation References:

docker exec

docker run

docker stop

docker rm

Intent

The intent of this exercise it to familiarize yourself with the exec command, specifically so you can connect to a running container with a shell.

Overview

In this exercise we will create a container running detached. Then we will use exec to start a shell on that container and run some commands. We will then exit the shell, disconnecting us from the container. Finally, we will stop the container and remove it.

Kata Steps

Create a container

Command

docker run -d nginx:alpine

Output

/ # docker run -d nginx:alpine
381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917

Attach to the Container uisng sh

Command

docker exec -it 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917 sh

Output

/ # docker exec -it 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917 sh

Run some commands inside the container

Command

hostname

Output

/ # hostname
381c2c925259

Disconect from the contianer

Command

exit

Output

/ # exit

Stop the Container

Command

docker stop 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917

Output

/ # docker stop 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917

Delete the container

Command

docker rm 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917

Output

/ # docker rm 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917

Previous | Index | Next