0
votes

I am a newbie to ansible and trying to convert my docker-compose file to ansible.yml.

I put the docker image of a dummy flask app into my gitlab repository as mentioned in https://codebabel.com/ci-gitlabci-docker/.

I wrote a docker-decompose.yaml to run both the above image and elastic search and it's working fine.

  # Run both the app and Elasticsearch inside Docker containers.
version: '3'

services:
  app:
    image: registry.gitlab.com/bacdef/flaskapp:latest
    restart: on-failure
    ports:
      - 5000:5000
    depends_on:
    - elasticsearch
    links:
    - elasticsearch

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
    container_name: es01
    environment:
      - node.name=es01
      - cluster.initial_master_nodes=es01
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet

volumes:
  esdata01:
    driver: local

networks:
  esnet:

Then I tried to convert the same docker-compose.yml into ansible.yml as follows

---
- hosts: localhost
  tasks:
    - name: "Elasticsearch"
      docker_container:
        name: "es01"
        image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
        ports:
          - "9200:9200"
        volumes:
          - "sdata01:/usr/share/elasticsearch/data"
        env:
          node.name: es01
          cluster.initial_master_nodes: es01
          cluster.name: docker-cluster
          bootstrap.memory_lock: "true"

    - name: "Launch app container"
      docker_container:
        name: "app"
        image: registry.gitlab.com/bacdef/flaskapp:latest
        ports:
          - "5000:5000"

When I ran the above ansible file it fails with

PLAY [localhost]


TASK [Gathering Facts] ********************************************************* ok: [localhost]

TASK [Elasticsearch] *********************************************************** changed: [localhost]

TASK [Launch app container] **************************************************** fatal: [localhost]: FAILED! => {"changed": false, "msg": "Error starting container 380e56eb2d9579d407727b031713b056efda14f1a73fd9c88f65349567caa9f9: 500 Server Error: Internal Server Error (\"driver failed programming external connectivity on endpoint app (2020132c9cb503817740c063b1d231e3bb72e56d91a0386edd0cc6d23def992f): Bind for 0.0.0.0:5000 failed: port is already allocated\")"}

PLAY RECAP ********************************************************************* localhost : ok=2 changed=1 unreachable=0
failed=1 skipped=0 rescued=0 ignored=0

But I am sure that no other process is using the port 5000 other than this docker image. I ran losof -i:5000 and killed the process which was running on this port before running ansible-playbook ansible.yml I could not find why the process is not able to bind to 5000.

Could someone please share some high level advice

1

1 Answers

1
votes

If ansible tells you that the port is already in use, then it means that it is already in use.
Try to run lsof as root to find wich process is binded to your port :

sudo lsof -i -P -n | grep 5000

Eventually, you can also check that you have no container running on this port (with a basic docker container ls)