Bellots
Bellots

Reputation: 1773

Routes not updating inside my Laravel Container

I've got this docker-compose:

version: '3'
services:
  app:
    build:
      context: .
      dockerfile: .docker/Dockerfile
    image: laravel-docker
    ports:
      - 8080:80
    volumes:
      - ./:/var/www
    links:
      - mysql
      - redis
    environment:
      DB_HOST: mysql
      DB_DATABASE: laravel_docker
      DB_USERNAME: app
      DB_PASSWORD: password
      REDIS_HOST: redis
      SESSION_DRIVER: redis
      CACHE_DRIVER: redis
  mysql:
    image: mysql:5.7
    ports:
      - 13306:3306
    environment:
      MYSQL_DATABASE: laravel_docker
      MYSQL_USER: app
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
  redis:
    image: redis:4.0-alpine
    ports:
      - 16379:6379

and this Dockerfile:

FROM php:7.1.8-apache

COPY . /srv/app
COPY .docker/vhost.conf /etc/apache2/sites-available/000-default.conf

WORKDIR /srv/app


RUN docker-php-ext-install mbstring pdo pdo_mysql \
    && chown -R www-data:www-data /srv/app
RUN a2enmod rewrite

which is my configuration to run a Laravel container with MySQL and Redis. Everything works perfectly, but I'm encountering problems when I try to add (or update) a new route: it doesn't appear until I don't stop all containers and restart them with --build tag.

Is there a way to add and update routes without restart my containers?

Upvotes: 0

Views: 3095

Answers (3)

Tarnay Kálmán
Tarnay Kálmán

Reputation: 7066

Based on the Dockerfile your app lives at /srv/app, yet in the yml file you list /var/www as the mount target. Change that to /srv/app

Explanation: Building the Dockerfile results in an immutable image. The software inside the image was configured to serve your application from /srv/app. Since COPY . /srv/app added your app to the image at the right location, it could be served from there just fine, but that command adds it when the image is built, and then it becomes an immutable part of the image, so the changes you make on the host are not going to be visible inside. What you want to do is bind mounting your project directory to /srv/app, and that will obscure (temporarily "replace") the contents of that directory with the one on your host, and this is what that yml line does. (Btw the fact that mounts obscure the existing directory is not docker-specific.)

https://docs.docker.com/storage/bind-mounts/#mounting-into-a-non-empty-directory-on-the-container

The reason why we often both COPY and bind mount our project directories is that this practice allows us to use the same Dockerfile for both development (without frequent image rebuilds) and production.

Upvotes: 1

Michael Miller
Michael Miller

Reputation: 399

I'd be curious to know if your change is properly propagating to your volume. It could be a permissions issue inside the container. What happens if you connect to the container and "cat" the contents of the routes file? Does it match the file outside the container? What OS are you running docker on? How locked down is the OS's file system? Are there any constraints that would make volumes work funky? Also, what file system sync process are you using? Are you just using the default?

Upvotes: 0

whmkr
whmkr

Reputation: 3085

ssh to the app container and from the project directory run this command:

php artisan route:clear

Upvotes: 2

Related Questions