Skip to content

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 run command creates a new container and runs it^[400-devops-06-kubernetes-k8s-paas-01docker.md]. Options include --name to assign a specific name and -d to run the container in the background (detached mode)^[400-devops-06-kubernetes-k8s-paas-01docker.md].
  • Listing Containers: docker ps lists running containers, while docker ps -a lists all containers, including those that have exited^[400-devops-06-kubernetes-k8s-paas-01docker.md].
  • Removing Containers: docker rm removes a container. The -f flag 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 -p flag maps a host port to a container port (e.g., -p 81:80 maps host port 81 to container port 80)^[400-devops-06-kubernetes-k8s-paas-01docker.md].
  • Data Volumes: The -v flag 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 -e flag 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/bash opens 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 yum or apt-get can be used to install necessary software tools^[400-devops-06-kubernetes-k8s-paas-01docker.md].
  • Committing Changes: docker commit creates 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].

Sources

^[400-devops-06-kubernetes-k8s-paas-01docker.md]