无法使用ansible创建docker容器

时间:2017-04-05 10:28:00

标签: python docker ansible

问题-1

我通过ansible安装了docker docker-py,pip,python设置工具,docker-io。

我尝试使用“ansible-container init”,ansible-container build“在docker中安装容器”但我收到语法错误,

***Traceback (most recent call last):
  File "/usr/bin/ansible-container", line 11, in <module>
    load_entry_point('ansible-container==0.3.0', 'console_scripts', 'ansible-container')()
  File "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 560, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 2648, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 2302, in load
    return self.resolve()
  File "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 2308, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/lib/python2.6/site-packages/container/cli.py", line 12, in <module>
    from . import engine
  File "/usr/lib/python2.6/site-packages/container/engine.py", line 554
    special_set = {'.', ':'}
                      ^
SyntaxError: invalid syntax***

第2期

我甚至尝试使用docker-images和docker-container在yaml文件中构建和运行。

我在下面得到了一些其他内容。

***TASK [Build Docker image from Dockerfiles.] ************************************
ok: [192.168.56.104]
TASK [Run the test container.] *************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'keys'
fatal: [192.168.56.104]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_UTbJkn/ansible_module_docker_container.py\", line 2007, in <module>\n    main()\n  File \"/tmp/ansible_UTbJkn/ansible_module_docker_container.py\", line 2000, in main\n    cm = ContainerManager(client)\n  File \"/tmp/ansible_UTbJkn/ansible_module_docker_container.py\", line 1652, in __init__\n    self.present(state)\n  File \"/tmp/ansible_UTbJkn/ansible_module_docker_container.py\", line 1677, in present\n    different, differences = container.has_different_configuration(image)\n  File \"/tmp/ansible_UTbJkn/ansible_module_docker_container.py\", line 1220, in has_different_configuration\n    expected_exposed=[re.sub(r'/.+$', '', p) for p in config.get('ExposedPorts', dict()).keys()],\nAttributeError: 'NoneType' object has no attribute 'keys'\n", "module_stdout": "", "msg": "MODULE FAILURE"}
        to retry, use: --limit @/home/vagrant/playbook/dockerinstall.retry***

我在yaml的代码是这样的,

   - name: Build Docker image from Dockerfiles.
     docker_image:
        name: test
        path: test
        state: build
   - name: Run the test container.
     docker_container:
        image: test:latest
        name: test
        state: present

1 个答案:

答案 0 :(得分:0)

Sriram,感谢您对Ansible Container的兴趣,以及花时间发布问题。

Issue-1 中的错误似乎与Python的版本有关。我发现它引用了/usr/lib/python2.6。切换到2.7+,这应该消失。

问题-2 中,您似乎发布了main.yml的副本。您不应该使用docker_imagedocker_container模块。我将尝试解释,但您可能会发现查看我们的Getting Started Guide

会很有帮助

我们的想法是使用Ansible Container首先build成像。例如,假设您要运行Nginx Web服务器。您首先要构建一个能够运行服务器的映像,这样您的main.yml将如下所示:

- name: Install nginx
  hosts: web
  roles:
  - j00bar.nginx-container

您的container.yml可能如下所示:

Version: 2
services:
  nginx:
    image: centos:7
    ports:
    - "8000:80"
    user: 'nginx'
    command: ['/usr/bin/dumb-init', 'nginx', '-c', '/etc/nginx/nginx.conf']

当然,您需要将角色添加到requirements.yml,如下所示:

- role: j00bar.nginx-container

要构建图像,请执行ansible-container build。这将基于web图像和centos:7容器启动名为build的新容器。

build容器将下载角色,并对main.yml容器执行web playbook。完成后,它将提交web容器的副本,从而创建新图像。

现在您已准备好通过执行ansible-container run来运行Nginx服务器。这次它将使用新图像启动web容器。

希望这是有道理的。但是,请再次查看Getting Started Guide