Skip to content

Commit

Permalink
Improve docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
filipopo committed Nov 26, 2024
1 parent 0287118 commit b8e6ec9
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 45 deletions.
31 changes: 26 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
ARG PY_VER="3.12"
ARG PY_VER=3.12
ARG BUILD=default

# Build stage
FROM python:${PY_VER}-slim AS base
ARG BUILD

WORKDIR /app

COPY app/requirements.txt .
COPY app/requirements.txt app/requirements-mssql.txt .

RUN pip install --no-cache-dir --upgrade -r requirements.txt
RUN pip install --no-cache-dir --upgrade -r requirements.txt && \
if [ "$BUILD" = "mssql" ]; then \
pip install --no-cache-dir --upgrade -r requirements-mssql.txt && \
apt update && apt install -y curl gpg && \
apt clean && rm -rf /var/lib/apt/lists/* && \
VER=$(cut -d. -f1 /etc/debian_version) && \
curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg && \
curl https://packages.microsoft.com/config/debian/${VER}/prod.list > /etc/apt/sources.list.d/mssql-release.list; \
fi

COPY app .

RUN echo "python manage.py migrate --noinput && gunicorn --bind 0.0.0.0:8000 urlshortener.wsgi" > start.sh && \
python manage.py collectstatic --noinput && \
rm -rf webapp/static requirements.txt
rm -rf webapp/static requirements.txt requirements-mssql.txt

# Deploy stage
FROM python:${PY_VER}-slim
FROM python:${PY_VER}-slim AS build_default
ARG PY_VER

WORKDIR /app
Expand All @@ -25,6 +35,17 @@ COPY --from=base /usr/local/bin/gunicorn /usr/local/bin/gunicorn

COPY --from=base /usr/local/lib/python${PY_VER}/site-packages/ /usr/local/lib/python${PY_VER}/site-packages/

FROM build_default AS build_mssql

ONBUILD COPY --from=base /usr/share/keyrings/microsoft-prod.gpg /usr/share/keyrings/microsoft-prod.gpg

ONBUILD COPY --from=base /etc/apt/sources.list.d/mssql-release.list /etc/apt/sources.list.d/mssql-release.list

ONBUILD RUN apt update && ACCEPT_EULA=y apt install -y unixodbc msodbcsql17 && \
apt clean && rm -rf /var/lib/apt/lists/*

FROM build_$BUILD

COPY --from=base /app .

EXPOSE 8000
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cd urlshortener
python manage.py startapp webapp
```

and the infra folder which was created using this cdktf template:
and the cdk folder which was created using this cdktf template:

`cdktf init --template=typescript --providers=azurerm --local`

Expand Down Expand Up @@ -67,7 +67,7 @@ This application comes with an API which it interally uses, the available routes
curl -b "csrftoken=${csrf}" -H "X-CSRFToken: ${csrf}" -d "url=https://example.com" http://127.0.0.1:8000/url
```
```
{"message": "http://127.0.0.1:8000/u/filip", "url": true}
{"message": "http://127.0.0.1:8000/u/A", "url": true}
```
</details>

Expand All @@ -92,9 +92,9 @@ Regardless of how you deploy this app, there are some environment variables that

```
DB_ENGINE=mssql
DB_NAME=database
DB_USER=root
DB_PASSWORD=password
DB_NAME=urlshortener
DB_USER=sa
DB_PASSWORD=P@ssw0rd!
DB_HOST=example.database.windows.net
DB_PORT=1433
```
Expand Down Expand Up @@ -123,7 +123,7 @@ There are several options for installing the app, here is a non exhaustive list:

For this approach you will need Docker: https://www.docker.com

Running `docker compose up` will build the Dockerfile in the current directory and start 3 containers, the python app, a mssql database and an nginx static file server. Consider using an .env file with the env-file option
Running `docker compose up` will build the Dockerfile in the current directory and start 3 containers, the python app, a mssql database and an nginx static file server

Alternatively, you can run just the python app like this:

Expand All @@ -133,6 +133,8 @@ There are several options for installing the app, here is a non exhaustive list:
```

If you're deploying to the cloud, make sure you build for the right platform e.g `--platform linux/amd64`

If you're going to use the mssql database, build with `--build-arg BUILD=mssql`
</details>

<details>
Expand All @@ -144,9 +146,9 @@ There are several options for installing the app, here is a non exhaustive list:

the VE can be activated: `source venv/bin/activate` and deactivated: `deactivate`

To get started install the dependencies
To get started install the dependencies, you may skip mssql if you don't plan on using it

`pip install -r requirements.txt`
`pip install -r requirements.txt requirements-mssql.txt`

Then this command to apply the database migrations

Expand Down
1 change: 1 addition & 0 deletions app/requirements-mssql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mssql-django==1.5
1 change: 0 additions & 1 deletion app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
django==5.0.9
mssql-django==1.5
gunicorn==23.0.0
12 changes: 6 additions & 6 deletions app/urlshortener/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
DATABASES = {
'default': {
'ENGINE': os.getenv('DB_ENGINE', 'mssql'),
'NAME': os.getenv('DB_NAME', 'database'),
'USER': os.getenv('DB_USER', 'root'),
'PASSWORD': os.getenv('DB_PASSWORD', 'password'),
'NAME': os.getenv('DB_NAME', 'urlshortener'),
'USER': os.getenv('DB_USER', 'sa'),
'PASSWORD': os.getenv('DB_PASSWORD', 'P@ssw0rd!'),
'HOST': os.getenv('DB_HOST', 'example.database.windows.net'),
'PORT': os.getenv('DB_PORT', '1433'),
}
Expand Down Expand Up @@ -128,11 +128,11 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

CSRF_TRUSTED_ORIGINS = os.getenv('DJANGO_CSRF', 'https://example.com').split(',')
CSRF_TRUSTED_ORIGINS = os.getenv('DJANGO_CSRF', 'http://127.0.0.1,http://127.0.0.1:8000').split(',')

CSRF_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = os.getenv('DJANGO_CSRF') != None

SESSION_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = os.getenv('DJANGO_CSRF') != None

# Redirect to home URL after login and logout
LOGIN_REDIRECT_URL = '/'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 16 additions & 7 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
services:
web:
build: .
build:
context: .
args:
BUILD: mssql
restart: unless-stopped
environment:
- DB_EXTERNAL=true
- DB_HOST=db
depends_on:
db:
condition: service_healthy
Expand All @@ -13,17 +19,20 @@ services:
restart: unless-stopped
environment:
MSSQL_PID: "Express"
MSSQL_SA_PASSWORD: "${DB_PASSWORD:-P@ssw0rd}"
MSSQL_SA_PASSWORD: "${DB_PASSWORD:-P@ssw0rd!}"
ACCEPT_EULA: "Y"
SQLCMDPASSWORD: "${DB_PASSWORD:-P@ssw0rd}"
SQLCMDPASSWORD: "${DB_PASSWORD:-P@ssw0rd!}"
healthcheck:
test: /opt/mssql-tools*/bin/sqlcmd -C -S localhost -U sa -Q "SELECT 1" -b -o /dev/null
interval: 10s
retries: 10
timeout: 3s
start_period: 10s
#volumes:
# - /var/opt/mssql:/var/opt/mssql
start_period: 20s
volumes:
- ./compose/init.sql:/init/init.sql
- ./compose/entrypoint.sh:/usr/local/bin/entrypoint.sh
#- /var/opt/mssql:/var/opt/mssql
entrypoint: ["sh", "/usr/local/bin/entrypoint.sh"]

nginx:
image: nginx
Expand All @@ -34,7 +43,7 @@ services:
- web
volumes:
- static:/app/prod_static
- ./nginx.conf:/etc/nginx/conf.d/default.conf
- ./compose/nginx.conf:/etc/nginx/conf.d/default.conf

volumes:
static:
14 changes: 14 additions & 0 deletions compose/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
set -e

# Start SQL Server in the background
/opt/mssql/bin/sqlservr &

# Give SQL Server time to start
sleep 10

# Run SQL script
/opt/mssql-tools*/bin/sqlcmd -C -S localhost -U sa -i /init/init.sql -b

# Wait for SQL Server to exit
wait
5 changes: 5 additions & 0 deletions compose/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'urlshortener')
BEGIN
CREATE DATABASE urlshortener;
END;
GO
21 changes: 21 additions & 0 deletions compose/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server {
listen 80;

location /favicon.ico {
alias /app/prod_static/favicon.ico;
}

location /static {
alias /app/prod_static;
}

location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
}
18 changes: 0 additions & 18 deletions nginx.conf

This file was deleted.

0 comments on commit b8e6ec9

Please sign in to comment.