R
R
Roman Dubinin2019-06-18 14:10:54
Docker
Roman Dubinin, 2019-06-18 14:10:54

Why can't I mount a folder from another container into a container?

Two containers are launched, nginx and php-fpm.
The sources (php files, assets, files uploaded by users) are located in php-fpm in the /app folder and nginx should see them.
The best thing I could dig up on the net was to make a shared volume and copy everything from /app to this volume when starting the php container. But, firstly, this is some kind of game, because the / app folder weighs dofiga (how seriously does someone live?), And secondly, nginx will not see the files uploaded by users.
Is there really no way to create a volume that will mount a folder not from the host, but from the container?
UPD Example.
First container:

docker build -t image -f Dockerfile .
docker run -d --name container1 -v app:/app image

Dockerfile
FROM alpine:3.9
WORKDIR /app
COPY index.html script.sh ./
CMD ["script.sh"]

index.html
<h1>Hello, Docker!</h1>

script.sh

Скрипт, который в бесконечном цикле создаёт и изменяет файлы и папки в текущей директории, может что-нибудь скачивает из интернета, синхронизируется с какими-нибудь сервисами, неважно, главное, что файлы в /app меняются.

Second container:
docker volume create shared
docker run -d --name container2 -p 80:80 -v shared:/usr/share/nginx/html:ro nginx:1.13

And that's why it's impossible to make the volume forwarded to container2 be created based on the folder in container1 and follow its changes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TyzhSysAdmin, 2019-06-18
@POS_troi

Take the data out of the container and you will be happy, well, at the same time, take care that you have two containers that do not decide to write to the same file at the same time.

Z
zzamzam, 2019-06-22
@Inlore

Stage 1. Stateless
Build one image for both nginx and php-fpm containers, in which you will have the application code in / app and the necessary packages will be installed. Here you should only have data that does not change while the application is running. You start both containers, just for one, the entry point will be nginx, and for the other, php-fpm. Together, they will take up disk space only according to the size of the image, tk. it will be the same readonly layer
Stage 2. Statefull
All data that changes while the application is running must live in a separate storage that will be connected to both containers somewhere in /app/storage, be it just a named volume, some kind of rbd ceph or s3 storage (in this case, nginx sends there simply through the locations defined for such data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question