目的
Djangoで作成したWebアプリをdocker-composeで起動する
事前にローカルデプロイ可能か確認
フォルダ構成
プロジェクトフォルダ構成は以下の通り
django-app/
├── db.sqlite3
├── Dockerfile
├── docker-compose.yml
├── manage.py
├── django_app/
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── django-app_params
└── venv
仮想環境作成して、Djangoインストール
まずはアプリフォルダ内(manage.pyがある階層)で仮想環境(myenv)を作成する。
その後仮想環境に入り、サーバを起動する。問題なくアクセス可能であることを確認
$ cd ~/django-app
$ python -m venv myenv
$ source venv/bin/activate
(venv) $ pip list django
Package Version
---------- -------
asgiref 3.6.0
Django 4.1.5
pip 22.0.2
setuptools 59.6.0
sqlparse 0.4.3
uWSGI 2.0.21
wheel 0.38.4
(venv) $ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
October 07, 2023 - 02:06:14
Django version 4.1.5, using settings 'django_app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[07/Oct/2023 02:16:41] "GET / HTTP/1.1" 200 10681
[07/Oct/2023 02:16:41] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[07/Oct/2023 02:16:41] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[07/Oct/2023 02:16:41] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[07/Oct/2023 02:16:41] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /favicon.ico
[07/Oct/2023 02:16:41] "GET /favicon.ico HTTP/1.1" 404 2114
docker-composeファイル作成
Dockerfileとdocker-compose.ymlをmanage.pyのあるアプリフォルダ内に作成する
$ cat Dockerfile
FROM python:3.8
RUN pip install django
# コンテナ内の作業ディレクトリを/appにする
WORKDIR /app
# ホスト側のカレントフォルダ(アプリのソースコード)をコンテナ内の/app配下にコピーする
COPY . .
CMD ["python","manage.py","runserver","0.0.0.0:8000"]
$ cat docker-compose.yml
version: '3.7'
services:
web:
#ビルドするための方法を定義
build:
#contextでdockerイメージをビルドするためのソースコードの場所を指定する
context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
volumes:
- .:/app
docker-composeビルド実行
Dockerfileを元にビルドする。イメージが作成される
$ docker-compose build
Building web
[+] Building 61.7s (9/9) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 152B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.8 2.4s
=> [internal] load build context 3.3s
=> => transferring context: 45.26MB 3.1s
=> [1/4] FROM docker.io/library/python:3.8@sha256:2e652376e0db17d97355ebfe7871736fb30d551ab164172157f7982ccb73c5d9 46.4s
=> => resolve docker.io/library/python:3.8@sha256:2e652376e0db17d97355ebfe7871736fb30d551ab164172157f7982ccb73c5d9 0.1s
=> => sha256:32280dd146dc84b459615d3b4c28597f5b299b9469cb9403a12abd245879a114 2.01kB / 2.01kB 0.0s
=> => sha256:debce5f9f3a9709885f7f2ad3cf41f036a3b57b406b27ba3a883928315787042 64.11MB / 64.11MB 14.5s
=> => sha256:2e652376e0db17d97355ebfe7871736fb30d551ab164172157f7982ccb73c5d9 1.86kB / 1.86kB 0.0s
=> => sha256:167b8a53ca4504bc6aa3182e336fa96f4ef76875d158c1933d3e2fa19c57e0c3 49.56MB / 49.56MB 17.1s
=> => sha256:b47a222d28fa95680198398973d0a29b82a968f03e7ef361cc8ded562e4d84a3 24.03MB / 24.03MB 6.4s
=> => sha256:3d2139d85ac7bb32a986818909221b5f8e7e34b8c517d21f9492cae9018838b5 7.56kB / 7.56kB 0.0s
=> => sha256:1d7ca7cd2e066ae77ac6284a9d027f72a31a02a18bfc2a249ef2e7b01074338b 211.04MB / 211.04MB 34.8s
=> => sha256:ff3119008f58beef8f336fa833707b0fe914db94ca6b7bb55abe3e1bf2b1ad56 6.39MB / 6.39MB 17.2s
=> => sha256:5ed97432ea99f277045d56b600e7b41464091407398c5cdba8ad69bffde82e33 17.28MB / 17.28MB 22.6s
=> => sha256:fbe0b21996cf78b9ecf92c6279b420145a88874c2c3ae32cfe2d7c4a8912abb5 243B / 243B 17.6s
=> => extracting sha256:167b8a53ca4504bc6aa3182e336fa96f4ef76875d158c1933d3e2fa19c57e0c3 2.8s
=> => sha256:a5d22cc5763378cf3d0330f0ef557fa32b2eef9054ae18d6f41ae7c29aae09f8 2.85MB / 2.85MB 19.7s
=> => extracting sha256:b47a222d28fa95680198398973d0a29b82a968f03e7ef361cc8ded562e4d84a3 1.0s
=> => extracting sha256:debce5f9f3a9709885f7f2ad3cf41f036a3b57b406b27ba3a883928315787042 4.3s
=> => extracting sha256:1d7ca7cd2e066ae77ac6284a9d027f72a31a02a18bfc2a249ef2e7b01074338b 9.3s
=> => extracting sha256:ff3119008f58beef8f336fa833707b0fe914db94ca6b7bb55abe3e1bf2b1ad56 0.5s
=> => extracting sha256:5ed97432ea99f277045d56b600e7b41464091407398c5cdba8ad69bffde82e33 0.7s
=> => extracting sha256:fbe0b21996cf78b9ecf92c6279b420145a88874c2c3ae32cfe2d7c4a8912abb5 0.0s
=> => extracting sha256:a5d22cc5763378cf3d0330f0ef557fa32b2eef9054ae18d6f41ae7c29aae09f8 0.2s
=> [2/4] RUN pip install django 8.7s
=> [3/4] WORKDIR /app 0.1s
=> [4/4] COPY . . 2.4s
=> exporting to image 1.5s
=> => exporting layers 1.4s
=> => writing image sha256:fbca17f5a264faf9352d644b70ecd1d176c98b3467aa7945fc4f63b68738fcfd 0.0s
=> => naming to docker.io/library/djangoapp 0.0s
(venv) $ docker image ls | grep django
djangoapp latest fbca17f5a264 9 minutes ago 1.08GB
docker-composeでコンテナ起動
docker-compose.ymlを元にコンテナを起動する
(venv) $ docker-compose up -d
Starting django-app_web_1 ... done
(venv) $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee61f4de97ab djangoapp "python manage.py ru…" 9 minutes ago Up 4 seconds 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp django-app_web_1
http://127.0.0.1:8000
.gitlab-ci.yml作成
(venv) $ cat .gitlab-ci.yml
image: python:3.8
stages:
- build
- test
-deploy
test:
stage: test
script:
- python manage.py test
build:
stage: build
script:
- pip install django
- python manage.py collectstatic
deploy:
stage: deploy
script:
- docker-compose up -d
[…] ArgoCD構築 – https://murci.net/archives/4876 Djangoアプリ構築 – https://murci.net/archives/5133 […]