HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Use a volume or copy my artefacts into docker image

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
imagedockerintoartefactsusevolumecopy

Problem

I want to dockerize my php application and deploy it in production. Here is my application structure :

api
|
+-- src
+-- config
+-- vendor
+-- tests
+-- public


I need to embed api/* in my container, and I'm confused about the best approach.
Here is mine :

-
A light multi-stage docker image (around 85 mb but can be more optimized)

FROM php:7.4-fpm-alpine AS base

WORKDIR /var/www/html
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN apk update && \
  docker-php-ext-install mysqli tokenizer pdo_mysql && \
  curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

FROM base AS dev

COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug-dev.ini
RUN apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS && \
  pecl install xdebug-2.9.1 && \
  docker-php-ext-enable xdebug && \
  touch /tmp/xdebug.log && \
  chmod -R 666 /tmp/xdebug.log && \
  apk del -f .phpize-deps


-
A mounted volume (docker-compose.yml)

api:
  volumes:
    - ./api:/var/www/html
  command: composer install && php-fpm;


I saw a lot of docker images using COPY .. instruction in Dockerfile instead of volumes. I believe COPY is great to copy small config files (or a small build for compiled languages), but it's not optimized to keep the images small (basic rule with docker).

Am I missing something?
For example, is it bad practice to handle volumes from outside the dev environment ?

Solution

To copy into the image or to mount volumes--my advice:

  • if the files will change infrequently, then copy them into the image, with the understanding that if they need to be modified, you will need to rebuild the image.



  • if you need to frequently change the files, or store some kind of state about the files (and re-use them with future container instances of the image), then mount them as volumes.

Context

StackExchange DevOps Q#12942, answer score: 3

Revisions (0)

No revisions yet.