【发布时间】:2021-05-13 18:41:47
【问题描述】:
我正在 dockerising 一个 django 应用程序 (webapp)。但是,当我进行代码更改时,它们似乎不会在重建容器时反映出来。
我正在尝试测试我是否可以点击“app”,但名称已更改为“newapp”。
testcase.py
from django.test import TestCase
import requests
class TestServerUp(TestCase):
def testhitendpoint(self):
r = requests.get(host='app', port=1234)
self.assertEquals(r.status_code, 200)
运行命令 sudo docker exec -it webapp python manage.py test tests.testcase 失败,因为主机应该是 'newapp'
requests.exceptions.ConnectionError: HTTPConnectionPool(host='app', port=1234): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd97d430550>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
我把测试用例改成:
from django.test import TestCase
import requests
class TestServerUp(TestCase):
def testhitendpoint(self):
r = requests.get(host='newapp', port=1234)
self.assertEquals(r.status_code, 200)
我把容器放下:
sudo docker-compose -f ./deploy/docker-compose.yaml down
我从 docker 中移除容器:
sudo docker system prune --all --volumes -f,然后是 sudo docker rm -f $(sudo docker ps -aq),只是为了确定。
我重建容器:
sudo docker-compose -f ./deploy/docker-compose.yaml build --no-cache
然后我将容器旋转起来:
sudo docker-compose -f ./deploy/docker-compose.yaml up --force-recreate -d
我再次运行测试:
sudo docker exec -it webapp python manage.py test tests.testcase
(或sudo docker exec -it [container-id] python manage.py test tests.testcase)
我得到完全相同的错误信息:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='app', port=1234): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd97d430550>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
查看容器 (sudo docker exec -it webapp /bin/bash)
运行cat tests/testcase.py 会显示第一个测试用例的原始代码(host='app'),而不是修改后的代码(host='newapp')。
部署/docker-compse.yaml
version: "3.0"
services:
db:
image: postgres
container_name: postgres_db
restart: always
ports:
- 5433:5432
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=$POSTGRES_DB
- POSTGRES_USER=$POSTGRES_USER
- POSTGRES_PASSWORD=$POSTGRES_PASSWORD
app:
build: ../webapp
container_name: webapp
entrypoint: /entrypoint.sh
ports:
- 8000:8000
volumes:
- app_data:/webapp
environment:
- DJANGO_SECRET_KEY=$DJANGO_SECRET_KEY
- ENVIRONMENT=$ENVIRONMENT
- POSTGRES_DB=$POSTGRES_DB
- POSTGRES_USER=$POSTGRES_USER
- POSTGRES_PASSWORD=$POSTGRES_PASSWORD
depends_on:
- db
volumes:
postgres_data:
app_data:
webapp/Dockerfile
FROM python:3.8.7-buster
ENV PYTHONUNBUFFERED=1
WORKDIR /webapp
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV USE_DOCKER=true
# install dependencies
RUN pip install --upgrade pip &&\
apt-get update &&\
apt-get install -y libpq-dev python3-dev
COPY requirements.txt /webapp/
RUN pip install -r requirements.txt
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
COPY . /webapp/
RUN mkdir /webapp/staticfiles
webapp/entrypoint.sh
#!/bin/bash
# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput
# Apply database migrations
echo "Apply database migrations"
python manage.py migrate --noinput
# Start server
echo "Starting server"
gunicorn webapp.wsgi:application --bind 0.0.0.0:8000
我认为 docker 要么从我没有删除的缓存中提取数据,要么正在针对旧容器运行“docker exec -it”。
任何我可能忽略的建议将不胜感激!
【问题讨论】:
-
尝试 docker-compose build 和 docker-compose up
-
我有类似的设置,我通常运行:
docker-compose up --force-recreate --always-recreate-deps -V --build
标签: python django docker docker-compose