Exploring Docker Container Execution
The Docker Container Lifecycle
The lifecycle of a Docker container consists of several stages, including creation, running, pausing, stopping, and removal. You can manage these stages using various Docker commands.
graph LR
A[Create] --> B[Run]
B --> C[Pause]
B --> D[Stop]
D --> E[Remove]
Running a Docker Container
To run a Docker container, you can use the docker run
command. This command allows you to specify the Docker image to use, as well as various options to configure the container's behavior.
## Run a Ubuntu container
docker run -it ubuntu:latest /bin/bash
Inspecting Docker Containers
You can use the docker inspect
command to get detailed information about a running container, including its configuration, network settings, and resource usage.
## Inspect a running container
docker inspect <container_id>
Monitoring Docker Containers
Docker provides several commands to monitor the status and performance of running containers, such as docker stats
and docker logs
.
## Monitor container resource usage
docker stats <container_id>
## View container logs
docker logs <container_id>
Networking in Docker Containers
Docker containers can be connected to various network drivers, such as the default bridge
network or user-defined networks. You can use the docker network
command to manage these networks.
## Create a custom network
docker network create my-network
## Connect a container to a network
docker run -it --network my-network ubuntu:latest /bin/bash
Persisting Data in Docker Containers
Docker provides several options for persisting data in containers, such as volumes and bind mounts. These allow you to store data outside the container's file system, ensuring that it persists even if the container is stopped or removed.
## Create a volume and mount it to a container
docker volume create my-volume
docker run -it -v my-volume:/data ubuntu:latest /bin/bash