Wagtail in Docker

django, docker, programming, Python, Windows

Over the weekend I found out about a CMS software written on top of Django called Wagtail.

The installation instructions promise an easy path. Just run:

pip install wagtail

Since I guard my global pip pretty carefully in order to reduce version collisions and whatnot, my first inclination was to see if I can test this from within a Docker container.

To start the experiment within a Docker container using Python, I start with:

mkdir wagtail
cd wagtail

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/wagtail \
 python:3.7-buster /bin/bash

I use “%CD%” because I’m on Windows. If I were on Linux/Mac, I suppose it would be something like “${PWD}” instead.

So once in, I install and initialize per the instructions from Getting started — Wagtail Documentation 3.0.1 documentation:

cd /wagtail
pip install wagtail
wagtail start hahaha 

Interestingly, that wagtail start command, in addition to generating a bunch of files typical for a Django app, also created a Dockerfile.

So maybe the installer may already have some Docker support in mind?

Reading through it and spending more time than I expected to get to work, here are the steps to:

  • Get a Wagtail project set up from scratch using Docker without having to install any Python on the host.
  • Use a volume from the host for the app AND the SQLite DB so that they can be conveniently backed up and transferred (e.g. pushed up to some code repository like Github or SVN).

Generate a new Wagtail project

Pretty much what I had above:

mkdir wagtail
cd wagtail

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/wagtail \
 python:3.7-buster /bin/bash
cd /wagtail
pip install wagtail
wagtail start hahaha
exit

I’m using “hahaha” as my project name. Substitute as needed. Also: %CD% should be ${PWD} for Linux/Mac.

Build the Image

cd hahaha
docker build -t hahaha . 

Fix up file permissions (Windows only)

Since the app should be run by the user wagtail:wagtail, fix up the permissions on the files.

For some reason this is not done correctly for Windows despite the command in Dockerfile, so a manual step is required:

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/app \
 --user root hahaha /bin/bash

chown -R wagtail:wagtail .
exit

Set Up the App

This is typical Django setup stuff:

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/app \
 hahaha /bin/bash
python manage.py migrate
python manage.py createsuperuser
exit

The above will create the db.sqlite3 file to be used for the app and also set up an admin user to be used to sign into the app.

Run The App

Finally, to run the app:

docker run -it --rm -p 8000:8000 \
 --mount type=bind,source="%CD%",target=/app hahaha

The -it --rm arguments are optional, but they help in stopping and cleaning up the container during development.

The site can be accessed at, of course, http://localhost:8000/. To manage it, use the superuser created earlier to get into the Admin Interface.

Worker Timeout

And now to actually start playing around with Wagtail….

So far I’m seeing a lot of errors when requesting pages in the Admin site. They look like this:

[2022-08-01 01:48:15 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:12)
[2022-08-01 01:48:15 +0000] [12] [INFO] Worker exiting (pid: 12)
[2022-08-01 01:48:15 +0000] [13] [INFO] Booting worker with pid: 13
[2022-08-01 01:48:58 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:13)
[2022-08-01 01:48:58 +0000] [13] [INFO] Worker exiting (pid: 13)
[2022-08-01 01:48:58 +0000] [14] [INFO] Booting worker with pid: 14
[2022-08-01 01:49:56 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:14)
[2022-08-01 01:49:56 +0000] [14] [INFO] Worker exiting (pid: 14)
[2022-08-01 01:49:56 +0000] [15] [INFO] Booting worker with pid: 15
[2022-08-01 01:50:34 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:15)
[2022-08-01 01:50:34 +0000] [15] [INFO] Worker exiting (pid: 15)
[2022-08-01 01:50:34 +0000] [16] [INFO] Booting worker with pid: 16

Docker Now Available for Windows Home

docker, Windows

For the longest time, Docker just didn’t like Windows Home. Legend has it that, with VirtualBox running a Linux VM, you then can install Docker Toolbox on top of that. I’ve tried that route. It works until I try to do volume mounts to the host file system. Somehow somewhere in the Docker > Virtualbox > Windows Home link something’s not right.

With the recent changes to Windows to add Linux support, Docker can now run on Windows Home. Unless there are other reasons (e.g. Remote Desktop), there is no need to upgrade to Windows Professional ($99 USD).

Install Linux Support

The process to install Docker on Windows Home is kinda long and spans multiple pages. Here is the summary:

  • Get the “Windows 10 May 2020 Update” or later by downloading and running the “Windows 10 Update Assistant”: https://www.microsoft.com/en-us/software-download/windows10 . NOTE: This will take a while.
  • Run PowerShell as Administrator (Windows-S; type “PowerShell”; click “Run as Administrator”)
  • Run in PowerShell:
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • Restart Windows
  • Run PowerShell as Administrator (Windows-S; type “PowerShell”; click “Run as Administrator”)
  • Run in PowerShell:
    wsl --set-default-version 2
  • Open Microsoft Store and search for “Windows Subsystem for Linux” (WSL) (https://aka.ms/wslstore) and install a Linux distribution (e.g. “Ubuntu”)
  • Run the Linux distribution. You will be prompted to create a user account and set its password. See https://docs.microsoft.com/en-us/windows/wsl/install-win10#troubleshooting-installation for troubleshooting if necessary.

Install Docker for Windows

  • Go to https://hub.docker.com/editions/community/docker-ce-desktop-windows/ and download “Docker Desktop for Windows” (Don’t worry about the wording about Windows Professional). This downloads the “Docker Desktop Installer.exe” file that you then run.
  • Typically this will install into C:\Program Files\Docker\Docker. The runnable is “Docker Desktop.exe“.

References

https://hub.docker.com/editions/community/docker-ce-desktop-windows/

https://docs.microsoft.com/en-us/windows/wsl/install-win10

Fast Guide to Launching an EC2 Instance w/ SSH Access

AWS, ec2, ssh, Windows

Concepts

Minimal number of concepts to understand:

  • Key pair — a pair of public and private cryptographic keys that will be used to establish a secure shell/terminal to the launched EC2 instance.
  • Security group — a group of access rules that determine what network traffic and go into (inbound rules) and go out of (outbound rules) the EC2 instance.
  • IAM role — a collection of rules that determine what AWS services the EC2 instance will have access to (and what kind of access). E.g. read-only access to S3.
  • AMI — an image that prescribes an OS and some software to run when an EC2 instance comes up.

Shared Key Pair

The only thing that is shared between EC2 and the SSH program that matters in this example is the key pair. The instructions here will describe how to create a new key pair.

Creating a Key Pair

  • Log into the AWS console. The remaining steps to launch an instance will be done in the AWS Console.
  • Access Services > EC2 > Key Pairs from AWS Console.
  • Click “Create Key Pair”
  • Give it a name “KP”
  • Once it’s created, a “.pem” file will be downloaded. Remember the name and where the file is downloaded. It will be needed later.

Create a Security Group

  • Access Services > EC2 > Security Groups
  • Click “Create Security Group” to create a security group. Name it “SG.”
  • In the “Inbound” rules, add an entry for Type SSH, Protocol TCP, Port Range 22. For the Source, select “My IP” to let the tool automatically select your IP address.
  • Add other rules to open up more ports as needed.

Create an IAM Role

  • Access Services > Security, Identity, & Compliance > IAM > Roles
  • Click “Create Role” and select “EC2” (as opposed to Lambda)
  • Click “Next: Permissions”
  • Add permissions as needed (e.g. add “AmazonS3ReadOnlyAccess” if read-only access to S3 is needed).
  • Give the role a name and description.

Launch Instance

  • Access Services > EC2 > EC2 Dashboard
  • Click “Launch Instance”
  • Select an appropriate AMI (e.g. any of the Amazon Linux ones) to use for the EC2 instance. For the instance type, start with “t2.nano” to experiment with since it’s cheapest. Once the instance is up and running, larger instance types can be used as needed.
  • Click “Next: Configure Instance Details.”
  • For IAM role, select the role created above. Everything else can stay as-is.
  • Click “Next: Add Storage.”
  • Edit as desired, but the default is fine.
  • Click “Next: Add Tags.”
  • Add tags as needed. These are optional.
  • Click “Next: Configure Security Group.”
  • Choose “Select an existing security group” and select the security group “SG” created above.
  • Click “Review and Launch.”
  • Click “Launch” after everything looks right.
  • A modal comes up to allow selection of a key pair to use to access the instance. Select “KP” as created above.
  • Continue the launch.
  • Click on the “i-xxxxxxxxx” link to see the status of the instance.
  • Wait until Instance State is “running” and Status Checks is “2/2.”
  • Note the “Public DNS (IPv4)” value. It is the host name to SSH into.

Connecting to The EC2 Instance

Windows with Bitvise SSH Client

  • Download and start Bitvise SSH Client.
  • Click “New profile”
  • Go to “Login” tab
  • Click the link “Client key manager”
  • Click “Import”
  • Change file filter to (*.*)
  • Locate the .pem file downloaded above and import it. Accept default settings.
  • In the “Server” tab, enter the Public DNS host name from above. Use port 22.
  • In the “Authentication” section, enter “ec2-user” as the Username.
  • Use “publickey” as the Initial Method.
  • For “Client key,” select the profile created earlier when importing the .pem file.
  • Click “Log in” and confirm any dialogs.

Mac OS

Change the permission of the downloaded .pem file to allow only the owner access:

chmod 400 ~/Downloads/mykey.pem

Use ssh with the .pem file:

ssh -i ~/Downloads/mykey.pem ec2-user@xx-xx-xx-xx.yyyy.amazonaws.com