Docker Documentation References:
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.
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.
Command
docker run -d nginx:alpine
Output
/ # docker run -d nginx:alpine
381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
Command
docker exec -it 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917 sh
Output
/ # docker exec -it 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917 sh
Command
hostname
Output
/ # hostname
381c2c925259
Command
exit
Output
/ # exit
Command
docker stop 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
Output
/ # docker stop 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
Command
docker rm 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
Output
/ # docker rm 381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917
381c2c925259a8774f78e77f23e4f477b1206eb3bf847c306b21666b4e92f917