docker-compose up 重新构建不更新代码?
資深大佬 : danshendog 1
问题是这样的,我用了 docker-compose 打包一个 Django Web 项目,分为两个容器:nginx 和 django(包含 uWSGI 和 django),第一次使用命令docker-compose up将项目构建好跑起来了,后来重新修改了一下 uWSGI 的配置文件 uwsgi.ini, 改了一些其中的配置,也修改了一些 Django 项目中的 Python 代码,然后执行docker-compose build和docker-compose up重新构建,但是构建完了之后运行还是老样子,和第一次构建起来的程序一样,没有发生任何修改。 使用docker exec -it bash进入 django 容器之后发现 uwsgi.ini 和修改过的 python 代码都没有改变,反复尝试过了下面几种方法:
- 删除 docker 中所有相关的容器、镜像,重新构建,还是无效,代码和配置文件还是没变
- 使用
docker-compose build --no-cache重新构建,还是没变 - 使用
docker system prune -a删除所有 docker 镜像之后再重新构建,还是没变 - 使用
docker-compose up --force-recreate重新构建,还是没变
折腾了一个晚上,真的是要哭了。 附上我的两个 Dockerfile 和 docker-compose.yml:
version: '3' services: nginx: container_name: nginx-container build: ./nginx restart: always ports: - "80:80" - "8080:8080" volumes: - ./nginx_log:/var/log/nginx - shared_data:/django_project depends_on: - django privileged: true networks: net-django: ipv4_address: 10.127.2.3 django: container_name: django-container build: ./django_project restart: always command: uwsgi --ini uwsgi_conf.ini ports: - "8111:8111" volumes: - ./uwsgi_log:/django_project/logs - shared_data:/django_project privileged: true networks: net-django: ipv4_address: 10.127.2.2 volumes: shared_data: networks: net-django: ipam: config: - subnet: 10.127.2.0/24
django 的 Dockerfile:
FROM python:3 ENV PYTHONUNBUFFERED 1 ENV DJANGO_SECRET_KEY xxx RUN mkdir /django_project WORKDIR /django_project ADD . /django_project RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt EXPOSE 8111
nginx 的 Dockerfile
FROM nginx:latest COPY nginx.conf /etc/nginx/nginx.conf COPY django_nginx.conf /etc/nginx/sites-available/ RUN mkdir -p /etc/nginx/sites-enabled/ && ln -s /etc/nginx/sites-available/django_nginx.conf /etc/nginx/sites-enabled/ CMD ["nginx", "-g", "daemon off;"]
按理说重新构建的话会将修改过的代码和配置文件通过ADD . /django_project重新放入容器啊,为什么就是不变呢? 有大佬赐教吗?
大佬有話說 (13)