如果不存在所需的Docker镜像,如何定义构建目录

时间:2016-06-30 20:04:59

标签: docker docker-compose

我有一个使用docker和docker-compose的项目,我有一个问题,我有一个自定义基本图像,如果我们的本地计算机上不存在图像,我必须手动构建。当FROM命令中使用的图像不存在或被删除时,有没有什么方法可以在docker文件中为初始情况定义构建目录?

这是我的撰写文件:

projectx:
  build: .
  environment:
    - PROJECTX_ENVIRONMENT=docker
    - PROJECTX_HOME=/projectx/server
  volumes:
    - ~/projectx:/projectx
  links:
    - mysql:mysql.projectx.local
    - postgres:postgres.projectx.local
    - dynamo:dynamo.projectx.local
    - s3:s3.projectx.local
  ports:
    - '8000:8000'
    - '80:80'
    - '12:22'
mysql:
  build: ./projectxMysql
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: rootpass
    MYSQL_USER: user1
    MYSQL_PASSWORD: password1
    MYSQL_DATABASE: mydb
    ADDITIONAL_SQL: GRANT ALL PRIVILEGES ON *.* TO 'user1' with grant option;
  volumes:
      - ~/db/mysql:/var/lib/mysql
postgres:
  image: postgres:9.4.5
  environment:
    POSTGRES_USER: username
    POSTGRES_PASSWORD: pass
    POSTGRES_DB: my_db
  ports:
    - '5432:5432'
  volumes:
    - ~/db/projectxpostgres:/var/lib/postgresql/data
dynamo:
  image: tray/dynamodb-local
  ports:
    - '7999:7999'
  command: '-port 7999 -inMemory'
s3:
  image: lphoward/fake-s3
  volumes:
    - ~/db/fake-s3:/fakes3_root
  ports:
    - '6999:6999'
  command : '-r /fakes3_root -p 6999'

projectx dockerfile看起来像:

FROM projectx-base

RUN mkdir /etc/apache2/logs

COPY ./config/apache/httpd.conf /etc/apache2/apache2.conf

COPY ./config/apache/extra /etc/apache2/extra
COPY ./config/apache/other /etc/apache2/other
COPY ./config/apache/mime.types /etc/apache2/mime.types

COPY config/php.ini /usr/local/etc/php/
COPY app /var/www/html
RUN chmod -R 644 /var/www && chmod -R +x /var/www

RUN mkdir -p /var/log/projectx

RUN apt-get install -y openssh-server ssh supervisor
RUN mkdir -p /var/run/sshd
RUN mkdir -p /var/log/supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

RUN echo 'root:projectxdocker' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd


EXPOSE 80 443 22
CMD ["/usr/bin/supervisord", "-n"]

projectx-base dockerfile位于子文件夹(projectxBase)中,如下所示:

FROM php:5.5-apache
MAINTAINER John

RUN apt-get update && apt-get install -y \
        vim \
        telnet \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng12-dev \
        libpq-dev \
        php5-dev \
        mysql-client \
        php5-mysql \
        php5-pgsql

RUN pecl install xdebug
RUN apt-get install -y libmemcached-dev
RUN pear channel-update pear.php.net
RUN pecl channel-update pecl.php.net
RUN pear upgrade-all

RUN docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd && \
    docker-php-ext-configure mysql --with-mysql=mysqlnd && \
    docker-php-ext-configure mysqli --with-mysqli=mysqlnd 

RUN docker-php-ext-install -j$(nproc) mcrypt pcntl pdo_mysql mysqli mysql pdo_pgsql
RUN pecl install memcached
RUN pecl install igbinary
RUN docker-php-ext-enable memcached

我意识到我可以放入一个dockerfile但我宁愿将其抽象出来

0 个答案:

没有答案