Docker container lifecycle management¶
Docker container lifecycle management encompasses the operational procedures used to create, monitor, and remove containers. As the fundamental unit of Docker, a container is a runnable instance of an image.^[600-developer__docker__docker.md]
Creation and Execution¶
Containers are created from images using the docker run command.^[600-developer__docker__docker.md] To run a process in the background, the -d (detached) flag is used, while the -p flag maps the container's ports to the host machine.^[600-developer__docker__docker.md] A container must be created from a specific image ID or repository tag.^[600-developer__docker__docker.md]
Monitoring and Inspection¶
Operators can interact with running containers to view logs or inspect processes:
* View logs: The docker logs command displays the standard output of a container, with flags like -t (timestamps), -f (follow), and --tail (limit lines) available for filtering.^[600-developer__docker__docker.md]
* List processes: docker top displays the running processes within a specific container.^[600-developer__docker__docker.md]
* Inspect metadata: docker inspect returns detailed low-level information about the container's configuration and state.^[600-developer__docker__docker.md]
Interaction and Execution¶
Administrators can connect to or execute commands inside a running container:
* Attach: docker attach connects the terminal's standard input, output, and error streams to the container, allowing direct interaction.^[600-developer__docker__docker.md]
* Execute commands: docker exec allows running specific commands inside a container, such as opening a bash shell with docker exec -t <container ID> /bin/bash.^[600-developer__docker__docker.md]
Termination and Removal¶
The lifecycle ends with stopping and removing the container.
* Stop: Containers can be gracefully stopped using docker container stop, allowing the process to exit cleanly.^[600-developer__docker__docker.md]
* Kill: For immediate termination, docker container kill forces a shutdown of the specified container.^[600-developer__docker__docker.md]
* Remove: docker container rm deletes the container.^[600-developer__docker__docker.md] Bulk removal is possible by combining commands, such as docker rm $(docker ps -a -f status=exited -q), which removes all containers with an "exited" status.^[600-developer__docker__docker.md]
Sources¶
^[600-developer__docker__docker.md]