Create and move a directory for your project. Here, we will proceed with the application name linebot.
MacBook-Air ~ % mkdir jinolinebot
MacBook-Air ~ % cd jinolinebot
MacBook-Air jinolinebot %
Create a Dockerfile and open it in an editor.
MacBook-Air jinolinebot % touch Dockerfile
Edit and save the Dockerfile.
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
Create requirements.txt and edit it as follows
MacBook-Air jinolinebot % touch requirements.txt
requirements.txt
Django>=1.8,<2.0
psycopg2
Create docker-compose.yml and edit it as follows
MacBook-Air jinolinebot % touch docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: postgres
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
Now that the three files are ready, launch the app.
MacBook-Air jinolinebot % docker-compose run web django-admin startproject app name.
Creating network "jinolinebot_default" with the default driver
Building web
Step 1/7 : FROM python:3
---> 28a4c88cdbbf
~
Creating jinolinebot_db_1 ... done
Creating jinolinebot_web_run ... done
After starting up, the next step is to set up the connection to the database. Edit the configuration file generated by the previous command as follows and save it.
app name/settings.py
~
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
~
Launch the container, and when the following display is displayed first, access http://0.0.0.0:8000/
MacBook-Air jinolinebot % docker-compose up
~
Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
It worked! If you see Congratulations on your first Django-powered page.
https://github.com/yongjugithub/djangodocker
https://docs.docker.jp/compose/django.html#id2
Recommended Posts