Docker Operations¶
Manager Docker containers, volumes and networks. These operations allow you to manage Docker from the view of the current inventory host. See the @docker Connector to use Docker containers as inventory directly.
Facts used in these operations: docker.DockerContainer, docker.DockerNetwork, docker.DockerVolume.
docker.container
¶
Manage Docker containers
docker.container(
container, image="", ports=None, networks=None, volumes=None, env_vars=None,
pull_always=False, present=True, force=False, start=True, **kwargs,
)
container: name to identify the container
image: container image and tag ex: nginx:alpine
networks: network list to attach on container
ports: port list to expose
volumes: volume list to map on container
env_vars: environment variable list to inject on container
pull_always: force image pull
force: remove a container with same name and create a new one
present: whether the container should be up and running
start: start or stop the container
Examples:
# Run a container
docker.container(
name="Deploy Nginx container",
container="nginx",
image="nginx:alpine",
ports=["80:80"],
present=True,
force=True,
networks=["proxy", "services"],
volumes=["nginx_data:/usr/share/nginx/html"],
pull_always=True,
)
# Stop a container
docker.container(
name="Stop Nginx container",
container="nginx",
start=False,
)
# Start a container
docker.container(
name="Start Nginx container",
container="nginx",
start=True,
)
- Note:
This operation also inherits all global arguments.
docker.image
¶
Stateless operation
This operation will always execute commands and is not idempotent.
Manage Docker images
docker.image(image, present=True, **kwargs)
image: Image and tag ex: nginx:alpine
present: whether the Docker image should exist
Examples:
# Pull a Docker image
docker.image(
name="Pull nginx image",
image="nginx:alpine",
present=True,
)
# Remove a Docker image
docker.image(
name="Remove nginx image",
image:"nginx:image",
present=False,
)
- Note:
This operation also inherits all global arguments.
docker.network
¶
Manage docker networks
docker.network(
network, driver="", gateway="", ip_range="", ipam_driver="", subnet="", scope="",
opts=None, ipam_opts=None, labels=None, ingress=False, attachable=False, present=True,
**kwargs,
)
network: Network name
driver: Network driver ex: bridge or overlay
gateway: IPv4 or IPv6 Gateway for the master subnet
ip_range: Allocate container ip from a sub-range
ipam_driver: IP Address Management Driver
subnet: Subnet in CIDR format that represents a network segment
scope: Control the network’s scope
opts: Set driver specific options
ipam_opts: Set IPAM driver specific options
labels: Label list to attach in the network
ingress: Create swarm routing-mesh network
attachable: Enable manual container attachment
present: whether the Docker network should exist
Examples:
# Create Docker network
docker.network(
network="nginx",
attachable=True,
present=True,
)
- Note:
This operation also inherits all global arguments.
docker.prune
¶
Stateless operation
This operation will always execute commands and is not idempotent.
Execute a docker system prune.
docker.prune(all=False, volumes=False, filter="", **kwargs)
all: Remove all unused images not just dangling ones
volumes: Prune anonymous volumes
filter: Provide filter values (e.g. “label=<key>=<value>” or “until=24h”)
Examples:
# Remove dangling images
docker.prune(
name="remove dangling images",
)
# Remove all images and volumes
docker.prune(
name="Remove all images and volumes",
all=True,
volumes=True,
)
# Remove images older than 90 days
docker.prune(
name="Remove unused older than 90 days",
filter="until=2160h"
)
- Note:
This operation also inherits all global arguments.
docker.volume
¶
Manage Docker volumes
docker.volume(volume, driver="", labels=None, present=True, **kwargs)
volume: Volume name
driver: Docker volume storage driver
labels: Label list to attach in the volume
present: whether the Docker volume should exist
Examples:
# Create a Docker volume
docker.volume(
name="Create nginx volume",
volume="nginx_data",
present=True
)
- Note:
This operation also inherits all global arguments.