Ale*_*ran 4 django docker docker-compose
有这个 dockerfile:
FROM python:3.8.3-alpine
ENV MICRO_SERVICE=/home/app/microservice
# RUN addgroup -S $APP_USER && adduser -S $APP_USER -G $APP_USER
# set work directory
RUN mkdir -p $MICRO_SERVICE
RUN mkdir -p $MICRO_SERVICE/static
# where the code lives
WORKDIR $MICRO_SERVICE
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev gcc python3-dev musl-dev \
&& apk del build-deps \
&& apk --no-cache add musl-dev linux-headers g++
# install dependencies
RUN pip install --upgrade pip
# copy project
COPY . $MICRO_SERVICE
RUN pip install -r requirements.txt
COPY ./entrypoint.sh $MICRO_SERVICE
CMD ["/bin/bash", "/home/app/microservice/entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)
以及以下 docker-compose.yml 文件:
version: "3.7"
services:
nginx:
build: ./nginx
ports:
- 1300:80
volumes:
- static_volume:/home/app/microservice/static
depends_on:
- web
restart: "on-failure"
web:
build: . #build the image for the web service from the dockerfile in parent directory
command: sh -c "python manage.py collectstatic --no-input &&
gunicorn djsr.wsgi:application --bind 0.0.0.0:${APP_PORT}"
volumes:
- .:/microservice:rw # map data and files from parent directory in host to microservice directory in docker containe
- static_volume:/home/app/microservice/static
env_file:
- .env
image: wevbapp
expose:
- ${APP_PORT}
restart: "on-failure"
volumes:
static_volume:
Run Code Online (Sandbox Code Playgroud)
我需要引用其他目录而不是 .devcontainer 中的以下文件(在 docker-compose.yml 文件中):
这是我的文件夹结构:
一个简单的解决方案是将dockerfile、docker-compose.yml和.env移动到 django 目录djsr中,但我试图保持文件结构像这样。如何在docker-compose.yml中引用这些文件?
Dav*_*aze 17
将几个与 Docker 相关的文件放在项目根目录中是相当常见的,这可能会为您省去一些麻烦;我建议将其作为首选。
如果您确实想将其全部保存在子目录中,那么这是可能的。运行时docker-compose,您可以指定配置文件的位置。它将认为所有路径都是相对于该文件的目录的。
# Either:
docker-compose -f .devcontainer/docker-compose.yml up
cd .devcontainer && docker-compose up
Run Code Online (Sandbox Code Playgroud)
当您构建映像时,构建会读取上下文目录,并且COPY语句始终相对于该目录进行解释。对于您的设置,您需要将上下文目录置于源代码树的顶部,然后在子目录中指定备用 Dockerfile。
services:
web:
build:
context: ..
dockerfile: .dockerenv/Dockerfile
Run Code Online (Sandbox Code Playgroud)
大多数情况下,Dockerfile 本身没问题,但如果入口点脚本位于子目录中,则COPY命令也需要反映这一点。由于您要复制整个源目录,因此您还可以将图像内的内容重新排列为您想要的布局。
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
# Either:
COPY .dockerenv/entrypoint.sh ./
# Or:
RUN mv .dockerenv/entrypoint.sh .
# Or:
CMD ["./dockerenv/entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)
我不推荐您拥有的卷结构,但如果您想保留它,您还需要将绑定挂载的源路径更改为父目录。(特别注意,在前面的 Dockerfile 片段中,有几个选项涉及在映像内移动文件,并且绑定挂载将隐藏该更改。)
services:
web:
volumes:
# Ignore the application built into the container, and use
# whatever's checked out on the host system instead.
- ..:/home/app/microservice
# Further ignore the static assets on the host system and
# use the content in a named volume instead.
- static_volume:/home/app/microservice/static
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30247 次 |
| 最近记录: |