简体   繁体   English

使用 Ansible 启动多个 Docker 容器

[英]Launching Multiple Docker Containers using Ansible

I am trying to build an image and trying to launch multiple docker containers using ansible playbook.我正在尝试构建映像并尝试使用 ansible 剧本启动多个 docker 容器。 I am not able to understand how do i publish the ports.我无法理解如何发布端口。 The below playbook gives me an error which is quite obvious that the port is already allocated but then how do i achieve this because from outside the containers there will only be one port right to acces all the containers?下面的剧本给了我一个错误,很明显端口已经分配但是我如何实现这一点,因为从容器外部只有一个端口有权访问所有容器?

Playbook -剧本 -

- name: Manage Docker instances via Ansible
  hosts: shashank-VM
  connection: local
  become: yes
  become_method: sudo
  tasks:
    - name: Building an image from Dockerfile
      docker_image:
        build:
          path: .
          pull: yes
        name: web_new
        source: build

    - name: Creation of Docker Containers
      docker_container:
        name: my-app-{{ item }}
        image: web_new
        state: present
        ports:
         - "79:80"
      with_sequence: count=3

    - name: Starting Docker Containers
      docker_container:
        name: my-app-{{ item }}
        image: web_new
        state: started
      with_sequence: count=3

Error -错误 -

changed: [shashank-VM]

TASK [Creation of Docker Containers] *********************************************************************************************************
changed: [shashank-VM] => (item=1)
changed: [shashank-VM] => (item=2)
changed: [shashank-VM] => (item=3)

TASK [Starting Docker Containers] ************************************************************************************************************
changed: [shashank-VM] => (item=1)
failed: [shashank-VM] (item=2) => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "ansible_loop_var": "item", "changed": false, "item": "2", "msg": "Error starting container beb7f1d204f47862d16722f70b812df7193ddacf12d15350a9095cec2ebf4d85: 500 Server Error: Internal Server Error (\"driver failed programming external connectivity on endpoint my-app-2 (880c06fe9e2efa75537e350734be1d46d0cc76e7acf70733d19ad38706dde5ab): Bind for 0.0.0.0:78 failed: port is already allocated\")"}
failed: [shashank-VM] (item=3) => {"ansible_loop_var": "item", "changed": false, "item": "3", "msg": "Error starting container 048f2f3ea6fed5e094fdf59a4650b2b3f8164d804ee7dc8875e6e95bda1300d7: 500 Server Error: Internal Server Error (\"driver failed programming external connectivity on endpoint my-app-3 (8247f75384b240cb9bf1ee66cc9f0404df5465e6c08903304f14bd813c218fa1): Bind for 0.0.0.0:78 failed: port is already allocated\")"}

NOTE: I have an application for which I am building an image and there will be multiple containers running for that image.注意:我有一个正在为其构建映像的应用程序,并且将为该映像运行多个容器。 How do i accessible my application from outside?我如何从外部访问我的应用程序? How do i work on the ports?我如何在端口上工作?

Any help is appreciated任何帮助表示赞赏

The cause of the issue is here:问题的原因在这里:

> Bind for 0.0.0.0:78 failed: port is already allocated

Check what application/container blocks port 78检查哪些应用程序/容器阻塞了端口78

  • You can do it with ss :你可以用ss做到这一点:
sudo ss -plunt | grep :78
  • Or with lsof :或与lsof
lsof -i :78
  • Or with fuser :或使用fuser
fuser -v -n tcp 78

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM