title: How can I define a build directory if a required Docker images doesn't exist
body: <p>I have a project that uses docker and docker-compose and I have an issue where I have a custom base Image that I have to manually build if image doesn't exist on our local machine. Is there any way I can define a build directory inside a docker file for the initial case when the image used in the <code>FROM</code> command doesn't exist or was deleted?</p>
  
  <p>Here is my compose file:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>the projectx dockerfile looks like:</p>
  
  <pre><code>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 &amp;&amp; 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"]
  </code></pre>
  
  <p>The projectx-base dockerfile is  inside a sub folder (projectxBase) and looks like:</p>
  
  <pre><code>FROM php:5.5-apache
  MAINTAINER John
  
  RUN apt-get update &amp;&amp; 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 &amp;&amp; \
      docker-php-ext-configure mysql --with-mysql=mysqlnd &amp;&amp; \
      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
  </code></pre>
  
  <p>I realise I could put into one dockerfile but I would prefer to keep it abstracted out</p>