Docker Run
docker run alpine printenv
This command tells Docker to create a container using the alpine
image and run the printenv
command (which is included in the alpine image).
Since the alpine image wasn’t on my computer, Docker automatically downloaded it to create the container.
Now, let’s run two more containers with the same command to display their hostnames:
docker run alpine printenv
docker run alpine printenv
We’re creating two more containers using the alpine
image and running the printenv
command. Since the image is already on my machine, it isn’t downloaded again. These commands create two separate containers.
Remove all Containers
If I list all containers, including stopped ones, with docker ps -a
, I’ll see three stopped containers (one from the first command and two from the second). To avoid removing each container manually, I can use this command:
docker container prune -f
This removes all stopped containers at once. The -f
flag tells Docker to proceed without asking for confirmation.
To remove all running containers, use the following command:
docker rm $(docker ps -aq)