本文源自於
https://dzone.com/articles/how-to-deploy-a-django-application-with-docker
只不過這里用自己的方式再簡述一遍。而且使用了較新的Python 和django 版本
因為現在大部分都是使用windows環境的,所以我就沒有使用linux或者文中所說的“Alibaba Cloud ECS Linux instance”
步驟:
1.安裝Docker windows版本
2.安裝python 及 django
3.創建django程序
4.編輯DockerfIle
# set the base image FROM python:3 # File Author / Maintainer MAINTAINER Esther #add project files to the usr/src/app folder ADD . /usr/src/app #set directoty where CMD will execute WORKDIR /usr/src/app COPY requirements.txt ./ # Get pip to download and install requirements: RUN pip install --no-cache-dir -r requirements.txt # Expose ports EXPOSE 8000 # default command to execute CMD exec gunicorn djangoapp.wsgi:application --bind 0.0.0.0:8000 --workers 3
5.編輯requirements.txt
#requirements.txt Django==2.1 gunicorn==19.9.0
最終生成的目錄結構如下:
7.在該目錄下,運行CMD命令。執行創建鏡像的命令
docker build -t django_application_image .
8.運行該容器
docker run -p 8000:8000 -i -t django_application_image
至此,容器已經成功運行起來了,並且服務端口已經映射到 8000 端口了。
至於退出容器,可以使用
Ctrl P + Q 退出容器,並讓容器在后台運行
如果想再停止該容器,可以執行
docker stop imageid