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

Create a "debug" Docker configuration for running in VS Code #22

Merged
merged 21 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
79ab2c7
Lines added by VS Code, one commented out so that it builds
MBARIMike Jul 20, 2021
a16932f
Update instructions with local and production examples
MBARIMike Jul 21, 2021
1d93a69
Modified from smdb/compose/local/django/Dockerfile for use in VS Code
MBARIMike Jul 21, 2021
f747b95
Modified from smdb/local.yml for debug configuration that is to work …
MBARIMike Jul 21, 2021
505e203
Config files to support debugging in VS Code
MBARIMike Jul 21, 2021
28b90e7
For building devcontainer in VS Code customized for django per
MBARIMike Jul 21, 2021
03230f0
Use container's /root/.zshrc
MBARIMike Jul 21, 2021
15c26bb
It appears that smdb/.vscode/launch.json is used for debugging
MBARIMike Jul 21, 2021
2923e32
Add 'source /entrypoint' to /root/.zshrc, solving database connection…
MBARIMike Jul 21, 2021
7ccac97
Add less wget zsh installs, remove suggestions that don't seem to be …
MBARIMike Jul 21, 2021
f256380
Format comment
MBARIMike Jul 21, 2021
66c6dd1
Comment out /bin/zsh, add 'source /entrypoint' to ~/.bashrc
MBARIMike Jul 22, 2021
bf017a4
Comment out 'set' lines so that shell doesn't exit on failed command …
MBARIMike Jul 27, 2021
6d93274
Use port 8001 for runserver_plus starte in debug container for VS Code
MBARIMike Jul 28, 2021
b30247a
Add runserver_plus configuration in launch.json that works
MBARIMike Jul 28, 2021
57f1e67
Add port 8001 and remove test start of debugpy, that's not needed wit…
MBARIMike Jul 28, 2021
a020b69
Use requirements/local.txt (no differences) and debug/django/start (p…
MBARIMike Jul 28, 2021
55ed39b
Edited comments
MBARIMike Jul 28, 2021
926d9a0
Add instructions for working on the code with VS Code
MBARIMike Jul 28, 2021
683c1c9
Add application settings overrides
MBARIMike Jul 28, 2021
bb05380
Mention 'web request' debugging
MBARIMike Jul 28, 2021
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
44 changes: 44 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// devcontainer.json
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.112.0/containers/docker-existing-docker-compose
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
{
"name": "Existing Docker Compose (Extend)",
// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
"dockerComposeFile": ["../smdb/debug.yml"],
// The 'service' property is the name of the service for the container that VS Code should
// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
"service": "django",
// The optional 'workspaceFolder' property is the path VS Code should open by default when
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
"workspaceFolder": "/app",
// Set *default* container specific settings.json values on container create.
"settings": {
// Deprecated - use `#terminal.integrated.defaultProfile.windows#` in settings.json instead
//"terminal.integrated.shell.linux": "/bin/zsh",
"[python]": {
"editor.rulers": [120]
},
"editor.formatOnSave": true,
"python.pythonPath": "/usr/local/bin/python",
"python.linting.pylintEnabled": true,
"python.formatting.provider": "black",
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"visualstudioexptteam.vscodeintellicode",
"esbenp.prettier-vscode"
],
// Uncomment the next line if you want start specific services in your Docker Compose config.
"runServices": ["postgres", "node"],
// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
// "shutdownAction": "none",
// Uncomment the next line to run commands after the container is created - for example installing git.
"postCreateCommand": "wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh || true && echo 'source /entrypoint' >> ~/.zshrc && echo 'source /entrypoint' >> ~/.bashrc"
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
7 changes: 7 additions & 0 deletions .devcontainer/docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.0"

services:
django:
volumes:
- ./.devcontainer/.zshrc:/root/.zshrc

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ Mount `smb://titan.shore.mbari.org/SeafloorMapping` and load initial Mission dat
docker-compose run --rm -u <uid> -v /Volumes/SeafloorMapping:/mbari/SeafloorMapping django scripts/load.py -v
```

### Work on the code with VS Code

1. Install [VS Code](https://code.visualstudio.com/download) and the
[Remote-Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)
extension.

2. From VS Code: File -> Open and select your SMDB_HOME directory. The devcontainer.json
file will be detected and you will be prompted to "Reopen in Container". Click the button
and wait for the containers to build and run.

3. Run an IPython shell giving access through Django to the database:

In the Debug panel click the play button next to the "manage.py shell_plus" item in the pick list at top.
A "In [1]:" prompt should appear in the Terminal pane - test by printing all the Missions in the database:
```
In [1]: Mission.objects.all()
```

4. Run the server in VS Code's debugger:

In the Debug panel click the play button next to the "Debug runserver_plus" item in the pick list at top.
You may set breakpoints and examine variables while the application's view code executes web requests.


### Deploy a production instance of smdb

Expand Down
27 changes: 27 additions & 0 deletions smdb/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,30 @@
.pre-commit-config.yaml
.readthedocs.yml
.travis.yml
**/__pycache__
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
# Build in VS Code fails with '**/compose*: https://github.com/mbari-org/SeafloorMappingDB/issues/21#issuecomment-883648752
##**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md
25 changes: 25 additions & 0 deletions smdb/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Django runserver_plus",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": ["runserver_plus", "0.0.0.0:8000"],
"django": true
},
{
"name": "manage.py shell_plus",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": ["shell_plus"],
"justMyCode": false
}
]
}
4 changes: 4 additions & 0 deletions smdb/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.pythonPath": "/bin/python"
"terminal.integrated.defaultProfile.linux": "zsh"
}
80 changes: 80 additions & 0 deletions smdb/compose/debug/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Base the build off of recent stable GDAL image
FROM osgeo/gdal:ubuntu-small-3.3.0 as python

# Python build stage
FROM python as python-build-stage

ARG BUILD_ENVIRONMENT=local

# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
build-essential \
graphviz-dev \
# psycopg2 dependencies
libpq-dev \
python3-dev \
python3-pip

# Requirements are installed here to ensure they will be cached.
COPY ./requirements .

# Create Python Dependency and Sub-Dependency Wheels.
RUN pip wheel --wheel-dir /usr/src/app/wheels \
-r local.txt


# Python 'run' stage
FROM python as python-run-stage

ARG BUILD_ENVIRONMENT=debug
ARG APP_HOME=/app

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}

WORKDIR ${APP_HOME}

# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
graphviz \
graphviz-dev \
# psycopg2 dependencies
libpq-dev \
# Translations dependencies
gettext \
postgresql-client \
python3-pip \
# For quicker SeafloorMapping file finds
mlocate \
# Install here rather than from .devcontainer/devcontainer.json
git less wget zsh \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
# copy python dependency wheels from python-build-stage
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
COPY --from=python-build-stage /usr/lib/libgdal* /usr/lib/
RUN ldconfig

# use wheels to install python dependencies
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
&& rm -rf /wheels/

COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint

COPY ./compose/debug/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start



# copy application code to WORKDIR
COPY . ${APP_HOME}

ENTRYPOINT ["/entrypoint"]
9 changes: 9 additions & 0 deletions smdb/compose/debug/django/start
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset


python manage.py migrate
python manage.py runserver_plus 0.0.0.0:8001
6 changes: 3 additions & 3 deletions smdb/compose/production/django/entrypoint
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset
##set -o errexit
##set -o pipefail
##set -o nounset



Expand Down
81 changes: 81 additions & 0 deletions smdb/debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
version: '3'

volumes:
local_postgres_data: {}
local_postgres_data_backups: {}

services:
django:
build:
context: .
dockerfile: ./compose/debug/django/Dockerfile
image: smdb_debug_django
container_name: django
depends_on:
- postgres
- mailhog
volumes:
# Edit to use directory on your host
- /Users/mccann/docker_smdb_vol:/etc/smdb:z
- .:/app:z
env_file:
- ./.envs/.local/.django
- ./.envs/.local/.postgres
ports:
# VS Code debug launch uses 8000, command: /start uses 8001
- "8000:8000"
- "8001:8001"
- "5678:5678"
command: /start

postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: smdb_production_postgres
container_name: postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data:Z
- local_postgres_data_backups:/backups:z
env_file:
- ./.envs/.local/.postgres

## docs:
## image: smdb_local_docs
## container_name: docs
## build:
## context: .
## dockerfile: ./compose/local/docs/Dockerfile
## env_file:
## - ./.envs/.local/.django
## volumes:
## - ./docs:/docs:z
## - ./config:/app/config:z
## - ./smdb:/app/smdb:z
## ports:
## - "7000:7000"
## command: /start-docs

mailhog:
image: mailhog/mailhog:v1.0.0
container_name: mailhog
ports:
- "8025:8025"

node:
build:
context: .
dockerfile: ./compose/local/node/Dockerfile
image: smdb_local_node
container_name: node
depends_on:
- django
volumes:
- .:/app:z
# http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html
- /app/node_modules
command: npm run dev
ports:
- "3000:3000"
# Expose browsersync UI: https://www.browsersync.io/docs/options/#option-ui
- "3001:3001"
3 changes: 3 additions & 0 deletions smdb/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file
django==3.1.1
gunicorn==20.0.4
6 changes: 4 additions & 2 deletions smdb/scripts/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
cd SeafloorMappingDB
export SMDB_HOME=$(pwd)
export COMPOSE_FILE=$SMDB_HOME/smdb/local.yml
docker-compose run --rm -u 399 -v /mbari/SeafloorMapping:/mbari/SeafloorMapping django {__file__} -v
(Replace '399' with your MBARI user id, what `id -u` returns.)
docker-compose run --rm -u $UID -v /Volumes/SeafloorMapping:/mbari/SeafloorMapping django {__file__} -v
-- or, on production --
export COMPOSE_FILE=$SMDB_HOME/smdb/production.yml
docker-compose run --rm -u $UID -v /mbari/SeafloorMapping:/mbari/SeafloorMapping django {__file__} -v
"""


Expand Down