Django框架

创建django-pg项目目录

[root@docker ~]# mkdir docker-compose-django
[root@docker ~]# cd docker-compose-django/
[root@docker docker-compose-django]# mkdir django-pg

在项目目录下创建docker-compose.yml文件

  • 该文件定义了两个服务,一个是名为db的Postgres数据库,另一个是名为web的Django应用程序
#进入django-pg根目录,配置docker-compose.yml,注意选项和参数之间的空格,不能用Tab键,只能用空格键
#选项与选项之间有层级关系,注意对齐
[root@docker ~]# cd docker-compose-django/django-pg
[root@docker django-pg]# vim docker-compose.yml 

version: '3'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - db_data:/var/lib/postgresql
  web:
   build: .
   command: python manage.py runserver 0.0.0.0:8000
   volumes:
     - .:/code
   ports:
     - "8000:8000"
   depends_on:
     - db
volumes:
  db_data: {}
  
#在该目录下使用docker-compose config命令以此来检查配置文件参数是否有误,未报错即可
[root@docker django-pg]# docker-compose config
name: django-pg
services:
  db:
    environment:
      POSTGRES_DB: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
    image: postgres
    networks:
      default: null
    volumes:
    - type: volume
      source: db_data
      target: /var/lib/postgresql
      volume: {}
  web:
    build:
      context: /root/docker-compose-django/django-pg
      dockerfile: Dockerfile
    command:
    - python
    - manage.py
    - runserver
    - 0.0.0.0:8000
    depends_on:
      db:
        condition: service_started
    networks:
      default: null
    ports:
    - mode: ingress
      target: 8000
      published: "8000"
      protocol: tcp
    volumes:
    - type: bind
      source: /root/docker-compose-django/django-pg
      target: /code
      bind:
        create_host_path: true
networks:
  default:
    name: django-pg_default
volumes:
  db_data:
    name: django-pg_db_data

在项目目录下创建requirements.txt文件

  • requirements.txt文件用于记录所有依赖包及其精确的版本号,隶属于Python
#进入django-pg根目录,配置requirements.txt
[root@docker ~]# cd docker-compose-django/django-pg
[root@docker django-pg]# vim requirements.txt 

Django>=2.0,<3.0
psycopg2>=2.7,<3.0

在项目目录下创建Dockerfile文件

#进入django-pg根目录,配置Dockerfile
[root@docker ~]# cd docker-compose-django/django-pg
[root@docker django-pg]# vim Dockerfile 

#从Python 3父镜像开始
FROM python:3
ENV PYTHONUNBUFFERED 1
#在镜像中添加code项目
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
#在镜像中安装由requirements.txt文件指定要安装的Python依赖
RUN pip install -r requirements.txt
COPY . /code/

在项目目录下创建Django项目,构建上下文

#进入django-pg根目录,执行docker-compose run命令
[root@docker ~]# cd docker-compose-django/django-pg
[root@docker django-pg]# docker-compose run web django-admin startproject myexample .

.....(过程略)

#此时会去拉取镜像进行构建

查看服务列表

[root@docker django-pg]# docker-compose ps
NAME                COMMAND                  SERVICE             STATUS              PORTS
django-pg-db-1      "docker-entrypoint.s…"   db                  running             5432/tcp

#状态为runnning,端口5432开放即可

设置Django数据库连接

  • 以下配置参数是由docker-compose.yml文件所指定的postgres镜像所决定
#执行完docker-conmpose构建后在目录下会生成两个文件
[root@docker django-pg]# ls -l
total 16
-rw-r--r-- 1 root root 385 Jun  5 10:16 docker-compose.yml
-rw-r--r-- 1 root root 152 May 30 14:22 Dockerfile
-rwxr-xr-x 1 root root 629 Jun  5 10:32 manage.py
drwxr-xr-x 3 root root  93 Jun  5 10:57 myexample
-rw-r--r-- 1 root root  36 May 30 14:14 requirements.txt

#编辑myexample/settings.py文件,修改‘DATABASES’选项参数和‘ALLOWED_HOSTS’选项参数
[root@docker django-pg]# vim myexample/settings.py 

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

ALLOWED_HOSTS = ["192.168.88.101"]			#这里设置为你自己主机的IP,要用双引号引起来

启动Django应用程序

#在django-pg根目录下执行docker-compose up命令
[root@docker django-pg]# docker-compose up
[+] Running 2/0
 ⠿ Container django-pg-db-1   Created                                                                0.0s
 ⠿ Container django-pg-web-1  Created                                                                0.0s
Attaching to django-pg-db-1, django-pg-web-1
django-pg-db-1   | 
django-pg-db-1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
django-pg-db-1   | 
django-pg-db-1   | 2023-06-05 03:34:41.145 UTC [1] LOG:  starting PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
django-pg-db-1   | 2023-06-05 03:34:41.146 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
django-pg-db-1   | 2023-06-05 03:34:41.146 UTC [1] LOG:  listening on IPv6 address "::", port 5432
django-pg-db-1   | 2023-06-05 03:34:41.148 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
django-pg-db-1   | 2023-06-05 03:34:41.152 UTC [28] LOG:  database system was shut down at 2023-06-05 02:59:33 UTC
django-pg-db-1   | 2023-06-05 03:34:41.155 UTC [1] LOG:  database system is ready to accept connections
django-pg-web-1  | Watching for file changes with StatReloader
django-pg-web-1  | Performing system checks...
django-pg-web-1  | 
django-pg-web-1  | System check identified no issues (0 silenced).
django-pg-web-1  | 
django-pg-web-1  | You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
django-pg-web-1  | Run 'python manage.py migrate' to apply them.
django-pg-web-1  | June 05, 2023 - 03:34:42
django-pg-web-1  | Django version 2.2.28, using settings 'myexample.settings'
django-pg-web-1  | Starting development server at http://0.0.0.0:8000/
django-pg-web-1  | Quit the server with CONTROL-C.

#注意这里不要ctrl C掐掉服务进程了,直接去浏览器访问

再次查看服务列表

#另启一个终端查看服务,#进入django-pg根目录
[root@docker ~]# cd docker-compose-django/django-pg
[root@docker django-pg]# docker-compose ps
NAME                COMMAND                  SERVICE             STATUS              PORTS
django-pg-db-1      "docker-entrypoint.s…"   db                  running             5432/tcp
django-pg-web-1     "python manage.py ru…"   web                 running             0.0.0.0:8000->8000/tcp, :::8000->8000/tcp

#8000端口已映射,状态running

web访问Django主页

http://192.168.88.101:8000

注意这里的IP是'ALLOWED_HOSTS‘参数设置的本机IP

Docker运行Django框架-LMLPHP

关闭并清理服务

#先ctrl c掐掉进程,然后删除服务与其对应的卷,最后删除项目目录即可
[root@docker django-pg]# docker-compose down --volumes

关于Docker容器运行Django框架时的问题

1,docker-compose.yml文件中关于db的部分配置应是这样:

db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - db_data:/var/lib/postgresql

上述需设置环境,指定用户名,密码和数据库,这是为了解决django-pg-db-1容器无法正常运行,启动自动退出的问题


2,与此对应的myexample/settings.py文件中DATABASES配置应如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}


3,值得注意的一点,在myexample/settings.py文件中需设置网页访问地址IP参数否则浏览器无法正常访问django页面:

ALLOWED_HOSTS = []		#这个选项中括号里面填入Docker主机IP即可,注意要用双引号引起来
06-05 12:12