简体   繁体   中英

Sharing a folder between Docker-Containers with precompiled files

I use Github Actions to make a php-fpm Docker Container with Laravel and precompiled Docker Compose Files in it. The CSS and JS files are made with npm. This Docker Container is than pushed to my Docker Regestry.

I want to check this Container out and let it rund with Docker-Compose and a nginx container runs with this Configuration:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

So the PHP Part runs perfectly, but the CSS and JS part is not working, because the app Container has this files. How can i copy, share or something else between this two Containers?

You cant put in a Volume from the host, because the Volume will only contain, whats on the Host, and not whats in the Container... it has to be to the other Direction.

You should use a named volume which will perfectly fit your needs

volumes:
  assets:

services:
  nginx:
    ...
    volumes:
      - assets:/var/www/public/:ro

  app:
    ...
    volumes:
      - assets:/your/npm/output/folder

Your JS/CSS files will be properly shared between your 2 containers

You have to define a docker volume in docker built-time, set then your precompiled files, and after that mount this named volume to both systems.

The problem is that you're trying to mount something that is already in a docker image, not a docker volume. There's no way to do that in docker run-time. That's not the philosophy.

In summary, you're doing ( wrongly ):

  1. Create docker build image1 with precompiled files in /your_dir
  2. As you correctly said, you cannot mount from your host a dir to docker-image1:/your_dir because it'd override it.
  3. There's no elegant way to access from docker-image2 to docker-image1 .

Good practice:

  1. Create docker NAMED volume, called myvolume , for example.
  2. Create docker build image1 , which saves your precompiled in myvolume
  3. Create container docker run -t image1... yourCssDocker mounting myvolume
  4. Create container docker run -t image2... yourNginxDocker , mounting myvolume

That's the way to proceed. Note that you should access myvolume by its name, not by a bind volume (host path).

You can do all steps (4 from good practice) using just 1 docker-compose.yml file (using css-precomp as named docker volume name)

volumes:
  css-precomp:

services:
  my-service:
    build:
      - context: .
      - dockerfile: Dockerfile
    volumes:
      - css-precomp:/your_dir
    ...

Finally, you can check your data are correctly generated after building in Dockerfile, checking /var/lib/docker/volumes/css-precomp/_data

The Dockerfile COPY instruction lets you copy files from one image to another. If you've already built your PHP application image, you can build an nginx image with the static files from it like:

FROM nginx:latest
COPY --from=my/php-app /usr/src/app /usr/share/nginx/html
COPY nginx.conf /etc/nginx

You will now have two self-contained images. If the content in the PHP image changes then you can rebuild the nginx image to get updated content there. (If you use named volumes as other questions suggest, you will have to manually delete the volumes holding the static assets to update their contents.) This setup will also work well in more advanced setups where you have multiple copies of each component possibly running on different systems (Docker Swarm, Amazon ECS, Kubernetes).

If it's plausible to serve the static assets directly from the backend service, and use nginx only as a reverse proxy, that simplifies this deployment sequence significantly. That may not be possible with a PHP-FPM setup, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM