I'm using Ansible and its module docker_container
to launch tape unit test containers in nodejs. This is nice because I don't have to have npm mess up my host, my only dev box dependency is python and docker.
I need to be able to see stdout
to see that tests have been run. However, Docker's --attach
option is not exposed in docker_container
and I cannot find any way to have stdout
print out from the ansible launch of the container.
I can go back to bash scripts to launch docker containers but I'd rather not...
How can I display a container's standard out with Ansible's docker_container module?
The URL or Unix socket path used to connect to the Docker API. 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, the module will automatically replace 'tcp' in the connection URL with 'https'.
Environment Variables Control how the modules connect to the Docker API by setting the following variables in the environment of the host running Ansible: DOCKER_HOST. The URL or Unix socket path used to connect to the Docker API. DOCKER_API_VERSION.
Run your Docker image with Ansible Now, with the Docker container image with Ansible installed, we have a ready-made Docker container image that can be copied to any container host and used to run Ansible with all the components already installed and ready.
This actually works with Ansible v2.5, by using the detach
attribute:
- name: Run the tests
docker_container:
detach: false
name: "foo-container"
command: npm test
register: docker_container_output
- name: Show test output
debug:
msg: "{{ docker_container_output.ansible_facts.docker_container.Output }}"
- name: Remove the test container
docker_container:
state: absent
name: "foo-container"
It's important to specify detach: false
to capture the output, and then to use the ansible_facts.docker_container.Output
path to access the results.
Don't use Ansible for running simple unit tests. Ansible makes complicated things easy and easy things complicated.
The following bash script is 4 lines and the Ansible playbook is already at 25 lines and still not fully functional due to concurrency issues and output formatting issues (I'm sure I'd find more).
Ansible for docker is still too new, doesn't deal easily with concurrency issues, has missing docker features, etc. and probably should only be used when you are doing something complicated that bash can't easily handle (like dealing with remote servers, retries, etc)
#!/bin/bash
docker build -t server-unit-test .
docker run -it --name server-unit-test server-unit-test npm test
docker rm server-unit-test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With