1

this is my scenario, I have a Laravel 8 application that I'm trying to deploy using AWS ECS through AWS Fargate.

This is the useful part of my dockerfile:

### ... Previous composer and npm stages

FROM php:8.0-cli-alpine

# Concatenated RUN commands
RUN apk add --no-cache .........

RUN mkdir -p /run/apache2 \
    && rm  -rf /tmp/*

# Apache configuration
COPY docker/fargate/apache.conf /etc/apache2/conf.d

# PHP configuration
COPY docker/fargate/php.ini /etc/php8/conf.d

# Script permission
ADD docker/fargate/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

# Copy files from NPM
WORKDIR /var/www/html
COPY --from=build-npm /var/www/html /var/www/html

RUN chmod -R 777 /var/www/html
COPY docker/fargate/.env.fargate /var/www/html/.env

# Run script
EXPOSE 80
ENTRYPOINT  ["/docker-entrypoint.sh"]

And this is the docker-entrypoint.sh file

#!/bin/sh

# Cache
php artisan route:cache --quiet
php artisan config:cache --quiet

# Permissions
# On Alpine Apache's user and groups are apache
chown -R apache:apache /var/www/html
chgrp -R apache /var/www/html/bootstrap/cache
chmod -R ug+rwx /var/www/html/bootstrap/cache

# Launch the httpd in foreground
rm -rf /run/apache2/* || true && /usr/sbin/httpd -DFOREGROUND

Now, the deployed application works fine and I can browse my application.

But, I would like to mount a persistent volume at "/var/www/html" to be able to persist web sessions and other data. So, the volume exists but only seems to be mounted before the entrypoint command ran and, in this way, all the previous copied file vanish.

I tried adding these lines into the entrypoint script as first lines:

mkdir -p /var/www/html/bootstrap/cache
mkdir -p /var/www/html/public

It creates the folder correctly, so it confirms that the volume is mounted after the entrypoint starts.

Which is the correct way to achieve a goal like the one described here? I would like to persist data and overwrite them when container starts, if needed.

SamDroid
  • 111
  • 2

1 Answers1

2

AWS Fargate does not persist local storage. Even if you configure your task to use volumes created on the instance, the life cycle of the instance is tied to the life cycle of the task so there is no point in persisting it (on local storage). The way you do this is by persisting data on an EFS share (ECS/Fargate integrates with EFS natively). This is blog 1 of a series of 3 that introduces the use cases and walk you through an example of how to set this up. Hope this helps.

mreferre
  • 426
  • 1
  • 5