Docker image の作り方 (Django)

こちらと同じことを Django で行いました。
[Docker image の作り方 (Flask)](https://qiita.com/ekzemplaro/items/cc26ddeaa1bb00ba743a)

まず Django のプログラムを作成します。

```bash
django-admin startproject django01
cd django01/
python manage.py migrate
python manage.py startapp home
```

```py:home/view.py
from django.http import HttpResponse
from django.shortcuts import render
import random

def index(request):
	images = [
		"fmervo000001gsle.jpg",
		"rn2ola000000lk6e.jpg",
		"rn2ola000001gogf.jpg",
		"6fujishigai_s_s.jpg",
		"rn2ola000000lk6r.jpg",
		"5fujikawarakuza_s_s.jpg"
		]

	url_base = "https://www.city.fuji.shizuoka.jp/page/gazou/fmervo000001dsro-img/"

	url = url_base + random.choice(images)	
	dd = {
		'url': url,
		}

	return render(request, 'home/home.html', dd)
```

```html:home/templates/home/home.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
<title>富士山</title>
</head>
<body>
<div class="container">
<p>富士山 航空写真</p>
<blockquote>
<img src="{{url}}" />
</blockquote>
<p>Courtesy: <a href="https://www.city.fuji.shizuoka.jp/page/gazou/fmervo000001dsro.html">富士市</a></p>
</div>
Jul/2/2021 AM 11:29<br />
</body>
</html>
```

```py:django01/urls.py
from django.contrib import admin
from django.urls import path
from django.urls import include

urlpatterns = [
    path('', include('home.urls')),
    path('admin/', admin.site.urls),
]
uchida@iwata:~/projects/docker/django01$ gedit django01/urls.py
```

```py:django01/settings.py
（省略）
INSTALLED_APPS = [
    'home',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
（省略）
```

Django としての動作確認

```bash
./manage.py runserver
```

http://localhost:8000/ にアクセス

![django_aa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/2c1b9de5-4513-2ece-5d43-7f38184b1568.png)

次のファイルを作成
>Dockerfile
>requirements.txt

```text:Dockerfile
# our base image
FROM alpine:3.13

# Install python and pip
RUN apk add --update py3-pip

# install Python modules needed by the Python app
COPY requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r /usr/src/app/requirements.txt

# copy files required for the app to run
COPY manage.py /usr/src/app/
COPY db.sqlite3 /usr/src/app/
COPY home/ /usr/src/app/home/
COPY django01/ /usr/src/app/django01/

# tell the port number the container should expose
EXPOSE 8000

# run the application
CMD ["python3", "/usr/src/app/manage.py", "runserver", "0.0.0.0:8000"]
```

```text:requirements.txt
Django==3.2.5
```

フォルダー構造

```text
$ tree -L 1
.
├── Dockerfile
├── db.sqlite3
├── django01
├── home
├── manage.py
└── requirements.txt
```


Docker イメージの作成

```bash
docker build -t uchida/mydjangoapp .
```

```text
$ docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
uchida/mydjangoapp   latest    7bded2be2747   24 minutes ago   98.1MB
alpine               3.13      6dbb9cc54074   2 months ago     5.61MB
```


起動

```bash
docker run -p 8888:8000 --name mydjangoapp uchida/mydjangoapp
```

再起動

```bash
docker run -p 8888:8000 uchida/mydjangoapp
```

ブラウザーで
http://localhost:8888/
にアクセス
![docker_bb.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/ddd3fd4e-4528-b3ce-e129-2ce2b976b950.png)