![](/img/trans.png)
[英]How to tie mysql database in one container to another container running my django blog app (with docker-compose)
[英]Docker-compose - How to populate a container data to another container?
我的 docker-compose 文件如下:
version: '3'
services:
database:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: qwerty
POSTGRES_USER: qwerty
backend:
depends_on:
- database
build:
#Dockerfile used here will use python image,build the django project and run using uwsgi in port 4000
context: .
dockerfile: dockerfile_uwsgi
ports:
- "4000:4000"
image: backend_img
environment:
DB_HOST: database
DB_NAME: qwerty
DB_USER: qwerty
DB_PASSWORD: qwerty
migration:
depends_on:
- backend
image: backend_img
entrypoint: ["sh", "-c"]
command: ["
python manage.py collectstatic --noinput;
python manage.py makemigrations;
python manage.py migrate;"]
environment:
DB_HOST: database
DB_NAME: qwerty
DB_USER: qwerty
DB_PASSWORD: qwerty
frontend:
depends_on:
- backend
build:
#The dockerfile used her uses nginx image, it is configured to act as reverse proxy and serve static files.
context: .
dockerfile: dockerfile_nginx
ports:
- "9443:8443"
Explaination about docker-compose.yaml
: Here the backend container setups the django project and serves the project using uwsgi, using same image the migration container will collect the static files from all the app directories and populates it into the current working directory of the container. 前端容器是一個 nginx,它充當反向代理。 我也想從 nginx 容器中提供 static 文件。
我在這里面臨的問題是,我希望遷移容器創建的 static 文件出現在前端容器中。 這樣 nginx 就可以為 static 文件提供服務。 這怎么可能做到? 如果假設設計不應該是這里顯示的方式,請建議我如何重新設計以達到要求?
我知道使用共享卷可以做到這一點。 但是我不想使用共享卷,因為填充到共享卷中的數據將保留在其中,並假設如果開發人員修改了應用文件夾中的 static 內容,則更改不會填充到卷中,除非卷掛載點被刷新. 這是基於我觀察到的,如果我錯了,請糾正我。
無論在 docker 層為您的資源提供什么服務 - gunicorn、uwsgi 等 - 都可能支持為 static 資產提供服務,並且可以比 django 本身更有效地提供服務。
在您的情況下, nginx
本質上是您的應用程序外部的。 與其嘗試“將您的 static 資產放入 nginx”,不如讓客戶端完成該工作並將它們緩存在 nginx 中。 Nginx 有很好的緩存支持。
If you really want to get the static assets into a, you can COPY --from=...
like in a https://docs.docker.com/develop/develop-images/multistage-build/ to copy the static assets進入您的自定義 nginx 容器。 使用 django 容器作為源 - 您必須確保它在您的 django 容器之后構建。 這在 docker-compose 中可能無法完全實現。 那里有合理的摩擦; 當/如果您嘗試構建 docker 工件並將其部署到生產服務器時,您將遇到相同的摩擦。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.