docker工人等着数据库

我试图部署docker工人。 我有一个docker数据库容器,在其他容器是我的networking应用程序,我试图链接这两个容器。 问题是数据库容器没有时间来configuration自己,并且Web容器已经启动。 我的可靠的剧本看起来像这样:

... - name: run mysql in docker container docker: image: "mysql:5.5" name: database env: "MYSQL_ROOT_PASSWORD=password" state: running - name: run application containers docker: name: "application" image: "myapp" ports: - "8080:8080" links: - "database:db" state: running 

如何确定数据库是否启动? 我尝试与wait_for模块,但没有奏效。 我不想设置超时,这不是我的好select。

wait_for不适用于MySQL docker容器,因为它只检查端口是否可连接(这对于Docker容器来说是真正的)。 但是, wait_for不检查容器内的服务是否侦听端口并向客户端发送响应。

这就是我为了让MySQL服务在Docker容器内完全运行而在剧本中的等待:

 - name: Start MySQL container docker: name: some-name image: mysql:latest state: started ports: - "8306:3306" # it's important to expose the port for waiting requests env: MYSQL_ROOT_PASSWORD: "{{ mysql_root_password }}" - template: mode="a+rx,o=rwx" src=telnet.sh.j2 dest=/home/ubuntu/telnet.sh # wait while MySQL is starting - action: shell /home/ubuntu/telnet.sh register: result until: result.stdout.find("mysql_native_password") != -1 retries: 10 delay: 3 

而telnet.sh.j2是

 #!/bin/bash -e telnet localhost 8306 || true 

使用wait_for模块。 我不是MySQL的专家,但我认为在某些日志文件中会有一些端口或文件或消息的存在,您可以检查数据库是否启动。

这里是从上面的链接复制的wait_for例子。

 # wait 300 seconds for port 8000 to become open on the host, don't start checking for 10 seconds - wait_for: port=8000 delay=10 # wait 300 seconds for port 8000 of any IP to close active connections, don't start checking for 10 seconds - wait_for: host=0.0.0.0 port=8000 delay=10 state=drained # wait 300 seconds for port 8000 of any IP to close active connections, ignoring connections for specified hosts - wait_for: host=0.0.0.0 port=8000 state=drained exclude_hosts=10.2.1.2,10.2.1.3 # wait until the file /tmp/foo is present before continuing - wait_for: path=/tmp/foo # wait until the string "completed" is in the file /tmp/foo before continuing - wait_for: path=/tmp/foo search_regex=completed # wait until the lock file is removed - wait_for: path=/var/lock/file.lock state=absent # wait until the process is finished and pid was destroyed - wait_for: path=/proc/3466/status state=absent # wait 300 seconds for port 22 to become open and contain "OpenSSH", don't assume the inventory_hostname is resolvable # and don't start checking for 10 seconds - local_action: wait_for port=22 host="{{ ansible_ssh_host | default(inventory_hostname) }}" search_regex=OpenSSH delay=10 

为了避免嘘,我通常没有安装远程login…

 - name: Wait for database to be available shell: docker run --rm --link mysql:mysql mysql sh -c 'mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p{{mysql_password}} || true' register: result until: result.stderr.find("Can't connect to MySQL") == -1 retries: 10 delay: 3 

这对我来说很好:

  - name: get mariadb IP address command: "docker inspect --format '{''{ .NetworkSettings.IPAddress }''}' mariadb-container" register: mariadb_ip_address - name: wait for mariadb to become ready wait_for: host: "{{ mariadb_ip_address.stdout }}" port: 3306 state: started delay: 5 connect_timeout: 15 timeout: 30