使用Docker Compose和bind mount时如何防止容器内内容删除?

问题描述 投票:0回答:0

我在下面有 Dockerfile 和 compose 文件。在 dockerfile 中,如果还没有应用程序,我会创建一个新的 symfony 应用程序。当我运行

docker compose build --build-arg CREATE_APP=true --no-cache --pull
时,该过程运行良好。 但是当我运行
docker compose up -d
然后因为我的 ./app/ 文件夹在主机上是空的,
/var/www/html
的内容在容器中被删除。

我怎样才能防止这种情况发生?也许整个方法都不正确。我应该在容器运行后创建应用程序吗?

# syntax=docker/dockerfile:1.4
FROM php:8.2.3-fpm-alpine
ARG TIMEZONE
ARG SYMFONY_VERSION=6.*
ARG CREATE_APP=false
ARG GIT_USER_NAME
ARG GIT_USER_EMAIL

RUN apk add --no-cache \
        bash \
        acl \
        fcgi \
        file \
        gettext \
        git \
        unzip \
    ;

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions gd xdebug intl pdo_mysql zip opcache apcu @composer

# Set timezone
RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone \
    && printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini \
    && "date"

COPY --link xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
COPY --link ./php.ini /usr/local/etc/php/conf.d/docker-php-config.ini

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

# Install symfony if required
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN set -eux; \
    if [ "${CREATE_APP}" = "true" ]; then \
        # composer create-project symfony/skeleton:${SYMFONY_VERSION} . --stability=stable; \
        # composer require webapp; \
        git config --global --add safe.directory /var/www/html; \
        git config --global user.name ${GIT_USER_NAME} ; \
        git config --global user.email ${GIT_USER_EMAIL}; \
        symfony new . --webapp; \
    fi


WORKDIR /var/www/html
version: '3'
services:
    php:
        build:
            context: ./docker/php
            args:
                TIMEZONE: ${TIMEZONE}
                username: ${username}
                GIT_USER_NAME: ${GIT_USER_NAME}
                GIT_USER_EMAIL: ${GIT_USER_EMAIL}
        depends_on:
            - db
        networks:
            - backend
        volumes:
            - ./app/:/var/www/html
docker docker-compose
© www.soinside.com 2019 - 2024. All rights reserved.