Let try to pull the busybox image, run the container by executing shell command in one shot:
docker run -it busybox shThis docker run format can be broken down into two parts:
- docker run [options]
- [image] [command]
- options
-itmeans we want to keep STDIN open even if not attached and allocate a pseudo-TTY - image we are using container busybox image
- command running the sh command
To verify the container is running, type the following command from the new terminal:
docker psAnd you will see a similar response like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30b97ee57158 busybox "sh" 7 seconds ago Up 6 seconds charming_jones
Go back to the previous terminal and exit shell environment by running:
exitNow verify the container again, you will see no container is currently running now:
docker psInstead, the container still exists with the Exited status; you can verify it by running:
docker ps -aIt will show a similar result:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30b97ee57158 busybox "sh" 12 minutes ago Exited (0) 9 minutes ago charming_jones
You can remove container by typing:
docker rm 30b97ee57158If you want container automatically remove itself after the container in exited status. You can specify --rm options when executing the docker run command:
docker run -it --rm busybox shAlso, instead of creating a container for executing the program, we can use docker exec to running the program in the current container:
# Check container id from existing running containers
docker ps
# Return the result similar as following
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a843b66bebbe busybox "sh" 4 seconds ago Up 4 seconds pensive_carson
# Execute the program using the current container
docker exec -it a843b66bebbe shNext: Dockerfile