Debian Codegraph @

ansible_collections/community/docker/docs/docsite/rst/scenario_guide.rst

raw
1 ..
2 Copyright (c) Ansible Project
3 GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 SPDX-License-Identifier: GPL-3.0-or-later
5
6 .. _ansible_collections.community.docker.docsite.scenario_guide:
7
8 Docker Guide
9 ============
10
11 The `community.docker collection <https://galaxy.ansible.com/ui/repo/published/community/docker/>`_ offers several modules and plugins for orchestrating Docker containers and Docker Swarm.
12
13 .. contents::
14 :local:
15 :depth: 1
16
17
18 Requirements
19 ------------
20
21 Most of the modules and plugins in community.docker require the `Docker SDK for Python <https://docker-py.readthedocs.io/en/stable/>`_. The SDK needs to be installed on the machines where the modules and plugins are executed, and for the Python version(s) with which the modules and plugins are executed. You can use the :ansplugin:`community.general.python_requirements_info module <community.general.python_requirements_info#module>` to make sure that the Docker SDK for Python is installed on the correct machine and for the Python version used by Ansible.
22
23 Note that plugins (inventory plugins and connection plugins) are always executed in the context of Ansible itself. If you use a plugin that requires the Docker SDK for Python, you need to install it on the machine running ``ansible`` or ``ansible-playbook`` and for the same Python interpreter used by Ansible. To see which Python is used, run ``ansible --version``.
24
25 You can install the Docker SDK for Python for Python 3.6 or later as follows:
26
27 .. code-block:: console
28
29 $ pip install docker
30
31 For Python 2.7, you need to use a version between 2.0.0 and 4.4.4 since the Python package for Docker removed support for Python 2.7 on 5.0.0. You can install the specific version of the Docker SDK for Python as follows:
32
33 .. code-block:: console
34
35 $ pip install 'docker==4.4.4'
36
37 Note that the Docker SDK for Python was called ``docker-py`` on PyPi before version 2.0.0. Please avoid installing this really old version, and make sure to not install both ``docker`` and ``docker-py``. Installing both will result in a broken installation. If this happens, Ansible will detect it and inform you about it. If that happens, you must uninstall both and reinstall the correct version. If in doubt, always install ``docker`` and never ``docker-py``.
38
39
40 Connecting to the Docker API
41 ----------------------------
42
43 You can connect to a local or remote API using parameters passed to each task or by setting environment variables. The order of precedence is command line parameters and then environment variables. If neither a command line option nor an environment variable is found, Ansible uses the default value provided under `Parameters`_.
44
45
46 Parameters
47 ..........
48
49 Most plugins and modules can be configured by the following parameters:
50
51 docker_host
52 The URL or Unix socket path used to connect to the Docker API. Defaults to ``unix:///var/run/docker.sock``. To connect to a remote host, provide the TCP connection string (for example: ``tcp://192.0.2.23:2376``). If TLS is used to encrypt the connection to the API, then the module will automatically replace ``tcp`` in the connection URL with ``https``.
53
54 api_version
55 The version of the Docker API running on the Docker Host. Defaults to the latest version of the API supported by the Docker SDK for Python installed.
56
57 timeout
58 The maximum amount of time in seconds to wait on a response from the API. Defaults to 60 seconds.
59
60 tls
61 Secure the connection to the API by using TLS without verifying the authenticity of the Docker host server. Defaults to ``false``.
62
63 validate_certs
64 Secure the connection to the API by using TLS and verifying the authenticity of the Docker host server. Default is ``false``.
65
66 ca_path
67 Use a CA certificate when performing server verification by providing the path to a CA certificate file.
68
69 cert_path
70 Path to the client's TLS certificate file.
71
72 key_path
73 Path to the client's TLS key file.
74
75 tls_hostname
76 When verifying the authenticity of the Docker Host server, provide the expected name of the server. Defaults to ``localhost``.
77
78 ssl_version
79 Provide a valid SSL version number. The default value is determined by the Docker SDK for Python.
80
81 This option is not available for the CLI based plugins. It is mainly needed for legacy systems and should be avoided.
82
83
84 Module default group
85 ....................
86
87 To avoid having to specify common parameters for all the modules in every task, you can use the ``community.docker.docker`` :ref:`module defaults group <module_defaults_groups>`, or its short name ``docker``.
88
89 .. note::
90
91 Module default groups only work for modules, not for plugins (connection and inventory plugins).
92
93 The following example shows how the module default group can be used in a playbook:
94
95 .. code-block:: yaml+jinja
96
97 ---
98 - name: Pull image and start the container
99 hosts: localhost
100 gather_facts: false
101 module_defaults:
102 group/community.docker.docker:
103 # Select Docker Daemon on other host
104 docker_host: tcp://192.0.2.23:2376
105 # Configure TLS
106 tls: true
107 validate_certs: true
108 tls_hostname: docker.example.com
109 ca_path: /path/to/cacert.pem
110 # Increase timeout
111 timeout: 120
112 tasks:
113 - name: Pull image
114 community.docker.docker_image_pull:
115 name: python
116 tag: 3.12
117
118 - name: Start container
119 community.docker.docker_container:
120 cleanup: true
121 command: python --version
122 detach: false
123 image: python:3.12
124 name: my-python-container
125 output_logs: true
126
127 - name: Show output
128 ansible.builtin.debug:
129 msg: "{{ output.container.Output }}"
130
131 Here the two ``community.docker`` tasks will use the options set for the module defaults group.
132
133
134 Environment variables
135 .....................
136
137 You can also control how the plugins and modules connect to the Docker API by setting the following environment variables.
138
139 For plugins, they have to be set for the environment Ansible itself runs in. For modules, they have to be set for the environment the modules are executed in. For modules running on remote machines, the environment variables have to be set on that machine for the user used to execute the modules with.
140
141 .. envvar:: DOCKER_HOST
142
143 The URL or Unix socket path used to connect to the Docker API.
144
145 .. envvar:: DOCKER_API_VERSION
146
147 The version of the Docker API running on the Docker Host. Defaults to the latest version of the API supported
148 by Docker SDK for Python.
149
150 .. envvar:: DOCKER_TIMEOUT
151
152 The maximum amount of time in seconds to wait on a response from the API.
153
154 .. envvar:: DOCKER_CERT_PATH
155
156 Path to the directory containing the client certificate, client key and CA certificate.
157
158 .. envvar:: DOCKER_SSL_VERSION
159
160 Provide a valid SSL version number.
161
162 .. envvar:: DOCKER_TLS
163
164 Secure the connection to the API by using TLS without verifying the authenticity of the Docker Host.
165
166 .. envvar:: DOCKER_TLS_HOSTNAME
167
168 When verifying the authenticity of the Docker Host, uses this hostname to compare to the host's certificate.
169
170 .. envvar:: DOCKER_TLS_VERIFY
171
172 Secure the connection to the API by using TLS and verify the authenticity of the Docker Host.
173
174
175 Plain Docker daemon: images, networks, volumes, and containers
176 --------------------------------------------------------------
177
178 For working with a plain Docker daemon, that is without Swarm, there are connection plugins, an inventory plugin, and several modules available:
179
180 docker connection plugin
181 The :ansplugin:`community.docker.docker connection plugin <community.docker.docker#connection>` uses the Docker CLI utility to connect to Docker containers and execute modules in them. It essentially wraps ``docker exec`` and ``docker cp``. This connection plugin is supported by the :ansplugin:`ansible.posix.synchronize module <ansible.posix.synchronize#module>`.
182
183 docker_api connection plugin
184 The :ansplugin:`community.docker.docker_api connection plugin <community.docker.docker_api#connection>` talks directly to the Docker daemon to connect to Docker containers and execute modules in them.
185
186 docker_containers inventory plugin
187 The :ansplugin:`community.docker.docker_containers inventory plugin <community.docker.docker_containers#inventory>` allows you to dynamically add Docker containers from a Docker Daemon to your Ansible inventory. See :ref:`dynamic_inventory` for details on dynamic inventories.
188
189 The `docker inventory script <https://github.com/ansible-community/contrib-scripts/blob/main/inventory/docker.py>`_ is deprecated. Please use the inventory plugin instead. The inventory plugin has several compatibility options. If you need to collect Docker containers from multiple Docker daemons, you need to add every Docker daemon as an individual inventory source.
190
191 docker_host_info module
192 The :ansplugin:`community.docker.docker_host_info module <community.docker.docker_host_info#module>` allows you to retrieve information on a Docker daemon, such as all containers, images, volumes, networks and so on.
193
194 docker_login module
195 The :ansplugin:`community.docker.docker_login module <community.docker.docker_login#module>` allows you to log in and out of a remote registry, such as Docker Hub or a private registry. It provides similar functionality to the ``docker login`` and ``docker logout`` CLI commands.
196
197 docker_prune module
198 The :ansplugin:`community.docker.docker_prune module <community.docker.docker_prune#module>` allows you to prune no longer needed containers, images, volumes and so on. It provides similar functionality to the ``docker prune`` CLI command.
199
200 docker_image module
201 The :ansplugin:`community.docker.docker_image module <community.docker.docker_image#module>` provides full control over images, including: build, pull, push, tag and remove.
202
203 docker_image_build
204 The :ansplugin:`community.docker.docker_image_build module <community.docker.docker_image_build#module>` allows you to build a Docker image using Docker buildx.
205
206 docker_image_export module
207 The :ansplugin:`community.docker.docker_image_export module <community.docker.docker_image_export#module>` allows you to export (archive) images.
208
209 docker_image_info module
210 The :ansplugin:`community.docker.docker_image_info module <community.docker.docker_image_info#module>` allows you to list and inspect images.
211
212 docker_image_load
213 The :ansplugin:`community.docker.docker_image_load module <community.docker.docker_image_load#module>` allows you to import one or multiple images from tarballs.
214
215 docker_image_pull
216 The :ansplugin:`community.docker.docker_image_pull module <community.docker.docker_image_pull#module>` allows you to pull a Docker image from a registry.
217
218 docker_image_push
219 The :ansplugin:`community.docker.docker_image_push module <community.docker.docker_image_push#module>` allows you to push a Docker image to a registry.
220
221 docker_image_remove
222 The :ansplugin:`community.docker.docker_image_remove module <community.docker.docker_image_remove#module>` allows you to remove and/or untag a Docker image from the Docker daemon.
223
224 docker_image_tag
225 The :ansplugin:`community.docker.docker_image_tag module <community.docker.docker_image_tag#module>` allows you to tag a Docker image with additional names and/or tags.
226
227 docker_network module
228 The :ansplugin:`community.docker.docker_network module <community.docker.docker_network#module>` provides full control over Docker networks.
229
230 docker_network_info module
231 The :ansplugin:`community.docker.docker_network_info module <community.docker.docker_network_info#module>` allows you to inspect Docker networks.
232
233 docker_volume_info module
234 The :ansplugin:`community.docker.docker_volume_info module <community.docker.docker_volume_info#module>` provides full control over Docker volumes.
235
236 docker_volume module
237 The :ansplugin:`community.docker.docker_volume module <community.docker.docker_volume#module>` allows you to inspect Docker volumes.
238
239 docker_container module
240 The :ansplugin:`community.docker.docker_container module <community.docker.docker_container#module>` manages the container lifecycle by providing the ability to create, update, stop, start and destroy a Docker container.
241
242 docker_container_copy_into
243 The :ansplugin:`community.docker.docker_container_copy_into module <community.docker.docker_container_copy_into#module>` allows you to copy files from the control node into a container.
244
245 docker_container_exec
246 The :ansplugin:`community.docker.docker_container_exec module <community.docker.docker_container_exec#module>` allows you to execute commands in a running container.
247
248 docker_container_info module
249 The :ansplugin:`community.docker.docker_container_info module <community.docker.docker_container_info#module>` allows you to inspect a Docker container.
250
251 docker_plugin
252 The :ansplugin:`community.docker.docker_plugin module <community.docker.docker_plugin#module>` allows you to manage Docker plugins.
253
254
255 Docker Compose
256 --------------
257
258 Docker Compose v2
259 .................
260
261 There are several modules for working with Docker Compose projects:
262
263 community.docker.docker_compose_v2
264 The :ansplugin:`community.docker.docker_compose_v2 module <community.docker.docker_compose_v2#module>` allows you to use your existing Docker Compose files to orchestrate containers on a single Docker daemon or on Swarm.
265
266 community.docker.docker_compose_v2_exec
267 The :ansplugin:`community.docker.docker_compose_v2_exec module <community.docker.docker_compose_v2_exec#module>` allows you to run a command in a container of Docker Compose projects.
268
269 community.docker.docker_compose_v2_pull
270 The :ansplugin:`community.docker.docker_compose_v2_pull module <community.docker.docker_compose_v2_pull#module>` allows you to pull Docker Compose projects.
271
272 community.docker.docker_compose_v2_run
273 The :ansplugin:`community.docker.docker_compose_v2_run module <community.docker.docker_compose_v2_run#module>` allows you to run a command in a new container of a Docker Compose project.
274
275 These modules use the Docker CLI "compose" plugin (``docker compose``), and thus needs access to the Docker CLI tool.
276 No further requirements next to to the CLI tool and its Docker Compose plugin are needed.
277
278
279 Docker Machine
280 --------------
281
282 The :ansplugin:`community.docker.docker_machine inventory plugin <community.docker.docker_machine#inventory>` allows you to dynamically add Docker Machine hosts to your Ansible inventory.
283
284
285 Docker Swarm stack
286 ------------------
287
288 The :ansplugin:`community.docker.docker_stack module <community.docker.docker_stack#module>` module allows you to control Docker Swarm stacks. Information on Swarm stacks can be retrieved by the :ansplugin:`community.docker.docker_stack_info module <community.docker.docker_stack_info#module>`, and information on Swarm stack tasks can be retrieved by the :ansplugin:`community.docker.docker_stack_task_info module <community.docker.docker_stack_task_info#module>`.
289
290
291 Docker Swarm
292 ------------
293
294 The community.docker collection provides multiple plugins and modules for managing Docker Swarms.
295
296 Swarm management
297 ................
298
299 One inventory plugin and several modules are provided to manage Docker Swarms:
300
301 docker_swarm inventory plugin
302 The :ansplugin:`community.docker.docker_swarm inventory plugin <community.docker.docker_swarm#inventory>` allows you to dynamically add all Docker Swarm nodes to your Ansible inventory.
303
304 docker_swarm module
305 The :ansplugin:`community.docker.docker_swarm module <community.docker.docker_swarm#module>` allows you to globally configure Docker Swarm manager nodes to join and leave swarms, and to change the Docker Swarm configuration.
306
307 docker_swarm_info module
308 The :ansplugin:`community.docker.docker_swarm_info module <community.docker.docker_swarm_info#module>` allows you to retrieve information on Docker Swarm.
309
310 docker_node module
311 The :ansplugin:`community.docker.docker_node module <community.docker.docker_node#module>` allows you to manage Docker Swarm nodes.
312
313 docker_node_info module
314 The :ansplugin:`community.docker.docker_node_info module <community.docker.docker_node_info#module>` allows you to retrieve information on Docker Swarm nodes.
315
316 Configuration management
317 ........................
318
319 The community.docker collection offers modules to manage Docker Swarm configurations and secrets:
320
321 docker_config module
322 The :ansplugin:`community.docker.docker_config module <community.docker.docker_config#module>` allows you to create and modify Docker Swarm configs.
323
324 docker_secret module
325 The :ansplugin:`community.docker.docker_secret module <community.docker.docker_secret#module>` allows you to create and modify Docker Swarm secrets.
326
327 Swarm services
328 ..............
329
330 Docker Swarm services can be created and updated with the :ansplugin:`community.docker.docker_swarm_service module <community.docker.docker_swarm_service#module>`, and information on them can be queried by the :ansplugin:`community.docker.docker_swarm_service_info module <community.docker.docker_swarm_service_info#module>`.