Sample method for Docker to deploy the Django project
- 2020-12-26 06:04:53
- OfStack
Using docker to deploy the django project is also easy
The environment
By default you have the docker environment installed
General structure of django project
(p3s) [root@opsweb]# tree opsweb
opsweb
├ ─ ─ apps
├ ─ ─ logs
├ ─ ─ manage.py
├ ─ ─ media
├ ─ ─ opsweb
├ ─ ─ README.md
├ ─ ─ requirements.txt
└ ─ ─ static
Write Dockerfile
This specifies that the Python version is officially available for docker
"0.0.0.0:8000" Here I open the container 8000 port
FROM python:3.6
RUN mkdir -p /usr/src/app
COPY pip.conf /root/.pip/pip.conf
COPY opsweb /usr/src/app/
COPY run_web.sh /usr/src/app/
RUN pip install -r /usr/src/app/requirements.txt
WORKDIR /usr/src/app
CMD [ "sh", "./run_web.sh"]
Write the pip file
I'm just going to use the mirror image pip install and I'm going to use the Ali cloud source a little bit faster
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
Full directory structure
[root@opsweb]# ls
Dockerfile opsweb pip.conf run_web.sh
build mirror
docker build -t ops:v0.1 .
Sending build context to Docker daemon 4.849 MB
Step 1 : FROM python:3.6
---> 3e4c2972dc8d
Step 2 : RUN mkdir -p /usr/src/app
---> Running in 8ddd929f5c18
---> 89dc0a1ffdf0
Removing intermediate container 8ddd929f5c18
Step 3 : COPY pip.conf /root/.pip/pip.conf
---> 5a4a165fed90
Removing intermediate container 37f3fdc7e5d2
Step 4 : COPY opsweb /usr/src/app/opsweb
---> f602e72ffd4c
Removing intermediate container 8d4bb616916d
Step 5 : COPY opsweb/requirements.txt /usr/src/app/
---> 6fe11a6fcbe0
Removing intermediate container faeadee32fed
Step 6 : RUN pip install -r /usr/src/app/requirements.txt
---> cc09c17d53da
Removing intermediate container d7b45bec6993
Step 7 : WORKDIR /usr/src/app
---> Running in c22dfdddbe81
---> c5c944b6df45
Removing intermediate container c22dfdddbe81
Step 8 : CMD python ./manage.py runserver 0.0.0.0:8000
---> Running in 29d5f0f53f6e
---> 10d37173fd13
Removing intermediate container 29d5f0f53f6e
Successfully built 10d37173fd13
conclusion
Then you can just start docker ES46en-p88:8000, ops: v0.1-ES50en, or you can deploy to k8s, which I won't go into details here.