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

Add a devcontainer configuration with Docker #4198

Merged
merged 24 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Answer the prompts with your own desired [options](http://cookiecutter-django.re
windows [n]: n
use_pycharm [n]: y
use_docker [n]: n
use_vscode_devcontainer [n]: n
Select postgresql_version:
1 - 14
2 - 13
Expand Down
1 change: 1 addition & 0 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"windows": "n",
"use_pycharm": "n",
"use_docker": "n",
"use_vscode_devcontainer": "n",
"postgresql_version": [
"14",
"13",
Expand Down
5 changes: 5 additions & 0 deletions docs/project-generation-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ use_pycharm:
use_docker:
Indicates whether the project should be configured to use Docker_ and `Docker Compose`_.

use_vscode_devcontainer:
Indicates whether the project should be configured to use `VS Code Dev Container`_.

postgresql_version:
Select a PostgreSQL_ version to use. The choices are:

Expand Down Expand Up @@ -145,6 +148,8 @@ debug:
.. _Docker: https://github.com/docker/docker
.. _Docker Compose: https://docs.docker.com/compose/

.. _VS Code Dev Container: https://github.com/microsoft/vscode-dev-containers

.. _PostgreSQL: https://www.postgresql.org/docs/

.. _Gulp: https://github.com/gulpjs/gulp
Expand Down
35 changes: 31 additions & 4 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,37 @@ def remove_pycharm_files():


def remove_docker_files():
shutil.rmtree("compose")
if "{{ cookiecutter.use_vscode_devcontainer }}".lower() == "n":
shutil.rmtree("compose")
file_names = ["local.yml", "production.yml", ".dockerignore"]
else:
file_names = ["production.yml"]

file_names = ["local.yml", "production.yml", ".dockerignore"]
for file_name in file_names:
os.remove(file_name)

if "{{ cookiecutter.use_pycharm }}".lower() == "y":
file_names = ["docker_compose_up_django.xml", "docker_compose_up_docs.xml"]
for file_name in file_names:
os.remove(os.path.join(".idea", "runConfigurations", file_name))


def remove_vscode_devcontainer_files():
dir_path = ".devcontainer"
if os.path.exists(dir_path):
shutil.rmtree(dir_path)


def create_vscode_devcontainer_bash_history_file():
history_dir_path = ".history"
if not os.path.exists(history_dir_path):
os.mkdir(history_dir_path)

history_file_path = ".history/bash_history"
with open(history_file_path, "a"):
pass


def remove_utility_files():
shutil.rmtree("utility")

Expand Down Expand Up @@ -449,6 +469,11 @@ def main():
else:
remove_docker_files()

if "{{ cookiecutter.use_vscode_devcontainer }}".lower() == "n":
remove_vscode_devcontainer_files()
else:
create_vscode_devcontainer_bash_history_file()

if (
"{{ cookiecutter.use_docker }}".lower() == "y"
and "{{ cookiecutter.cloud_provider}}" != "AWS"
Expand All @@ -463,12 +488,14 @@ def main():
if (
"{{ cookiecutter.use_docker }}".lower() == "n"
and "{{ cookiecutter.use_heroku }}".lower() == "n"
and "{{ cookiecutter.use_vscode_devcontainer }}".lower() == "n"
):
if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y":
print(
INFO + ".env(s) are only utilized when Docker Compose and/or "
"Heroku support is enabled so keeping them does not "
"make sense given your current setup." + TERMINATOR
"Heroku and/or VS Code Dev Container support is enabled so "
"keeping them does not make sense given your current setup."
+ TERMINATOR
)
remove_envs_and_associated_files()
else:
Expand Down
16 changes: 16 additions & 0 deletions {{cookiecutter.project_slug}}/.devcontainer/bashrc.override.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#
# .bashrc.override.sh
#

# persistent bash history
HISTFILE=~/.bash_history
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

# set some django env vars
source /entrypoint

# restore default shell options
set +o errexit
set +o pipefail
set +o nounset
83 changes: 83 additions & 0 deletions {{cookiecutter.project_slug}}/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// For format details, see https://containers.dev/implementors/json_reference/
{
"name": "{{cookiecutter.project_slug}}_dev",
"dockerComposeFile": [
"../local.yml"
],
"init": true,
"mounts": [
{
"source": "./.history/bash_history",
"target": "/home/vscode/.bash_history",
"type": "bind"
},
{
"source": "/tmp",
"target": "/tmp",
"type": "bind"
}
],
// Tells devcontainer.json supporting services / tools whether they should run
// /bin/sh -c "while sleep 1000; do :; done" when starting the container instead of the container’s default command
"overrideCommand": true,
"service": "django",
// "remoteEnv": {"PATH": "/home/vscode/.local/bin:${containerEnv:PATH}"},
"remoteUser": "vscode",
"workspaceFolder": "/app",
// Set *default* container specific settings.json values on container create.
"customizations": {
"vscode": {
"settings": {
"editor.formatOnSave": true,
"isort.args": [
"--profile",
"black"
],
browniebroke marked this conversation as resolved.
Show resolved Hide resolved
"[python]": {
"analysis.autoImportCompletions": true,
"analysis.typeCheckingMode": "basic",
"defaultInterpreterPath": "/usr/local/bin/python",
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
//"editor.defaultFormatter": "ms-python.black-formatter",
browniebroke marked this conversation as resolved.
Show resolved Hide resolved
// "formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
browniebroke marked this conversation as resolved.
Show resolved Hide resolved
"formatting.blackPath": "/usr/local/bin/black",
"formatting.provider": "black",
// "formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
browniebroke marked this conversation as resolved.
Show resolved Hide resolved
"languageServer": "Pylance",
// "linting.banditPath": "/usr/local/py-utils/bin/bandit",
"linting.enabled": true,
"linting.flake8Enabled": true,
"linting.flake8Path": "/usr/local/bin/flake8",
"linting.mypyEnabled": true,
"linting.mypyPath": "/usr/local/bin/mypy",
"linting.pycodestylePath": "/usr/local/bin/pycodestyle",
// "linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"linting.pylintEnabled": true,
"linting.pylintPath": "/usr/local/bin/pylint"
}
},
// https://code.visualstudio.com/docs/remote/devcontainerjson-reference#_vs-code-specific-properties
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"davidanson.vscode-markdownlint",
"mrmlnc.vscode-duplicate",
"visualstudioexptteam.vscodeintellicode",
"visualstudioexptteam.intellicode-api-usage-examples",
// python
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.isort",
// django
"batisteo.vscode-django"
]
}
},
// Uncomment the next line if you want start specific services in your Docker Compose config.
// "runServices": [],
// 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.
"postCreateCommand": "cat .devcontainer/bashrc.override.sh >> ~/.bashrc"
}
1 change: 1 addition & 0 deletions {{cookiecutter.project_slug}}/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why we need this one? It seems a bit unrelated (I don't see prettier configured).

I'm asking because In the pre-commit config, we exclude Django templates from prettier in another way:

exclude: '{{cookiecutter.project_slug}}/templates/'

Maybe we should make the 2 exclusins more consistent?

14 changes: 13 additions & 1 deletion {{cookiecutter.project_slug}}/compose/local/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ ENV BUILD_ENV ${BUILD_ENVIRONMENT}

WORKDIR ${APP_HOME}

{% if cookiecutter.use_vscode_devcontainer == "y" %}
# VS Code devcontainer dependencies and utils
RUN apt-get update && apt-get install --no-install-recommends -y \
sudo git bash-completion nano

# Create vscode user and add it to sudoers
RUN groupadd --gid 1000 vscode \
&& useradd --uid 1000 --gid vscode --shell /bin/bash --create-home vscode \
&& echo vscode ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/vscode \
&& chmod 0440 /etc/sudoers.d/vscode
{% endif %}

# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
# psycopg2 dependencies
Expand All @@ -51,7 +63,7 @@ COPY --from=python-build-stage /usr/src/app/wheels /wheels/

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

COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
Expand Down