# docker\_image/docker\_container not working - The error was: SyntaxError: invalid syntax

**URL:** <https://forum.ansible.com/t/docker-image-docker-container-not-working-the-error-was-syntaxerror-invalid-syntax/36272>\
**Category:** Ansible Project\
**Created:** [February 1, 2022, 10:37pm UTC](https://forum.ansible.com/t/docker-image-docker-container-not-working-the-error-was-syntaxerror-invalid-syntax/36272 "2022-02-01T22:37:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Randeepak\_Sandhu](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@Randeepak\_Sandhu](https://forum.ansible.com/u/Randeepak_Sandhu)\
**Post date:** [February 1, 2022, 10:37pm UTC](https://forum.ansible.com/t/docker-image-docker-container-not-working-the-error-was-syntaxerror-invalid-syntax/36272/1 "2022-02-01T22:37:59Z")

</div>

Hi,

Can someone please help me resolving this issue.

Summary

I am trying to build docker image passing in Dockerfile on the same host where Dockerfile and other related project files resides. Without using ansible, I am able to run my Dockerfile build an image out of it and start a container that is if i use docker itself. But when i try to put these steps into ansible playbook, it fails. On this host machine, docker, ansible, python-pip and docker sdk for python are installed

Outputs of different commands:  
ansible localhost -m debug -a “var=ansible\_python\_interpreter”  
localhost | SUCCESS =\> {  
“ansible\_python\_interpreter”: “/usr/bin/python2”  
}  
pip --version  
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)

pip show docker

Metadata-Version: 2.1  
Name: docker  
Version: 5.0.3

ansible --version | grep “python version”  
python version = 2.7.5

inventory file  
[local]  
localhost ansible\_connection=local

ansible --version

ansible 2.9.27 config file = /etc/ansible/ansible.cfg configured module search path = [u’/home/username/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Nov 13 2020, 02:52:00) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.3)]

Operating System: Oracle Linux Server 7.9

ansible-playbook -i inventory docker-container.yml OR ansible-playbook docker-container.yml

hosts: local  
vars:

- dockerfile\_sample: “Dockerfile”

- docker\_image\_name: “built-thru-ansible:1”

- docker\_container\_name: “running-thru-ansible”

- proxy: “[http://validproxyurl.com](http://validproxyurl.com):XXXX”  
tasks:

- name: “Build docker image”  
docker\_image:  
name: “{{docker\_image\_name}}”  
build:  
path: “path/to/dockerfiledir/”  
pull: no  
dockerfile: “{{dockerfile\_sample}}”  
buildargs:  
HTTP\_PROXY: “{{proxy}}”  
HTTPS\_PROXY: “{{proxy}}”  
state: present  
source: build

- name: “launch docker container”  
docker\_container:  
name: “{{docker\_container\_name}}”  
image: “{{docker\_image\_name}}”  
state: started  
exposed\_ports:

- “XXXX”  
ports:

- “XXXX:XXXX”  
register: docker\_info

Error: -

PLAY [local] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[WARNING]: Platform linux on host localhost 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](https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html) for more information.  
ok: [localhost]

TASK [Build docker image] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: SyntaxError: invalid syntax  
fatal: [localhost]: FAILED! =\> {“changed”: false, “module\_stderr”: “Traceback (most recent call last):\n File "/home/infra/.ansible/tmp/ansible-tmp-1643737614.37-38618-81901358275286/AnsiballZ\_docker\_image.py", line 102, in \n \_ansiballz\_main()\n File "/home/infra/.ansible/tmp/ansible-tmp-1643737614.37-38618-81901358275286/AnsiballZ\_docker\_image.py", line 94, in \_ansiballz\_main\n invoke\_module(zipped\_mod, temp\_path, ANSIBALLZ\_PARAMS)\n File "/home/infra/.ansible/tmp/ansible-tmp-1643737614.37-38618-81901358275286/AnsiballZ\_docker\_image.py", line 40, in invoke\_module\n runpy.run\_module(mod\_name=‘ansible.modules.cloud.docker.docker\_image’, init\_globals=None, run\_name=‘ **main** ’, alter\_sys=True)\n File "/usr/lib64/python2.7/runpy.py", line 176, in run\_module\n fname, loader, pkg\_name)\n File "/usr/lib64/python2.7/runpy.py", line 82, in \_run\_module\_code\n mod\_name, mod\_fname, mod\_loader, pkg\_name)\n File "/usr/lib64/python2.7/runpy.py", line 72, in \_run\_code\n exec code in run\_globals\n File "/tmp/ansible\_docker\_image\_payload\_pKjB0q/ansible\_docker\_image\_payload.zip/ansible/modules/cloud/docker/docker\_image.py", line 436, in \n File "/tmp/ansible\_docker\_image\_payload\_pKjB0q/ansible\_docker\_image\_payload.zip/ansible/module\_utils/docker/common.py", line 44, in \n File "/usr/lib/python2.7/site-packages/docker/ **init**.py", line 2, in \n from .api import APIClient\n File "/usr/lib/python2.7/site-packages/docker/api/ **init**.py", line 2, in \n from .client import APIClient\n File "/usr/lib/python2.7/site-packages/docker/api/client.py", line 222\n f’Error while fetching server API version: {e}'\n ^\nSyntaxError: invalid syntax\n”, “module\_stdout”: “”, “msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”, “rc”: 1}

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 2, 2022, 4:35pm UTC](https://forum.ansible.com/t/docker-image-docker-container-not-working-the-error-was-syntaxerror-invalid-syntax/36272/2 "2022-02-02T16:35:18Z")

</div>

Per the changelog from the docker python module ([https://docker-py.readthedocs.io/en/stable/change-log.html](https://docker-py.readthedocs.io/en/stable/change-log.html)), support for Python 2.7 was removed in version 5.0.0.

You will need to downgrade to an older version, or switch to using python3 to run your tasks.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 2, 2022, 6:35pm UTC](https://forum.ansible.com/t/docker-image-docker-container-not-working-the-error-was-syntaxerror-invalid-syntax/36272/3 "2022-02-02T18:35:55Z")

</div>

Hi,

when asking the same question in multiple locations  
([https://github.com/ansible-collections/community.docker/issues/290](https://github.com/ansible-collections/community.docker/issues/290)),  
please mention that so that people can check whether it has already  
been answered before spending time answering it again.

Thanks,  
Felix
