Closed grll closed 1 year ago
@grll Thank you for putting this together. I haven't used devcontainers before, and I was wondering how is the development experience improved.
there are several development improvement by using this approach. The fact that the IDE is within the container means that you don't need to work with potentially slow mounted volumes, most importantly all the IDE related experience can now be encapsulated within the container and easily shared with your colleagues on a project bases (prettier, pylint, the python interpreter ...) so you not only share the code for the project but also the complete environment to work with the code
@grll nice setup!
What is your experience with debugging in your setup?
Have you tried the new debugpy integration in the Python extension? I am currently struggling to make it work. If you have any tips could you share them please?
Thanks!
If you have problems with the missing DATABASE_URL
, add this to devcontainer.json
{
...
// set ENV variables such as DATABASE_URL
"postCreateCommand": "echo 'source /entrypoint' >> ~/.bashrc",
}
This ensures that the environment variables from /entrypoint
get set every time you start an interactive Bash session. However, you need to remove some shell options, so a session does not close whenever a program exists with a non-zero exit code.
# production/django/entrypoint
# comment this out or remove them
# set -o errexit
# set -o pipefail
# set -o nounset
I appreciate this description!
My configuration was getting a database connection error using zsh. I fixed it with this change to devcontainer.json
:
- "postCreateCommand": "apt-get update && apt-get install -y git zsh wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh || true"
+ "postCreateCommand": "wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh || true && echo 'source /entrypoint' >> ~/.zshrc"
and installing git
and wget
in my debug Dockerfile.
Also, I had a problem using docker-compose.override.yml
(can a file be mounted?) so I removed it and the .devcontainer/.zshrc
file. I suppose this means I'll need to edit the container's /root/.zshrc
with any desired changes.
I finally got things working for debugging View code in VS Code. A few notes:
**/compose*
line that VS Code added in .dockerignorelaunch.json
and settings.json
It seems that cookiecutter-django could add some set of these files to provide a VS Code friendly setup.
+1 for out-of-the-box optional VS Code setup.
would you accept a pull request for this issue..?
would you accept a pull request for this issue..?
Yes sure
Any luck?
i have some experience with vscode dev containers, but i've been using cookiecutter for just a couple of weeks, so please be patient if the pull request is too "simple". any feedback and correction is appreciated, i might also write some documentation about it if you want to.
I'm following this issue as I've been using Dev Containers a lot recently and would love to see them in cookie-cutter. If it helps, here's a Dev Container for Django+PostgreSQL: https://github.com/pamelafox/django-quiz-app/tree/main/.devcontainer It uses docker-compose.yaml to start postgreSQL inside the container. I imagine you could add redis to that docker-compose.yaml as well. Let me know if I can help!
It uses docker-compose.yaml to start postgreSQL inside the container. I imagine you could add redis to that docker-compose.yaml as well. Let me know if I can help!
I think that's a bit overkill: why do you need postgres or redis inside the dev container? i've never seen such a setup before...
Two reasons: 1) I find it painful to setup Postgres on the host machine and to make sure DBs have different names across apps. 2) I can use Github Codespaces to develop the apps (all in browser) which is awesome when teaching workshops.
That said, if you're using the Docker option for cookie-cutter, then running docker-compose -f local.yml build
takes care of setting up postgres. I've been using the non-Docker option.
Ah and looking at the original post, their devcontainer.json does reference local.yml.
... then you should try my fork and let me know! :)
you just have to create a new cookiecutter project answering yes to the `use_vscode_devcontainer" question, then open the project folder with vscode and click "open in container".
it takes a few minutes to build the images, but then it should work pretty straightforward...
@masavini Hi, I tried running your fork in WSL2, and got this error:
Start: Run in container: mkdir -p '/tmp/.X11-unix'
mkdir: cannot create directory ‘/tmp/.X11-unix’: File exists
Exit code 1
@masavini Hi, I tried running your fork in WSL2, and got this error:
Start: Run in container: mkdir -p '/tmp/.X11-unix' mkdir: cannot create directory ‘/tmp/.X11-unix’: File exists Exit code 1
then you should comment (or delete) the lines of devcontainer.json
where /tmp
is added as a bind mount.
"mounts": [
{
"source": "./.history/bash_history",
"target": "/home/vscode/.bash_history",
"type": "bind"
},
{
"source": "/tmp",
"target": "/tmp",
"type": "bind"
}
],
should become:
"mounts": [
{
"source": "./.history/bash_history",
"target": "/home/vscode/.bash_history",
"type": "bind"
}
],
@masavini Hi, I tried running your fork in WSL2, and got this error:
Start: Run in container: mkdir -p '/tmp/.X11-unix' mkdir: cannot create directory ‘/tmp/.X11-unix’: File exists Exit code 1
Which OS are you running @usr3?
@browniebroke Windows 11 on host Ubuntu 20.04.5 on WSL2
Hi @masavini, I tried running your fork (or rather, applied the changes from it to my existing project generated from cookiecutter-django), but for some reason pytest doesn't seem to work inside the dev container. Running pytest through docker-compose works fine though. I got the following traceback from running pytest inside the dev container:
@fraserdominicdavid pytest works fine for me on a vanilla project generated from the fork. Are you sure you haven't missed anything from the diff?
@browniebroke sorry for the late reply. As I recall, I have applied everything from the diff. However, I have fixed my issue now. I just followed what @masavini suggested to fix @usr3's error, that is, deleting the following lines from mounts
:
{
"source": "~/.ssh",
"target": "/tmp",
"type": "bind"
},
My machine is on Windows 10, so I'm not sure if this particular issue only happens on Windows machines.
@MBARIMike May I ask you share your setting?
Description
Hi,
I spent some time today setting up vs code with "dev container" to work with cookiecutter-django. At the end the dev experience is pretty good as you can code seemlessly directly from within the "django" container with the python interpreter linting and everything coming from the container.
I will share here my setup but it can definetly be improved and added to
cookiecutter-django
. I did the following things:Rationale
Better DX on VS Code using dev container.
Use case(s) / visualization(s)
Once the project created, I just added a
.devcontainer
folder at the root of my project with the following files:A
devcontainer.json
to set what happens when vs code is attaching to the container:a new
.zshrc
for adding the missing env variable to the shell:and finally the
docker-compose.override.yml
:Let me know what you think about this setup