Docker container operations¶
Docker container operations are the administrative tasks performed on the lifecycle of a Docker container, including creation, execution, interaction, network configuration, and resource management^[400-devops-06-kubernetes-k8s-paas-01docker.md].
Basic Operations¶
To manage the container lifecycle, Docker provides specific commands for starting, listing, and removing containers^[400-devops-06-kubernetes-k8s-paas-01docker.md].
- Starting Containers: The
docker runcommand creates a new container and runs it^[400-devops-06-kubernetes-k8s-paas-01docker.md]. Options include--nameto assign a specific name and-dto run the container in the background (detached mode)^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Listing Containers:
docker pslists running containers, whiledocker ps -alists all containers, including those that have exited^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Removing Containers:
docker rmremoves a container. The-fflag can be used to force removal^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Logging:
docker logs -f <容器id>tracks log output for a specific container^[400-devops-06-kubernetes-k8s-paas-01docker.md].
Configuration and Mapping¶
Operations often involve configuring how the container interacts with the host system and external networks^[400-devops-06-kubernetes-k8s-paas-01docker.md].
- Port Mapping: The
-pflag maps a host port to a container port (e.g.,-p 81:80maps host port 81 to container port 80)^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Data Volumes: The
-vflag mounts a host directory or file into the container, allowing data persistence and sharing^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Environment Variables: The
-eflag passes environment variables into the container at runtime^[400-devops-06-kubernetes-k8s-paas-01docker.md].
Interaction and Maintenance¶
Operators frequently need to inspect or modify running containers^[400-devops-06-kubernetes-k8s-paas-01docker.md].
- Inspection:
docker inspect <容器id>returns detailed low-level information about a container^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Interactive Access:
docker exec -ti <容器id> /bin/bashopens an interactive terminal inside the running container, allowing for command-line operations within the container environment^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Software Installation: Inside a container, standard package managers like
yumorapt-getcan be used to install necessary software tools^[400-devops-06-kubernetes-k8s-paas-01docker.md]. - Committing Changes:
docker commitcreates a new image from a container's changes, allowing the state of a modified container to be saved as a new image^[400-devops-06-kubernetes-k8s-paas-01docker.md].
Networking¶
Docker supports several network modes to control how containers communicate with each other and the outside world^[400-devops-06-kubernetes-k8s-paas-01docker.md].
- Bridge (NAT): The default mode; the container receives an IP address from a private subnet inside the host^[400-devops-06-kubernetes-k8s-paas-01docker.md].
- Host: The container shares the host's network namespace, removing network isolation between the container and the host^[400-devops-06-kubernetes-k8s-paas-01docker.md].
- None: Networking is disabled for the container^[400-devops-06-kubernetes-k8s-paas-01docker.md].
- Container: The container shares the network namespace with another specified container^[400-devops-06-kubernetes-k8s-paas-01docker.md].
Related Concepts¶
- [[Docker镜像管理实战]]
- Dockerfile
Sources¶
^[400-devops-06-kubernetes-k8s-paas-01docker.md]