Dockerfile没有在docker-compose up -d上按预期方式建立

时间:2019-01-21 14:41:09

标签: php apache docker docker-compose dockerfile

我有一个PHP Laravel项目。我是Docker的新手。

我有一个从php-apache镜像创建的Dockerfile,它首先安装了一些php软件包,php composer,并将所有文件从我的项目复制到docker镜像apache文件夹,在这里...如下所示(请参见Dockerfile),我有RUN composer install --optimize-autoloader --no-dev; \ ...,多行php artisan命令。由于某些未知原因,它们无法正常工作。

当我执行docker-compose up -d时,php artisan key:generate --force(请参阅Dockerfile)不会在Docker映像中的APP_KEY文件的.env中添加值({生成并启动映像后,请使用docker exec -it IMAGE_ID bash && cat .env进行检查)。提示应用程序密钥设置成功

此外,php artisan migrate无法迁移表,它说:

In Connection.php line 664:

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s  
  chema.tables where table_schema = homestead and table_name = migrations)     


In Connector.php line 70:

  SQLSTATE[HY000] [2002] Connection refused

但是...在构建并启动映像之后,我确实登录到其bash-> docker exec -it IMAGE_ID bash,然后执行php artisan migrate,它成功迁移了所有表。太奇怪了。

能否请您帮我弄清楚为什么Docker失败:

  1. 生成应用密钥?
  2. 迁移?

这是我的Dockerfile

FROM php:7.2-apache

RUN apt-get update; \
    apt-get install -y zlib1g-dev git zip unzip; \
    docker-php-ext-install zip pdo_mysql;

COPY --from=composer:1.7.3 /usr/bin/composer /usr/bin/composer

WORKDIR /var/www

COPY ./ ./
COPY .env.example .env
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf

RUN composer install --optimize-autoloader --no-dev; \
    php artisan key:generate --force; \
    php artisan migrate; \
    php artisan db:seed; \
    php artisan cache:clear; \
    php artisan config:cache; \
    php artisan route:cache; \
    php artisan storage:link; \
    chown -R www-data:www-data /var/www;

CMD ["apache2ctl", "-D" , "FOREGROUND"]

我的apache-config.conf

ServerName localhost

<VirtualHost *:80>
  ServerAdmin beqalomadze@gmail.com
  ServerName localhost
  DocumentRoot /var/www/public

  <Directory /var/www/public>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order deny,allow
      Allow from all
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我的docker-compose.yml

version: '3'

services:
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: posts
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - "./data/mysql:/var/lib/mysql"

  app:
    depends_on:
      - mysql
    build:
      context: ./
      dockerfile: Dockerfile
    volumes:
      - "./data/storage-app:/var/www/storage/app"
    ports:
      - 3030:80

我的.env.example

...
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=posts
DB_USERNAME=root
DB_PASSWORD=root
...

1 个答案:

答案 0 :(得分:0)

您可以尝试在MYSQL conf中添加端口:

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: posts
      MYSQL_ROOT_PASSWORD: root
    ports:
      - 3306:3306
    volumes:
      - "./data/mysql:/var/lib/mysql"