在Docker容器中构建项目并共享已安装的文件夹

时间:2018-10-22 11:49:44

标签: docker build docker-compose share mounted-volumes

我使用docker-compose构建容器,并在签出私有存储库的状态下,使用composer在容器内部构建项目。

我具有以下结构

- docker-project
-- docker //here I keep the containers
--- apache
---- Dockerfile // the content what I posted
-- docker-compose.yml
-- src //this is the mounted folder where I can work on project

构建后,我希望构建项目会显示在已安装的文件夹中,但是我得到的是空文件夹,这是我的yml

     apache:
        container_name: apache2
        build:
          context: ./docker/apache

        ports:
        - 80:80
        - 443:443
        links:
        - database
        restart: always
        volumes:
        - ./src:/var/www/
        - ./docker/logs/apache2/:/var/log/apache2/
        - ~/.ssh/id_rsa:/root/.ssh/id_rsa
        - ./docker/apache/sites/:/etc/apache2/sites-enabled

这里有docker文件

FROM php:7.1-apache
ENV APACHE_DOCUMENT_ROOT /var/www
RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    curl \
    openssl \
    openssh-server \
    sendmail \
    sendmail-bin \
    mailutils \
    libicu-dev \
    libmemcached-dev \
    libz-dev \
    libpq-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libssl-dev \
    libmcrypt-dev \
    libxml2-dev \
    libbz2-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    zlib1g-dev \
    libicu-dev  \
    g++ \
    libxml2-dev \
    git \
  && rm -rf /var/lib/apt/lists/*

# Install various PHP extensions which are required to build project
RUN docker-php-ext-configure bcmath --enable-bcmath \
    && docker-php-ext-configure pcntl --enable-pcntl \
    && docker-php-ext-configure pdo_mysql --with-pdo-mysql \
    && docker-php-ext-configure pdo_pgsql --with-pgsql \
    && docker-php-ext-configure mbstring --enable-mbstring \
    && docker-php-ext-configure soap --enable-soap \
    && docker-php-ext-install \
        bcmath \
        json \
        intl \
        mbstring \
        mcrypt \
        mysqli \
        pcntl \
        pdo_mysql \
        pdo_pgsql \
        soap \
        sockets \
        zip \
  && docker-php-ext-configure gd \
    --enable-gd-native-ttf \
    --with-jpeg-dir=/usr/lib \
    --with-freetype-dir=/usr/include/freetype2 && \
    docker-php-ext-install gd

##install xdebug and enable it. We need this for php unit coverage
RUN yes | pecl install xdebug \
&& docker-php-ext-enable xdebug

RUN yes | pecl install mongodb \
&& docker-php-ext-enable mongodb

RUN update-ca-certificates

# install composer to can install dependencies
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
    && php -r "unlink('composer-setup.php');"


RUN mkdir /root/.ssh
RUN rm -rf /var/www/project
WORKDIR /var/www/
RUN git checkout path/to/repo
RUN ls -l /var/www
WORKDIR /repo
RUN composer install
RUN ls -l

docker-compose build上,我看到该项目将被构建,但是在 docker-compose up文件夹为空

在这种情况下我缺少什么?

2 个答案:

答案 0 :(得分:0)

启动容器时,您正在/var/www上安装卷。

/var/www与主机上的./src/www相同,替换在映像构建期间添加到/var/www的任何内容。

请考虑使用composer docker image或确保将依赖项安装到另一个目录。在composer.json

中签出vendor_dir

示例: composer.json

{
 "require": {

 },
 "config": {
   "vendor-dir": "/var/www/vendor/"
 }
}

Docker映像构建:

...
RUN mkdir -p /var/www/vendor/
COPY composer.json /var/www/vendor/
RUN cd /var/www/vendor/ && composer install
RUN mkidr -p /var/www/app
...

docker-compose.yml

volumes:
  - ./src:/var/www/app

答案 1 :(得分:0)

我可以给出一种目前我们正在工作的方式。与其像我们这样克隆仓库,我们是在本地计算机上克隆仓库。

  • 您具有从中创建的标准docker映像 php:7.1-apache带有运行项目所需的必需软件。

  • 然后将项目克隆到主机。(请考虑到src文件夹)

  • 您在本地运行composer install

  • 然后按照您定义的docker-compose.ymlsrc文件夹装入/var/www/