Docker Operations

Manager Docker Containers, Volumes and Networks

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,
)
  • 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 varible list to inject on container
  • pull_always: force image pull
  • force: remove a contaner 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,
)

docker.image

Stateless operation

This operation will always execute commands and is not idempotent.

Manage Docker images

docker.image(image, present=True)
  • image: Image and tag ex: nginx:alpine
  • present: whether the Docker image should be 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,
)

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,
)
  • network_name: Image name
  • driver: Container image and tag ex: nginx:alpine
  • 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(
    name="Create nginx network",
    network_name="nginx",
    attachable=True,
    present=True,
)

docker.prune

Stateless operation

This operation will always execute commands and is not idempotent.

Execute a docker system prune.

docker.prune(all=False, volume=False, filter="")
  • 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"
)

docker.volume

Manage Docker volumes

docker.volume(volume, driver="", labels=None, present=True)
  • 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
)