Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker refactor ready for AWS #1176

Merged
merged 23 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5fb5147
Docker refactor ready for AWS
ahosgood Jul 11, 2023
453023e
Flake8
ahosgood Jul 11, 2023
554d43f
Quicker uptime - CSS/JS compilation in background
ahosgood Jul 11, 2023
970625b
Fix version of @nationalarchives/frontend
ahosgood Jul 12, 2023
410c29a
Update README.md
ahosgood Jul 12, 2023
66bb8ea
Add Mkdocs as a separate instance
ahosgood Jul 14, 2023
f07dbae
Merge branch 'develop' of github.com:nationalarchives/ds-wagtail into…
ahosgood Jul 17, 2023
64998af
Don't auto run migrations or server on dev server
ahosgood Jul 17, 2023
4527227
Update README
ahosgood Jul 17, 2023
c09cd8a
Change production port from 8080 to 8000
ahosgood Jul 19, 2023
3173808
Add AUTO_START_SERVER option, use non-root user
ahosgood Jul 19, 2023
d51dfc8
Hadolint suggestion
ahosgood Jul 19, 2023
a7e3c99
Don't copy in the development scripts
ahosgood Jul 19, 2023
95bc677
Use the root user (for now)
ahosgood Jul 19, 2023
7858cb6
Refactor to use non-root user
ahosgood Jul 20, 2023
b72ab3d
Remove debug logging
ahosgood Jul 20, 2023
1e3083e
Remove cron schedule from Docker build CI
ahosgood Jul 20, 2023
99fe01b
Merge branch 'develop' of github.com:nationalarchives/ds-wagtail into…
ahosgood Jul 20, 2023
c2ef844
Switch line endings for .sh files
ahosgood Jul 20, 2023
d79d27a
Fixed line endings (LF)
jamesbiggs Jul 20, 2023
d713eec
Build Docker image upon publishing a new release
ahosgood Jul 24, 2023
553065c
Add documentation for Docker changes
ahosgood Jul 24, 2023
4148f85
Merge branch 'develop' of github.com:nationalarchives/ds-wagtail into…
ahosgood Jul 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.DS_Store
.coveragerc
.devcontainer
.editorconfig
.env
.env.example
.environment
.git
.github
.gitignore
.nvmrc
.platform
.platform.app.yaml
.prettierrc.json
.vscode
Dockerfile
README.md
babel.config.json
database_dumps
docker-compose.yml
docs
fabfile.py
gunicorn.conf.py
lint-requirements.txt
media
mkdocs.yml
node_modules
platformsh-cli
setup.cfg
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DATABASE_URL=postgres://postgres:@db:5432/postgres
DATABASE_ENGINE=django.db.backends.postgresql
DATABASE_NAME=postgres
DATABASE_USER=postgres
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Docker

on:
schedule:
- cron: '44 6 * * *'
push:
branches:
- main
- develop
- feature/aws-migration # TEMP
ahosgood marked this conversation as resolved.
Show resolved Hide resolved
tags:
- v*

env:
IMAGE_NAME: ds-wagtail

jobs:
build:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v3
- uses: hadolint/[email protected]
with:
dockerfile: Dockerfile
- name: Build image
run: docker build --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}" --platform=linux/amd64 .
- name: Log in to registry
# This is where you will update the personal access token to GITHUB_TOKEN
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
- name: Push image
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
11 changes: 1 addition & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ __pycache__
.DS_Store

# Ignore the contents of template/static/ but keep the directory
templates/static/*
!templates/static/.gitkeep

templates/static/css/*
!templates/static/css
!templates/static/css/.gitkeep

templates/static/css/libraries/*
!templates/static/css/libraries
!templates/static/css/libraries/.gitkeep
templates/static

# Allow images within static
!templates/static/images
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16
v18.16
41 changes: 26 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
FROM python:3.11
LABEL maintainer="[email protected]"
# Generate static assets (CSS and JavaScript)
FROM node:18.16 AS staticassets
WORKDIR /home
COPY package.json package-lock.json webpack.config.js ./
RUN npm install
COPY scripts ./scripts
COPY sass ./sass
RUN npm run compile




ARG POETRY_HOME=/opt/poetry

FROM python:3.11

EXPOSE 8000

Expand All @@ -23,11 +33,8 @@ ENV \

WORKDIR /app

# Upgrade pip
RUN pip install --upgrade pip

# Install poetry as per official guidance:
# https://github.com/python-poetry/poetry#installation
# Install poetry
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl -sSL "https://install.python-poetry.org" | python -

# Add poetry's bin directory to PATH
Expand All @@ -36,14 +43,18 @@ ENV PATH="$POETRY_HOME/bin:$PATH"
# Copy files used by poetry
COPY pyproject.toml poetry.lock ./

# Install Python dependencies AND the 'etna' app
RUN poetry install

# Copy the executable
COPY bash/run.sh bash/run-dev.sh bash/
RUN chmod +x bash/run.sh bash/run-dev.sh

# Copy application code
COPY . .

# Load shortcuts
RUN cp ./bash/bashrc.sh /root/.bashrc

# Install Python dependencies AND the 'etna' app
RUN poetry install
# Copy static assets
COPY --from=staticassets /home/templates/static/css/dist/etna.css /home/templates/static/css/dist/etna.css.map templates/static/css/dist/
COPY --from=staticassets /home/templates/static/scripts templates/static/scripts

# Do nothing forever (use 'exec' to resuse the container)
CMD tail -f /dev/null
CMD ["./bash/run.sh"]
77 changes: 20 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,53 @@ Convenience commands have been added to `fabfile.py` to help you interact with t
Once installed, you can type `fab -l` to see a list of available commands.


### 1. Build Docker containers
### 1. Build and start Docker containers

```console
$ fab build
```

### 2. Start Docker containers

```console
$ fab start
```

### 3. Start a shell session with the 'web' container

```console
$ fab sh
```

### 5. Apply database migrations

```console
$ python manage.py migrate
```sh
fab start
jamesbiggs marked this conversation as resolved.
Show resolved Hide resolved
```

### 6. Run the 'development' web server
This command takes care of the following:

```console
$ python manage.py runserver 0.0.0.0:8000
```
1. Building all of the necessary Docker containers
1. Starting all of the necessary Docker containers
2. Installing any new python dependencies
3. Applying any new database migrations
4. Starting the Django development server
ahosgood marked this conversation as resolved.
Show resolved Hide resolved

### 7. Access the site
### 2. Access the site

<http://127.0.0.1:8000>

**Note: compiled CSS is not included and therefore needs to be built initially, and after each git pull. See the "Working with SASS/CSS section**
### 3. Create a Django user for yourself

### 8. Create a Django user for yourself

```console
$ python manage.py createsuperuser
```sh
python manage.py createsuperuser
jamesbiggs marked this conversation as resolved.
Show resolved Hide resolved
```

### 9. Access the Wagtail admin
### 4. Access the Wagtail admin

Navigate to the admin URL in your browser, and sign in using the username/password combination you chose in the previous step.

<http://127.0.0.1:8000/admin/>

## Quick start with `fab run`

While it's handy to be able to access 'web' via a shell and interact with Django directly, sometimes all you want is to view the site in a web browser. In these cases, you can use:

```console
$ fab run
```

This command takes care of the following:

1. Starting all of the necessary Docker containers
2. Installing any new python dependencies
3. Applying any new database migrations
4. Starting the Django development server

You can then access the site in your browser as usual:

<http://127.0.0.1:8000>

## Linux / OSX
If you are running a Unix based operating system, these alias commands may be useful to you to run inside the Docker container.

Running the development server:

``` console
$ djrun
``` sh
djrun
```
Run migrations:

```console
$ dj migrate
```sh
dj migrate
```
Create a super user:

```console
$ dj createsuperuser
```sh
dj createsuperuser
```

## Issues with your local environment?
Expand Down
4 changes: 4 additions & 0 deletions bash/bashrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
alias dj="python manage.py"
alias djrun="python manage.py runserver 0.0.0.0:8000"
alias djtest="python manage.py test --settings=config.settings.dev"

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
9 changes: 9 additions & 0 deletions bash/dev-watch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
nvm install
npm install
npm run dev:css &
npm run dev:js &
jamesbiggs marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions bash/run-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
chmod a+x bash/dev-watch.sh
./bash/dev-watch.sh &
poetry run python manage.py collectstatic --no-input
poetry run python manage.py migrate
poetry run python manage.py runserver 0.0.0.0:8000
4 changes: 4 additions & 0 deletions bash/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
poetry run python manage.py collectstatic --no-input
poetry run python manage.py migrate
poetry run gunicorn config.wsgi:application --capture-output --workers 3 --threads 3 --log-level=DEBUG -b 0.0.0.0:8080
2 changes: 1 addition & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"
STATIC_URL = "static/"

MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
Expand Down
12 changes: 11 additions & 1 deletion config/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import os

from .base import * # noqa: F401

DEBUG = False
# TODO: Temporary until the static files can be served via S3 or a CDN
DEBUG = True

# Ensure that certificates are always checked by Kong client in production
KONG_CLIENT_VERIFY_CERTIFICATES = True

# TODO: Generate SECRET_KEY
SECRET_KEY = os.getenv("SECRET_KEY", "")
# Need to get the IP of the load balancer or reverse proxy
ALLOWED_HOSTS = ["*"]
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
STATIC_ROOT = "/app/static"

try:
from .local import * # noqa: F401
except ImportError:
Expand Down
25 changes: 16 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
version: '3'

services:

db:
container_name: db
image: postgres:12.0
ports:
- "5432:5432"
- 5432:5432
volumes:
- ./database_dumps:/app/database_dumps
- ./database_dumps:/app/database_dumps

web:
container_name: web
platform: linux/amd64
build: .
volumes:
- ./:/app
- ./bash/bashrc.sh:/root/.bashrc
depends_on:
- db
- db
ports:
- 8000:8000 # runserver
- 8001:8001 # mkdocs serve
# Closely match prod server
# command: pipenv run gunicorn config.wsgi:application --bind 0.0.0.0:8000 --timeout 3600 --workers 3 --reload --capture-output
# Server for better debugging output
command: tail -f /dev/null # do nothing forever. Use fab sh to run the django server
- 8000:8000
command: sh bash/run-dev.sh
ahosgood marked this conversation as resolved.
Show resolved Hide resolved
restart: on-failure
init: true
stdin_open: true
tty: true
env_file:
- .env
environment:
- DATABASE_HOST=db
- DATABASE_NAME=postgres
- DATABASE_USER=postgres

cli:
container_name: cli
platform: linux/amd64
build: platformsh-cli
volumes:
Expand Down
Loading