gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:10:18 25 4
gpt4 key购买 nike

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

这是ansible剧本的一部分,我正在使用什么以及导致问题的原因:

 # 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'Connection refused但容器存在并且当我在终端中手动运行相同命令时,它都无法正常工作。

感谢您的任何回应!

最佳答案

多亏了Tarun Lalwani,我才开始朝正确的方向前进,而不是使用他提出的bash脚本,我以这样的方式使它变得可笑

- 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

关于laravel - Ansible,从Docker容器运行命令时mysql连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47058500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com