1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker image の作り方 (Django)

Last updated at Posted at 2021-07-02

こちらと同じことを Django で行いました。
Docker image の作り方 (Flask)

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

django-admin startproject django01
cd django01/
python manage.py migrate
python manage.py startapp home
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)
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>
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
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 としての動作確認

./manage.py runserver

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

django_aa.png

次のファイルを作成

Dockerfile
requirements.txt

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"]
requirements.txt
Django==3.2.5

フォルダー構造

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

Docker イメージの作成

docker build -t uchida/mydjangoapp .
$ 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

起動

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

再起動

docker run -p 8888:8000 uchida/mydjangoapp

ブラウザーで
http://localhost:8888/
にアクセス
docker_bb.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?