1

我的应用程序在 docker 容器上运行并部署了 google 计算组并启用了自动缩放。我面临的问题是从自动缩放的计算实例连接 mysql 实例,但它无法正常工作。

Dockerfile

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y software-properties-common && \
...installation other extenstion
RUN curl -sS https://getcomposer.org/installer | \
    php -- --install-dir=/usr/bin/ --filename=composer
COPY . /var/www/html
CMD cd /var/www/html
RUN composer install
ADD nginx.conf/default /etc/nginx/sites-available/default
RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
RUN chmod +x cloud_sql_proxy
RUN mkdir /cloudsql
RUN chmod 777 /cloudsql
RUN chmod 777 -R storage bootstrap/cache
EXPOSE 80
**CMD service php7.1-fpm start && nginx -g "daemon off;" &&  ./cloud_sql_proxy -dir=/cloudsql -instances=<connectionname>=tcp:0.0.0.0:3306 -credential_file=file.json &**

当我运行我的容器时,最后一行./cloud_sql_proxy -dir=/cloudsql -instances=<connectionname>=tcp:0.0.0.0:3306 -credential_file=file.json &没有被执行。

如果我./cloud_sql_proxy -dir=/cloudsql -instances=<connectionname>=tcp:0.0.0.0:3306 -credential_file=file.json &在容器内运行它(通过 docker 命令进入容器)它正在工作,当我再次关闭终端时它停止工作。

即使我尝试在后台运行,但没有运气。

有人知道吗?

4

1 回答 1

2

已修复

  1. 创建 start.sh 文件并将所有命令移动到 start.sh
  2. 启动 sql 代理后,我将 sleep 10 并启动 nginx 和 php

现在它按预期工作。

Dockerfile
FROM ubuntu:16.04
...other command
ADD start.sh /
RUN chmod +x /start.sh
EXPOSE 80
CMD ["/start.sh"]

这是 start.sh 文件

//start.sh
#!/bin/sh
./cloud_sql_proxy -dir=/cloudsql -instances=<connectionname>=tcp:0.0.0.0:3306     -credential_file=<file>.json &
sleep 10
service php7.1-fpm start
nginx -g "daemon off;"
于 2020-02-18T10:04:51.207 回答