docker_practice/compose/django.md

121 lines
3.9 KiB
Go
Raw Normal View History

# 使 Django
2015-01-13 18:04:53 +08:00
2019-03-08 12:21:07 +08:00
> `Python`
2017-11-23 15:48:52 +08:00
使 `Docker Compose` `Django/PostgreSQL`
2015-01-13 18:04:53 +08:00
2017-11-01 00:20:30 +08:00
Docker `Dockerfile` Docker
2015-01-13 18:04:53 +08:00
```docker
2017-11-23 15:48:52 +08:00
FROM python:3
2015-01-13 18:04:53 +08:00
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
2015-01-13 18:04:53 +08:00
RUN pip install -r requirements.txt
COPY . /code/
2015-01-13 18:04:53 +08:00
```
使 Python `Dockerfile` [ Dockerfile 使](../image/dockerfile/README.md)
2017-11-23 15:48:52 +08:00
`requirements.txt`
2015-01-13 18:04:53 +08:00
2017-11-22 11:13:23 +08:00
```bash
Django>=2.0,<3.0
psycopg2>=2.7,<3.0
2015-01-13 18:04:53 +08:00
```
2017-11-01 00:20:30 +08:00
`docker-compose.yml` 西 web 使 Docker
2015-01-13 18:04:53 +08:00
2017-11-01 00:20:30 +08:00
```yaml
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: 'postgres'
2017-11-23 15:48:52 +08:00
2017-11-01 00:20:30 +08:00
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
2017-11-01 00:20:30 +08:00
volumes:
- .:/code
ports:
- "8000:8000"
2015-01-13 18:04:53 +08:00
```
2017-11-23 15:48:52 +08:00
[`docker-compose.yml` ](compose_file.md)
2015-01-13 18:04:53 +08:00
2017-11-23 15:48:52 +08:00
使 `docker-compose run` `Django`
2015-01-13 18:04:53 +08:00
2017-11-22 11:13:23 +08:00
```bash
$ docker-compose run web django-admin startproject django_example .
2015-01-13 18:04:53 +08:00
```
2017-11-23 15:48:52 +08:00
web 使 Compose 使 `Dockerfile` web 使 `django-admin startproject django_example`
2015-01-13 18:04:53 +08:00
2017-11-26 09:54:04 +08:00
`Django`
2015-01-13 18:04:53 +08:00
2017-11-22 11:13:23 +08:00
```bash
2015-01-13 18:04:53 +08:00
$ ls
2017-05-24 11:13:57 +08:00
Dockerfile docker-compose.yml django_example manage.py requirements.txt
2015-01-13 18:04:53 +08:00
```
2017-11-23 15:48:52 +08:00
Linux,
2015-01-13 18:04:53 +08:00
2017-11-22 11:13:23 +08:00
```bash
$ sudo chown -R $USER:$USER .
2017-11-23 15:48:52 +08:00
```
`django_example/settings.py` `DATABASES = ...`
```python
2015-01-13 18:04:53 +08:00
DATABASES = {
'default': {
2017-11-23 15:48:52 +08:00
'ENGINE': 'django.db.backends.postgresql',
2015-01-13 18:04:53 +08:00
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
'PASSWORD': 'postgres',
2015-01-13 18:04:53 +08:00
}
}
```
2017-11-23 15:48:52 +08:00
2018-12-14 19:08:14 +08:00
[postgres](https://hub.docker.com/_/postgres/) 镜像固定设置好的。然后,运行 `docker-compose up`
2017-11-01 00:20:30 +08:00
2017-11-22 11:13:23 +08:00
```bash
2017-11-23 15:48:52 +08:00
$ docker-compose up
django_db_1 is up-to-date
Creating django_web_1 ...
Creating django_web_1 ... done
Attaching to django_db_1, django_web_1
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 |
web_1 | November 23, 2017 - 06:21:19
web_1 | Django version 1.11.7, using settings 'django_example.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
2015-01-13 18:04:53 +08:00
```
2015-01-14 10:03:47 +08:00
2017-11-26 09:54:04 +08:00
`Django` Docker `8000` `127.0.0.1:8000` `Django`
2017-11-23 15:48:52 +08:00
Docker `docker-compose up`
2015-01-14 10:03:47 +08:00
2017-11-22 11:13:23 +08:00
```bash
2016-11-25 22:24:48 +08:00
$ docker-compose run web python manage.py syncdb
```