从docker容器运行命令时,Ansible,mysql连接被拒绝

时间:2017-11-01 15:39:23

标签: laravel docker ansible docker-compose

我使用docker-compose运行Laravel项目并希望使用ansible进行部署,并简化本地环境的配置过程

以下是我使用的ansible剧本的一部分以及导致问题的原因:

f <- "as.integer"
v <- NA
do.call(f, list(v))

输出:

 # run docker-compose
- name: Run docker containers
  shell: docker-compose -f {{compose_file}} up -d

- name: Docker ps
  shell: docker-compose -f {{compose_file}} ps
  register: lists

- name: Print docker ps
  debug: msg="{{lists.stdout_lines}}"

# run migrations
- name: Run migrations
  shell: docker-compose -f {{compose_file}} exec -T --user {{wwwdata}} workspace sh -c 'cd {{remote_basedir}} && php artisan migrate --seed'

正如您所看到的那样,当TASK [Run docker containers] ************************************************************************* changed: [localhost] TASK [Docker ps] ************************************************************************* changed: [localhost] TASK [Print docker ps] ************************************************************************* ok: [localhost] => { "msg": [ " Name Command State Ports ", "------------------------------------------------------------------------------------------------------", "docker_applications_1 /true Exit 0 ", "docker_mailcatcher_1 mailcatcher --foreground - ... Up 25/tcp, 0.0.0.0:1080->80/tcp ", "docker_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:33066->3306/tcp ", "docker_nginx_1 nginx -g daemon off; Exit 1 ", "docker_php_1 docker-php-entrypoint php- ... Up 0.0.0.0:7778->7778/tcp, 9000/tcp ", "docker_redis_cache_1 docker-entrypoint.sh redis ... Up 6379/tcp, 0.0.0.0:6380->6380/tcp ", "docker_redis_disk_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp ", "docker_workspace_1 docker-php-entrypoint php- ... Up 9000/tcp " ] } TASK [Run migrations] ************************************************************************************************************************************************************************************************************** fatal: [localhost]: FAILED! => {"changed": true, "cmd": "docker-compose -f /var/www/project/docker/docker-compose.yml exec -T --user www-data workspace sh -c 'cd /var/www/vhosts/project && php artisan migrate --seed'", "delta": "0:00:01.333487", "end": "2017-11-01 17:13:08.772027", "failed": true, "msg": "non-zero return code", "rc": 1, "start": "2017-11-01 17:13:07.438540", "stderr": "", "stderr_lines": [], "stdout": "\n \n [Illuminate\\Database\\QueryException] \n SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s \n chema.tables where table_schema = database and table_name = migrations) \n \n\n \n [PDOException] \n SQLSTATE[HY000] [2002] Connection refused \n ", "stdout_lines": ["", " ", " [Illuminate\\Database\\QueryException] ", " SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s ", " chema.tables where table_schema = database and table_name = migrations) ", " ", "", " ", " [PDOException] ", " SQLSTATE[HY000] [2002] Connection refused ", " "]} to retry, use: --limit @/var/www/project/ansible_playbooks/install.retry PLAY RECAP ************************************************************************************************************************************************************************************************************************* localhost : ok=12 changed=11 unreachable=0 failed=1 运行错误docker-compose -f /var/www/adgate/project/docker/docker-compose.yml exec -T --user www-data rtb_workspace sh -c 'cd /var/www/vhosts/project && php artisan migrate --seed'但容器存在且我在终端中手动运行相同的命令时,它运行正常。

感谢您的回复!

1 个答案:

答案 0 :(得分:0)

感谢Tarun Lalwani我开始朝着正确的方向前进,而不是使用他提出的bash脚本,我用这样的方式用ansible做了

- name: wait for mysql container
  retries: 5
  delay: 5
  shell: docker-compose -f {{compose_file}} exec -T  mysql sh -c 'mysql --user=db_user --password=secret -e "SHOW DATABASES;" | grep -c database'
  register: mysql_alive
  until: mysql_alive.rc == 0