在获取django env安装程序时,正在研究如何容器化env。这样,我无法在Windows / Linux的Docker上使用该切入点。
Successfully built e9cb8e009d91
Successfully tagged avengervision_web:latest
avengervision_db_1 is up-to-date
Starting avengervision_web_1 ... done
CONTAINER ID IMAGE COMMAND CREATED
1da83169ba41 avengervision_web "sh /usr/src/app/ent…" 44 minutes
STATUS PORTS NAMES
Exited (2) 20 seconds ago avengervision_web_1
docker logs 1da83169ba41
sh: can't open '/usr/src/app/entrypoint.sh': No such file or directory
已经简化了entrypoint.sh以使其执行。
尝试过
ENTRYPOINT ["sh","/usr/src/app/entrypoint.sh"] &
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
确保以git和vscode结尾的行设置为LF,并通过dos2unix运行代码
在Windows和Linux上运行相同的Docker Compose,并在两者上获得相同的异常
作为额外的预防措施添加到Dockerfile中,以删除所有行尾,并确保chmod + x脚本
注释掉EntryPoint并运行docker run -tdi,我能够从容器内部docker附加并执行脚本。
***** docker-compose.yml *****
version: '3.7'
services:
web:
build:
context: .
dockerfile: ./docker/Dockerfile
#command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./main/:/usr/src/app/
ports:
- 8000:8000
environment:
- DEBUG=1
- SECRET_KEY=foo
- SQL_ENGINE=django.db.backends.postgresql
- SQL_DATABASE=hello_django_dev
- SQL_USER=hello_django
- SQL_PASSWORD=hello_django
- SQL_HOST=db
- SQL_PORT=5432
- DATABASE=postgres
depends_on:
- db
db:
image: postgres:11.2-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=hello_django
- POSTGRES_PASSWORD=hello_django
- POSTGRES_DB=hello_django_dev
volumes:
postgres_data:
***** Dockerfile *****
# pull official base image
FROM python:3.7-alpine
# set work directory
WORKDIR /usr/src/app
# set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
&& pip install psycopg2 \
&& apk del build-deps
# install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./docker/Pipfile /usr/src/app/Pipfile
RUN pipenv install --skip-lock --system --dev
# copy entrypoint.sh
COPY ./docker/entrypoint.sh /usr/src/app/entrypoint.sh
#RUN chmod +x /usr/src/app/entrypoint.sh
# copy project
COPY main /usr/src/app/main
COPY manage.py /usr/src/app
#RUN /usr/src/app/entrypoint.sh
RUN sed -i 's/\r$//' /usr/src/app/entrypoint.sh && \
chmod +x /usr/src/app/entrypoint.sh
# run entrypoint.sh
ENTRYPOINT ["sh","/usr/src/app/entrypoint.sh"]
***** entrypoint.sh *****
#!/bin/sh
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
echo "Testing"
#python /usr/src/app/manage.py flush
#python /usr/src/app/manage.py migrate
#python /usr/src/app/manage.py collectstatic --no-input --clear
exec "$@"
最终目标是容器将在创建的django应用程序启动后运行。
答案 0 :(得分:0)
利用此处列出的布局-https://github.com/testdrivenio/django-on-docker可以正常工作。我所做的区别是,我在根目录下创建了一个新的docker目录,然后让docker进行利用。一切似乎都按预期复制到了容器中,但是由于某种原因,EntryPoint无法正常工作。除了更新对新文件位置的引用以外,无需更改任何代码,一切正常。以下是所做的更改:
web:
build:
context: .
dockerfile: ./docker/Dockerfile
到
web:
build: ./app
,然后从更改目录结构
Project Layout:
├───.vscode
├───docker
│ └───Dockerfile
│ └───entrypoint.sh
│ └───Pipfile
│ └───nginx
└───main
├───migrations
├───static
│ └───images
├───templates
├───Artwork
├───django-env
│ ├───Include
│ ├───Lib
│ └───Scripts
└───docker-compose.yml
└───managy.py
到
Project Layout:
├───.vscode
├───app
│ └───main
│ ├───migrations
│ ├───static
│ │ └───images
│ ├───templates
│ └───Dockerfile
│ └───entrypoint.sh
│ └───managy.py
│ └───Pipfile
├───Artwork
├───django-env
│ ├───Include
│ ├───Lib
│ └───Scripts
└───nginx
└───docker-compose.yml