Symfony安装在docker容器中不起作用

Symfony install not working in docker container

提问人:Csaba Vörös 提问时间:11/17/2023 最后编辑:Csaba Vörös 更新时间:11/18/2023 访问量:71

问:

我在 docker 中遇到了一些 symfony 安装问题。我想创建一个 docker 镜像,它会自动安装 symfony 并运行它。我正在使用 .这是我的文件:docker-composedocker-compose.yml

version: "3"

networks:
   nginx-php81-mysql8-node:

services:
   # nginx
   nginx-service:
      image: nginx:stable-alpine
      container_name: nginx-container
      ports:
         - "8080:80"
      volumes:
         - ./app:/var/www/html
         - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      depends_on:
         - php81-service
         - mysql8-service

   # php
   php81-service:
      build:
         context: .
         dockerfile: docker/php/Dockerfile
      container_name: php81-container
      ports:
         - "8000:8000"
      volumes:
         - ./app:/var/www/html
         - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
         - ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini

这是我的 Dockerfile:

FROM php:8.1-fpm

WORKDIR /var/www/html

RUN apt-get update && apt-get install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
    && docker-php-ext-install intl opcache pdo pdo_mysql \
    && pecl install apcu \
    && docker-php-ext-enable apcu \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony5/bin/symfony /usr/local/bin/symfony

RUN composer create-project symfony/skeleton:^6.3 .

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

RUN touch /var/log/xdebug.log && \
    chmod 777 /var/log/xdebug.log

问题是,当我的映像正在构建时,我可以看到正在创建symfony项目,我看不到过程中的任何错误。但是,当我进入容器时,我的文件夹是空的,应该有symfony项目的文件。/var/www/html

编辑

好吧,现在我知道问题出在哪里了。因为我将本地文件夹挂载到容器中,所以它是空的。当我运行容器时,我的空文件夹已经挂载了 beenne。app

那么,有没有办法在容器内安装symfony,同时将文件夹映射到我的本地文件夹呢?

php docker symfony docker-compose composer-php

评论

0赞 Alexandre Tranchant 11/17/2023
你说它不起作用是什么意思?composer 命令之前是否有错误?您是否尝试使用完整路径而不是相对路径?在任何情况下,都不能在非空目录中创建项目。
0赞 Csaba Vörös 11/17/2023
在构建容器时,我看不到任何错误。命令后 html 文件夹为空。但是,如果我进入容器,并在安装它的目录中手动运行命令,并且symfony文件就在那里。composer create-project symfony/skeleton:^6.3 .create-projecthtml
0赞 Sammitch 11/17/2023
在映像构建期间创建任何内容是没有意义的,因为在运行容器时只是挂载到它上面。卷安装在映像中的目录,不会合并任何内容。/var/www/html./app
0赞 Csaba Vörös 11/17/2023
我只想在我的容器内运行所有内容。或者我应该在容器外部的目录中运行 symfony 安装程序?./app
0赞 Nico Haase 11/17/2023
“但它没有用”——这是什么意思?这是PHP问题,Symfony问题,Composer问题还是Docker问题?请通过编辑为您的问题添加所有钙化

答:

0赞 Adrian98do 11/17/2023 #1

另一种方法是在 docker 容器之外创建 symfony 项目,然后在 docker-compose 中挂载卷,指定 symfony 项目的位置以及要将其挂载到 de docker 容器中的位置。

 volumes:
     - ./symfony-path-host:/var/www/html

我希望这能帮到你。

评论

0赞 Csaba Vörös 11/18/2023
是的,只有一种方法可以做到这一点。但是我想在容器内安装symfony。
1赞 maiorano84 11/18/2023 #2

这是理解“生成时”和“运行时”之间差异的问题。

Dockerfile 是一组指令,用于指定映像生成。容器运行时会挂载卷。

将文件安装到映像的工作目录中,然后在运行映像时,将一个空文件夹装载到同一目录中,从而导致...。一个空项目。

有关如何解决此问题的一个很好的示例是查看一些流行的应用程序如何在运行时为其映像设置安装过程。他们通常通过创建入口点脚本(在容器启动时运行)来执行此操作,检查是否存在某些密钥文件,然后将源从其他目录复制到工作目录中。

以下是 Wordpress 如何做到这一点的一个例子: https://github.com/docker-library/wordpress/tree/master/latest/php8.2/fpm-alpine

在生成时,他们将代码安装到自己的存储库中:https://github.com/docker-library/wordpress/blob/master/latest/php8.2/fpm-alpine/Dockerfile#L100/usr/src/wordpress

RUN set -eux; \
    version='6.4.1'; \
    sha1='35e62d935d6a93097366476e389752dd55d8a077'; \
    \
    curl -o wordpress.tar.gz -fL "https://wordpress.org/wordpress-$version.tar.gz"; \
    echo "$sha1 *wordpress.tar.gz" | sha1sum -c -; \
    \
# upstream tarballs include ./wordpress/ so this gives us /usr/src/wordpress
    tar -xzf wordpress.tar.gz -C /usr/src/; \
    rm wordpress.tar.gz; \
    \
# https://wordpress.org/support/article/htaccess/
    [ ! -e /usr/src/wordpress/.htaccess ]; \
    { \
        echo '# BEGIN WordPress'; \
        echo ''; \
        echo 'RewriteEngine On'; \
        echo 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]'; \
        echo 'RewriteBase /'; \
        echo 'RewriteRule ^index\.php$ - [L]'; \
        echo 'RewriteCond %{REQUEST_FILENAME} !-f'; \
        echo 'RewriteCond %{REQUEST_FILENAME} !-d'; \
        echo 'RewriteRule . /index.php [L]'; \
        echo ''; \
        echo '# END WordPress'; \
    } > /usr/src/wordpress/.htaccess; \
    \
    chown -R www-data:www-data /usr/src/wordpress; \
# pre-create wp-content (and single-level children) for folks who want to bind-mount themes, etc so permissions are pre-created properly instead of root:root
# wp-content/cache: https://github.com/docker-library/wordpress/issues/534#issuecomment-705733507
    mkdir wp-content; \
    for dir in /usr/src/wordpress/wp-content/*/ cache; do \
        dir="$(basename "${dir%/}")"; \
        mkdir "wp-content/$dir"; \
    done; \
    chown -R www-data:www-data wp-content; \
    chmod -R 1777 wp-content

然后,在他们的入口点中,他们检查某些文件和文件夹。如果它们不存在,则将它们复制到 : https://github.com/docker-library/wordpress/blob/master/latest/php8.2/fpm-alpine/docker-entrypoint.sh#L34/var/www/html

echo >&2 "WordPress not found in $PWD - copying now..."
        if [ -n "$(find -mindepth 1 -maxdepth 1 -not -name wp-content)" ]; then
            echo >&2 "WARNING: $PWD is not empty! (copying anyhow)"
        fi
        sourceTarArgs=(
            --create
            --file -
            --directory /usr/src/wordpress
            --owner "$user" --group "$group"
        )
        targetTarArgs=(
            --extract
            --file -
        )
        if [ "$uid" != '0' ]; then
            # avoid "tar: .: Cannot utime: Operation not permitted" and "tar: .: Cannot change mode to rwxr-xr-x: Operation not permitted"
            targetTarArgs+=( --no-overwrite-dir )
        fi
        # loop over "pluggable" content in the source, and if it already exists in the destination, skip it
        # https://github.com/docker-library/wordpress/issues/506 ("wp-content" persisted, "akismet" updated, WordPress container restarted/recreated, "akismet" downgraded)
        for contentPath in \
            /usr/src/wordpress/.htaccess \
            /usr/src/wordpress/wp-content/*/*/ \
        ; do
            contentPath="${contentPath%/}"
            [ -e "$contentPath" ] || continue
            contentPath="${contentPath#/usr/src/wordpress/}" # "wp-content/plugins/akismet", etc.
            if [ -e "$PWD/$contentPath" ]; then
                echo >&2 "WARNING: '$PWD/$contentPath' exists! (not copying the WordPress version)"
                sourceTarArgs+=( --exclude "./$contentPath" )
            fi
        done
        tar "${sourceTarArgs[@]}" . | tar "${targetTarArgs[@]}"
        echo >&2 "Complete! WordPress has been successfully copied to $PWD"

tl;博士;使用 ENTRYPOINTs 来帮助处理构建中的运行时需求。

评论

0赞 Csaba Vörös 11/19/2023
谢谢,这个解决方案正在工作。也感谢您的解释。