环境信息
- MacBook Air(蒙特雷)
- Docker Compose 版本 v2.6.1
介绍
Wai “最近我只接触了 Next.js,我想使用另一种语言。”
Wai:“看看吧!我很久以前就做了一个机器人,碰过Python!”
” Wai “嗯,你可以用 Django 来创建一个 web 应用程序,我们来试试吧。”
环境
Wai ``我不喜欢在本地构建环境 - 你想使用 docker 吗?''
怀“哦,是的!”
Wai "做好目录,首先Dockerfile"
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
Wai:那么,你要制作requirements.txt吗?
卫“这是什么?”
? ? ? “类似于 package.json 的东西”
伟“我明白了”
Django>=1.8,<2.0
psycopg2
最后docker-compose.ymlYana
version: '3'
services:
db:
image: postgres
ports:
- "5432"
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Wai “是 db postgres 吗?”
Wai “你想试试开机吗?”
Wai:“咦,这是命令吗?”
docker-compose run web django-admin.py startproject composeexample .
Traceback (most recent call last):
File "/usr/local/bin/django-admin.py", line 2, in <module>
from django.core import management
File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 13, in <module>
from django.core.management.base import (
File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 17, in <module>
from django.db.migrations.exceptions import MigrationSchemaMissing
File "/usr/local/lib/python3.10/site-packages/django/db/migrations/__init__.py", line 2, in <module>
from .operations import * # NOQA
File "/usr/local/lib/python3.10/site-packages/django/db/migrations/operations/__init__.py", line 1, in <module>
from .fields import AddField, AlterField, RemoveField, RenameField
File "/usr/local/lib/python3.10/site-packages/django/db/migrations/operations/fields.py", line 4, in <module>
from django.db.models.fields import NOT_PROVIDED
File "/usr/local/lib/python3.10/site-packages/django/db/models/__init__.py", line 5, in <module>
from django.db.models.deletion import (
File "/usr/local/lib/python3.10/site-packages/django/db/models/deletion.py", line 5, in <module>
from django.db.models import signals, sql
File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/__init__.py", line 2, in <module>
from django.db.models.sql.query import * # NOQA
File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 11, in <module>
from collections import Counter, Iterator, Mapping, OrderedDict
ImportError: cannot import name 'Iterator' from 'collections' (/usr/local/lib/python3.10/collections/__init__.py)
” Wai “嗯?你报错了。”
Wai:“什么,为什么不能导入?”
ImportError:无法从 Python 中的“集合”导入名称“Iterable”
Wai:“嗯,python3.10删了什么?”
Wai “如果我这样做,我应该降级 Python 版本吗?”
FROM python:3.9-buster // ここを変更
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD ./requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
Wai “嘿,让我们重建再试一次!”
docker-compose build
docker-compose run web django-admin.py startproject composeexample .
ls -l
total 32
-rw-r--r-- 1 user staff 155 8 5 12:17 Dockerfile
drwxr-xr-x 6 user staff 192 8 5 12:19 composeexample/
-rw-r--r-- 1 user staff 211 8 5 11:45 docker-compose.yml
-rwxr-xr-x 1 user staff 812 8 5 12:19 manage.py*
-rw-r--r-- 1 user staff 26 8 5 11:46 requirements.txt
Wai:“哦,看起来成功了。”
数据库连接设置
Wai:“看来我接下来要写DB设置了。”
vi composeexample/settings.py
// 編集画面に移行後
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
卫“这很好”
启动
Wai “你想试试开机吗?”
docker-compose up
[+] Running 2/2
⠿ Container django_cardlayout-db-1 Created 0.0s
⠿ Container django_cardlayout-web-1 Recreated 0.2s
Attaching to django_cardlayout-db-1, django_cardlayout-web-1
django_cardlayout-db-1 |
django_cardlayout-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
django_cardlayout-db-1 |
django_cardlayout-db-1 | 2022-08-05 03:35:16.376 UTC [1] LOG: starting PostgreSQL 14.4 (Debian 14.4-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
django_cardlayout-db-1 | 2022-08-05 03:35:16.386 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
django_cardlayout-db-1 | 2022-08-05 03:35:16.386 UTC [1] LOG: listening on IPv6 address "::", port 5432
django_cardlayout-db-1 | 2022-08-05 03:35:16.390 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
django_cardlayout-db-1 | 2022-08-05 03:35:16.399 UTC [27] LOG: database system was shut down at 2022-08-05 03:34:24 UTC
django_cardlayout-db-1 | 2022-08-05 03:35:16.409 UTC [1] LOG: database system is ready to accept connections
django_cardlayout-web-1 | Performing system checks...
django_cardlayout-web-1 |
django_cardlayout-web-1 | System check identified no issues (0 silenced).
django_cardlayout-web-1 |
django_cardlayout-web-1 | You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
django_cardlayout-web-1 | Run 'python manage.py migrate' to apply them.
django_cardlayout-web-1 | August 05, 2022 - 03:35:17
django_cardlayout-web-1 | Django version 1.11.29, using settings 'composeexample.settings'
django_cardlayout-web-1 | Starting development server at http://0.0.0.0:8000/
django_cardlayout-web-1 | Quit the server with CONTROL-C.
django_cardlayout-web-1 | [05/Aug/2022 03:35:24] "GET / HTTP/1.1" 200 1716
django_cardlayout-web-1 | Not Found: /favicon.ico
django_cardlayout-web-1 | [05/Aug/2022 03:35:24] "GET /favicon.ico HTTP/1.1" 404 1970
围“Eekanjiyan”
Wai:“你想访问8000号吗?”
哇,打开你的浏览器本地主机:8000什么时候”
怀“是”
Wai "结尾是Ctrl + CYana"
在最后
这是一篇旧文章,但感谢您阅读本文。
我选择了这种风格,是为了以后看的时候不会看腻。我向所有阅读困难的人道歉。
本文涵盖了从构建环境到检查启动的所有内容。下次我们计划再深入一点。
另外,由于我是在自己学习的同时创作文章,所以不定期更新。不知道有没有人读得最多。
下次见。
原创声明:本文系作者授权爱码网发表,未经许可,不得转载;
原文地址:https://www.likecs.com/show-308622537.html