When you run a docker image and a container is created, when the container exists docker will not remove the container that existed by default, let's see this by creating multiple containers from fedora image,
Container 1:% docker run fedora echo hello1
hello1
Container 2:
% docker run fedora echo hello2
hello2
Container 3:
% docker run fedora echo hello3
hello3
Now, if I do a docker ps -a to see the list of all containers, I see that there are 3 containers that exited but have not been removed and utilizing memory.
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9072f3eff69 fedora "echo hello3" 9 seconds ago Exited (0) 9 seconds ago thirsty_almeida
21abcc335be3 fedora "echo hello2" 11 seconds ago Exited (0) 11 seconds ago interesting_turing
0c0ba20b0018 fedora "echo hello1" 57 seconds ago Exited (0) 56 seconds ago cool_robinson
If you wish to remove the created container when the docker run command completes, you should add --rm option to the docker run command.
Example: Docker run with --rm option:% docker run --rm fedora echo hello4
hello4
![Docker remove container when run completes example Docker remove container when run completes example](https://code2care.org/docker/remove-docker-container-when-it-exits/images/Docker remove container when run completes example.jpg)
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Install Docker on Mac using brew cask
- How to know the Docker Sandbox ID of a Container Network?
- How to Rename Docker Image with none TAG and REPOSITORY?
- How to know list of images available on your device
- Docker Alpine Linux and Apache2 Example
- Install Bash on Alpine Linux - Docker
- Docker Run Command Examples - Part 1
- Install the minimal Linux on Docker (only 5 mb Alpine Linux)
- [fix] docker: Error response from daemon: dial unix docker.raw.sock: connect: no such file or directory.
- Install RabbitMQ on Docker
- How to know docker Engine details
- [Fix] Docker Error response from daemon: manifest for :latest not found: manifest unknown
- How to stop and start a docker container
- How to create volume in Docker using Command
- How to know the Docker Engine Version
- [docker] Error response from daemon: No such container
- Install Docker for Mac using Home-brew Cask
- Docker - Incompatible CPU detected - M1/M2 Mac (macOS Sonoma)
- [fix] Docker Desktop App not starting on Mac (macOS)
- Unable to find image docker latest locally
- How to Stop/Cancel/kill docker image pull
- List of what's new in Docker 4.23
- [Docker M1/M2 Mac] qemu-x86_64: Could not open /lib64/ld-linux-x86-64.so.2: No such file or directory AWS CLI
- Install Docker Desktop on M1/M2 Apple Silicon ARM Chip Mac
- Docker - Running in Resource Saver mode
- Spring Boot: NamedParameterJdbcTemplate batch insert example - Java
- Ignore white space while Notepad++ compare two files - NotepadPlusPlus
- How to install tabnine on VS Code - HowTos
- Permanently set $PATH variable in Mac Zsh shell - MacOS
- Notepad++ Export file as HTML - NotepadPlusPlus
- Windows 10 now has a new enhanced Calculator with a new icon - News
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error - Java
- [Solution] WslRegisterDistribution failed with error: 0x80370102 VM on Mac - Windows