module docker : dynamic port binding problem

145 views
Skip to first unread message

Herve Leclerc

unread,
Jan 22, 2015, 3:19:10 AM1/22/15
to ansible...@googlegroups.com
Hello

Ansible 1.9 - docker-py 0.71 - docker version 1.4.1

I'm trying to do in a playbook this docker command line

 docker run --name projetx -d -v /Users/hleclerc/tmp/data/projetx/mysql:/var/lib/mysql -p :3306 -e MYSQL_ROOT_PASSWORD=pwd projetx/mysql

docker ps :

4876cb21a372        projetx/mysql:latest "/usr/local/bin/run"   4 seconds ago       Up 3 seconds   0.0.0.0:49193->3306/tcp  projetx
A dynamic port is allocated


Here is the playbooks :

1: 

- name: Starting MySQL container
    docker
:
      detach
: true
      image
: "projetx/mysql"
      name
:  "projetx"
     
ports: "::3306"
      env
: "MYSQL_ROOT_PASSWORD=pwd"
      volumes
:
       
- "/Users/hleclerc/tmp/data/projetx/mysql:/var/lib/mysql"
   

With this syntax if i do a docker ps : 

f84b89180678        projetx/mysql:latest       "/usr/local/bin/run"   About a minute ago   Up About a minute           3306/tcp

if i change ports with    

ports: 3306 

a docker ps will display

6cb9843603f7        projetx/mysql:latest  "/usr/local/bin/run"   8 seconds ago       Up 7 seconds     0.0.0.0:0->3306/tcp

if i change ports with ':3306' i get this error from ansible 

 bind = ('0.0.0.0', int(parts[0]))

ValueError: invalid literal for int() with base 10: ''

How can i have a correct behaviour ?

in advance thanks 


Brian Coca

unread,
Jan 22, 2015, 10:56:37 AM1/22/15
to ansible...@googlegroups.com
remove the colon's from the port setting

ports: 3306

--
Brian Coca

Herve Leclerc

unread,
Jan 23, 2015, 2:16:39 AM1/23/15
to ansible...@googlegroups.com
Hello

If i do it i've this

ports: 3306 

a docker ps display

6cb9843603f7 projetx/mysql:latest  "/usr/local/bin/run"   8 seconds ago Up 7 seconds  0.0.0.0:0->3306/tcp

This not good i should have 0.0.0.0:someDynamicPort->3306 and not 0

Herve Leclerc

unread,
Jan 24, 2015, 9:15:21 AM1/24/15
to ansible...@googlegroups.com
For information with docker-py :





from docker.client import Client
from docker.utils import kwargs_from_env
kwargs
= kwargs_from_env()
kwargs
['tls'].assert_hostname = False
client
= Client(**kwargs)
container_id
= client.create_container(image="busybox", ports=[3306] , command="sleep 30000000")
client
.start(container_id, port_bindings={3306: ('127.0.0.1',{})})


give me a correct behaviour
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
513db4e06837 busybox:latest "sleep 30000000" 3 seconds ago Up 3 seconds        127.0.0.1:49154->3306/tcp   sad_mclean


The problem is somewhere in docker module / parse

Herve Leclerc

unread,
Jan 24, 2015, 1:12:24 PM1/24/15
to ansible...@googlegroups.com
Finally i make it works you need both expose and ports parameters

- name: Starting MySQL container
    docker
:
      detach
: true

     
expose: 3306
      publish_all_ports
: true

      image
: "projetx/mysql"
      name
:  "projetx"
     
ports: 3306

      debug
: true
      env
: "MYSQL_ROOT_PASSWORD=password"
      volumes
:
       
- "/Users/hleclerc/tmp/data/projetx/mysql:/var/lib/mysql"
      docker_url
: "http://192.168.59.103:2375"
      state
: present
   
register: out

f5d49bd72ac0 projetx/mysql:latest "/usr/local/bin/run" 3 minutes agoUp 3 minutes        0.0.0.0:49155->3306/tcp  projetx


ttms
Reply all
Reply to author
Forward
0 new messages