Skip to content

Commit 610cadf

Browse files
authored
Merge pull request #130 from pythonkr/feature/docker
feat: docker image 추가
2 parents 445fa0b + 80e1aa7 commit 610cadf

4 files changed

Lines changed: 156 additions & 17 deletions

File tree

pyconweb2022/.dockerignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# Python
6+
__pycache__
7+
*.py[cod]
8+
*$py.class
9+
*.so
10+
.Python
11+
.venv
12+
venv/
13+
ENV/
14+
env/
15+
16+
# Django
17+
*.log
18+
db.sqlite3
19+
staticfiles/
20+
media/
21+
22+
# IDE
23+
.idea/
24+
.vscode/
25+
*.swp
26+
*.swo
27+
28+
# Docker
29+
Dockerfile
30+
docker-compose*.yml
31+
.docker
32+
33+
# Test
34+
.coverage
35+
htmlcov/
36+
.pytest_cache/
37+
.tox/
38+
39+
# OS
40+
.DS_Store
41+
Thumbs.db
42+
43+
# Misc
44+
*.egg-info/
45+
dist/
46+
build/

pyconweb2022/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM python:3.8-slim
2+
3+
ENV PYTHONDONTWRITEBYTECODE=1
4+
ENV PYTHONUNBUFFERED=1
5+
6+
WORKDIR /app
7+
8+
RUN apt-get update && apt-get install -y --no-install-recommends \
9+
build-essential \
10+
libpq-dev \
11+
default-libmysqlclient-dev \
12+
pkg-config \
13+
&& rm -rf /var/lib/apt/lists/*
14+
15+
RUN pip install --upgrade pip
16+
17+
COPY requirements.txt /app/requirements.txt
18+
RUN pip install --no-cache-dir -r requirements.txt
19+
20+
RUN pip install gunicorn
21+
22+
COPY pyconweb2022/ /app/
23+
24+
EXPOSE 8000
25+
26+
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "pyconweb2022.wsgi:application"]

pyconweb2022/docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
services:
2+
web:
3+
build:
4+
context: ..
5+
dockerfile: pyconweb2022/Dockerfile
6+
ports:
7+
- "8000:8000"
8+
volumes:
9+
- .:/app
10+
environment:
11+
- DEBUG=True
12+
- DJANGO_SETTINGS_MODULE=pyconweb2022.settings
13+
- AWS_PSQL_HOST=db
14+
- AWS_PSQL_PORT=5432
15+
- AWS_PSQL_DATABASE=pyconweb
16+
- AWS_PSQL_USER_ID=postgres
17+
- AWS_PSQL_PW=postgres
18+
depends_on:
19+
- db
20+
command: >
21+
sh -c "python manage.py migrate &&
22+
python manage.py runserver 0.0.0.0:8000"
23+
24+
db:
25+
image: postgres:14-alpine
26+
volumes:
27+
- postgres_data:/var/lib/postgresql/data
28+
environment:
29+
- POSTGRES_DB=pyconweb
30+
- POSTGRES_USER=postgres
31+
- POSTGRES_PASSWORD=postgres
32+
ports:
33+
- "5433:5432"
34+
35+
volumes:
36+
postgres_data:

pyconweb2022/pyconweb2022/settings.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
For the full list of settings and their values, see
1010
https://docs.djangoproject.com/en/4.0/ref/settings/
1111
"""
12-
import os.path
12+
import os
1313
from pathlib import Path
1414

1515
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -86,14 +86,24 @@
8686

8787
# Database
8888
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
89-
90-
91-
DATABASES = {
92-
"default": {
93-
"ENGINE": "django.db.backends.sqlite3",
94-
"NAME": BASE_DIR / "db.sqlite3",
89+
if os.getenv("AWS_PSQL_HOST"):
90+
DATABASES = {
91+
"default": {
92+
"ENGINE": "django.db.backends.postgresql",
93+
"HOST": os.getenv("AWS_PSQL_HOST"),
94+
"PORT": os.getenv("AWS_PSQL_PORT", "5432"),
95+
"NAME": os.getenv("AWS_PSQL_DATABASE"),
96+
"USER": os.getenv("AWS_PSQL_USER_ID"),
97+
"PASSWORD": os.getenv("AWS_PSQL_PW"),
98+
}
99+
}
100+
else:
101+
DATABASES = {
102+
"default": {
103+
"ENGINE": "django.db.backends.sqlite3",
104+
"NAME": BASE_DIR / "db.sqlite3",
105+
}
95106
}
96-
}
97107

98108

99109
# Password validation
@@ -130,15 +140,36 @@
130140
# Static files (CSS, JavaScript, Images)
131141
# https://docs.djangoproject.com/en/4.0/howto/static-files/
132142

133-
STATIC_URL = "static/"
134-
135-
# S3
136-
DEFAULT_FILE_STORAGE = (
137-
"pyconweb2022.customBoto3Storage.SecurityTokenWorkaroundS3Boto3Storage"
138-
)
139-
STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage"
140-
AWS_S3_SESSION_PROFILE = "pycon"
141-
AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STATIC_S3_BUCKET")
143+
STATIC_URL = "/static/"
144+
STATIC_ROOT = BASE_DIR / "staticfiles"
145+
MEDIA_URL = "/media/"
146+
MEDIA_ROOT = BASE_DIR / "media"
147+
148+
if os.getenv("AWS_STATIC_S3_BUCKET"):
149+
DEFAULT_FILE_STORAGE = (
150+
"pyconweb2022.customBoto3Storage.SecurityTokenWorkaroundS3Boto3Storage"
151+
)
152+
STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage"
153+
AWS_S3_SESSION_PROFILE = "pycon"
154+
AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STATIC_S3_BUCKET")
155+
156+
STORAGES = {
157+
"default": {
158+
"BACKEND": "pyconweb2022.customBoto3Storage.SecurityTokenWorkaroundS3Boto3Storage",
159+
},
160+
"staticfiles": {
161+
"BACKEND": "storages.backends.s3boto3.S3StaticStorage",
162+
},
163+
}
164+
else:
165+
STORAGES = {
166+
"default": {
167+
"BACKEND": "django.core.files.storage.FileSystemStorage",
168+
},
169+
"staticfiles": {
170+
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
171+
},
172+
}
142173

143174
# Default primary key field type
144175
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

0 commit comments

Comments
 (0)