Failing to start nginx container when volumes is used (using ansible and docker-compose) - docker

I am trying to start an nginx container using ansible with docker-compose from one machine to a different machine.
Whenever I include nginx.conf to the volumes, there is an error which I do not understand. The container is only created but not starting.
MACHINE-1
Command to run the playbook: ansible-playbook -v nginx-playbook.yml -l ubuntu_node_1 -u root
my playbook:
- name: nginx-docker_compose
hosts: all
gather_facts: yes
become: yes
tasks:
- community.general.docker_compose:
project_name: nginx
definition:
version: '2'
services:
web:
image: nginx:latest
volumes:
- ./vars/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "8080:80"
[EDITED]
Here is the error:
Using /etc/ansible/ansible.cfg as config file
PLAY [nginx-docker_compose] ********************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 172.31.15.176 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior
Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [172.31.15.176]
TASK [community.general.docker_compose] ********************************************************************************************************************************
fatal: [172.31.15.176]: FAILED! => {"changed": false, "errors": [], "module_stderr": "Recreating nginx_web_1 ... \n\u001b[1A\u001b[2K\nRecreating nginx_web_1 ... \n\u001b[1B", "module_stdout": "", "msg": "Error starting project Encountered errors while bringing up the project."}
PLAY RECAP *************************************************************************************************************************************************************
172.31.15.176 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[root#ip-172-31-12-130 docker_server]# ansible-playbook -v nginx-playbook.yml -l ubuntu_node_1 -u root
Using /etc/ansible/ansible.cfg as config file
PLAY [nginx-docker_compose] ********************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 172.31.15.176 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior
Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [172.31.15.176]
TASK [community.general.docker_compose] ********************************************************************************************************************************
fatal: [172.31.15.176]: FAILED! => {"changed": false, "errors": [], "module_stderr": "Recreating 9b102bbf98c2_nginx_web_1 ... \n\u001b[1A\u001b[2K\nRecreating 9b102bbf98c2_nginx_web_1 ... \n\u001b[1B", "module_stdout": "", "msg": "Error starting project Encountered errors while bringing up the project."}
PLAY RECAP *************************************************************************************************************************************************************
172.31.15.176 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
NOTE: When I try to run nginx container directly using docker-compose with the same config on MACHINE-2, it works.
I believe there are some permission issues happening while trying to execute the playbook from MACHINE-1 to MACHINE-2 but can not figure it out.

It works now. Thanks to #mdaniel.
Things I changed:
I wrote the entire directory in the playbook- /home/some_more_folders/nginx.conf
and copied the same file with same directory structure on the destination machine.
Still open questions:
Any idea why is it necessary to copy any file to the destination machine (such as nginx.conf)?
How this manual process of copying of config files to destination machine for docker-compose be automated?

Related

Ansible Skipping Docker Build

Trying to get Ansible set up to learn about it, so could be a very simple mistake but I can't find the answer to it anywhere. When I try to run ansible-playbook it's just simply skipping the job with the following output:
ansible-playbook -i hosts simple-devops-image.yml --check
PLAY [all] ***********************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 127.0.0.1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
ok: [127.0.0.1]
TASK [build docker image using war file] *****************************************************************************************************************************************************************************************************************************************************
skipping: [127.0.0.1]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
My .yml playbook file:
---
- hosts: all
become: yes
tasks:
- name: build docker image using war file
command: docker build -t simple-devops-image .
args:
chdir: /usr/local/src
My hosts file:
[localhost]
127.0.0.1 ansible_connection=local
command module is skipped when executing with check mode. Remove —check from ansible-playbook command to build docker image.
Here is a note from the doc:
Check mode is supported when passing creates or removes. If running in check mode and either of these are specified, the module will check for the existence of the file and report the correct changed status. If these are not supplied, the task will be skipped.

How to use docker_image and docker_container module in Ansible

I would like to make a simple script that can deploy and download docker image from docker hub using Ansible but when I am trying to execute mycode I experience some errors as follows:
PLAY [manage docker using ansible] *********************************************
TASK [Gathering Facts] *********************************************************
ok: [controller]
ok: [worker]
TASK [ping hosts] **************************************************************
ok: [controller]
ok: [worker]
TASK [pull docker image] *******************************************************
fatal: [controller]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (docker_image) module: source Supported parameters include: api_version,archive_path,buildargs,cacert_path,cert_path,container_limits,debug,docker_host,dockerfile,filter_logger,force,http_timeout,key_path,load_path,name,nocache,path,pull,push,repository,rm,ssl_version,state,tag,timeout,tls,tls_hostname,tls_verify,use_tls"}
fatal: [worker]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (docker_image) module: source Supported parameters include: api_version,archive_path,buildargs,cacert_path,cert_path,container_limits,debug,docker_host,dockerfile,filter_logger,force,http_timeout,key_path,load_path,name,nocache,path,pull,push,repository,rm,ssl_version,state,tag,timeout,tls,tls_hostname,tls_verify,use_tls"}
to retry, use: --limit #/home/testuser/docker_manage.retry
PLAY RECAP *********************************************************************
controller : ok=2 changed=0 unreachable=0 failed=1
worker : ok=2 changed=0 unreachable=0 failed=1
here is my code:
---
- name: manage docker using ansible
hosts: all
become: true
tasks:
- name: ping hosts
ping:
- name: pull docker image
docker_image:
name: busybox
source: pull
- name: deploy container
docker_container:
name: first_container
image: busybox
state: present
I have tried to follow guide on this [link] (https://docs.ansible.com/ansible/latest/scenario_guides/guide_docker.html) to config my ansible.cfg file.
Unsupported parameters for (docker_image) module: source
Although you did not provide your ansible version in your question, I'm quite sure you need to upgrade (or to remove this parameter). The source parameter was added in ansible 2.8
Ref: https://docs.ansible.com/ansible/latest/modules/docker_image_module.html#parameter-source

Running 'docker-compose up' throws permission denied when trying official samaple of Docker

I am using Docker 1.13 community edition on a CentOS 7 x64 machine. When I was following a Docker Compose sample from Docker official tutorial, all things were OK until I added these lines to the docker-compose.yml file:
volumes:
- .:/code
After adding it, I faced the following error:
can't open file 'app.py': [Errno 13] Permission denied. It seems that the problem is due to a SELinux limit. Using this post I ran the following command:
su -c "setenforce 0"
to solve the problem temporarily, but running this command:
chcon -Rt svirt_sandbox_file_t /path/to/volume
couldn't help me.
Finally I found the correct rule to add to SELinux:
# ausearch -c 'python' --raw | audit2allow -M my-python
# semodule -i my-python.pp
I found it when I opened the SELinux Alert Browser and clicked on 'Details' button on the row related to this error. The more detailed information from SELinux:
SELinux is preventing /usr/local/bin/python3.4 from read access on the
file app.py.
***** Plugin catchall (100. confidence) suggests **************************
If you believe that python3.4 should be allowed read access on the
app.py file by default. Then you should report this as a bug. You can
generate a local policy module to allow this access. Do allow this
access for now by executing:
ausearch -c 'python' --raw | audit2allow -M my-python
semodule -i my-python.pp

using ansible with docker-compose

I am trying to deploy a docker setup using Ansible playbook. For this, I am using docker_service.
My Playbook looks like:
---
- name: Run Docker compose
hosts: all
gather_facts: no
tasks:
- debug: msg="Container - {{ inventory_hostname }}"
- docker_service:
project_src: "compose"
state: absent
- docker_service:
project_src: "compose"
state: present
Upon running this simple playbook as:
ansible-playbook -v playbook.yml --ask-sudo-pass
I added --ask-sudo-pass to ensure that it was not a permission issue.
OUTPUT
SUDO password:
PLAY [Run Docker compose] ******************************************************
TASK [debug] *******************************************************************
ok: [prolims-staging] => {
"msg": "Container - prolims-staging"
}
TASK [docker_service] **********************************************************
fatal: [prolims-staging]: FAILED! => {"changed": false, "msg": "Error connecting: Error while fetching server API version: ('Connection aborted.', error(13, 'Permission denied'))"}
to retry, use: --limit #/data/prolims-provision/provision-docker.retry
PLAY RECAP *********************************************************************
prolims-staging : ok=1 changed=0 unreachable=0 failed=1
I did try looking out for this issue on other forums as well ( and similar questions on this StackOverflow too), but those were not helpful.
Note: I am able to run docker-compose successfully in the target machine from its CLI (using sudo).
Also, I tried playing around with docker_container as well. I tried to execute a playbook with contents below:
...
- name: check container status
command: docker ps
register: result
- name: Create a container
docker_container:
name: db_pg
image: "postgres:latest"
state: present
recreate: yes
...
and running this playbook works perfectly fine.
I assume, posting my docker-compose file might not be relevant here.
I followed this example, but did not work. Maybe, I might be missing some stupid or really important thing here.
Any help on understanding and resolving this issue would be appreciated.
I am able to run docker-compose successfully in the target machine from its CLI (using sudo).
So you need to use become declaration for the task.
I added --ask-sudo-pass to ensure that it was not a permission issue.
Just adding --ask-sudo-pass to the ansible-playbook parameters doesn't have any effect unless the relevant tasks/plays have become declaration (and become_method is set to sudo, but this is by default).
Reference.

Issue faced on GLIBC_PRIVATE not defined during ansible container build

Code : container.yml
version: '2'
settings:
conductor_base: centos:7
services:
ansible.play_container:
from: "play:8_jre_security"
roles:
- play_container
ports:
- "9000:9000"
user: play
command: ['app/xxx/docker-entrypoint.sh']
registries: {}
OS / ENVIRONMENT Details :
Ansible Container, version 0.9.2
Linux, 3.10.0-327.13.1.el7.x86_64, #1 SMP Mon Feb 29 13:22:02 EST 2016, x86_64
2.7.5 (default, May 3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] /usr/bin/python
Command Used : sudo ansible-container --debug build
Error Log:
Fatal: [ansible.play_container]: FAILED! => {
"changed": false,
"module_stderr": "/_usr/bin/python: relocation error: /_usr/lib64/libc.so.6: symbol _dl_starting_up, version GLIBC_PRIVATE not defined in file ld-linux-x86-64.so.2 with link time reference\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}
to retry, use: --limit #/tmp/tmpXAGeVC/playbook.retry
PLAY RECAP *********************************************************************
ansible.play_container : ok=0 changed=0 unreachable=0 failed=1
2017-11-28T06:09:14.110001 Error applying role! [container.core] caller_file=/_ansible/container/core.py caller_func=apply_role_to_container caller_line=699 engine=<container.docker.engine.Engine object at 0x7fbcfd43ce90> exit_code=2 playbook=[{'hosts': u'ansible.play_container', 'roles': ['play_container'], 'vars': {}}]
Please check and help on steps to resolve the relocation error
The conductor base image and the target container base image need to derive from the same distro. I'm guessing whatever play:8_jre_security is, it's not a CentOS 7 derived image.

Resources