И ещё одна интересная выжимка фактов о докере, которая поможет в кратчайшие сроки начать его продуктивное использование. Цель данной статьи…
When using Ansible’sdocker_container
to manage containers, we’ve seen people use wait_for
ensure the container is started.
This requires that the container exposes a port and typically on the host network. Then the IP and the exposed/published port can be used to verify the container (or the service inside) is running.
What if there is no port, or no host binding?
If there is nothing to expose from the container (e.g. think of a queue worker), the following can be done:
— | |
— name: Start a service without a port | |
docker_container: | |
name: service_without_port | |
image: whatever | |
… | |
register: my_service_status | |
— name: Save status | |
set_fact: | |
container_is_running: «{{ my_service_status.ansible_facts.docker_container.State.Running }}« | |
— fail: | |
msg: «The service is not running« | |
when: not container_is_running |
If however you expose a port, but not on the host, then the following will help to grab the IP of the container and to be able to verify operation:
— | |
# assume this is preceeded by the docker_container module above | |
— name: Get container IP | |
set_fact: | |
container_ip: «{{ my_service_status.ansible_facts.docker_container.NetworkSettings.IPAddress }}« | |
— name: Check container | |
wait_for: | |
host: «{{ container_ip }}« | |
port: 31337 |