Code navigation lost in VScode with Django + Docker - django

I could not find the code definition using Ctrl + click when I use Docker + Django in my project, since source code are now in container. How could I configure my VScode to enable code navigation?

Here is some kind of hack that i am using right now. create a virtual environment.
python3 -m venv venv
Activate the virtual environment in terminal using
source venv/bin/activate
now install dependencies from requirements.txt
pip3 install -r requirements.txt
configure IDE with virtual enviornment. this will give you snippets etc. Now run commands inside the container
docker-compose -f local.yml run --rm django python manage.py migrate
docker-compose -f local.yml run --rm django python manage.py runserver

Related

Running django developing server in a docker container

So I'm trying to run Django developing server on a container but I can't access it through my browser. I have 2 containers using the same docker network, one with postgress and the other is Django. I manage to ping both containers and successfully connect 2 of them together and run ./manage.py runserver ok but can't curl or open it in a browser
Here is my Django docker file
FROM alpine:latest
COPY ./requirements.txt .
ADD ./parking/ /parking
RUN apk add --no-cache --virtual .build-deps python3-dev gcc py3-pip postgresql-dev py3-virtualenv musl-dev libc-dev linux-headers
RUN virtualenv /.env
RUN /.env/bin/pip install -r /requirements.txt
WORKDIR /parking
EXPOSE 8000 5432
The postgres container I pulled it from docker hub
I ran django with
docker run --name=django --network=app -p 127.4.3.1:6969:8000 -it dev/django:1.0
I ran postgres with
docker run --name=some-postgres --network=app -p 127.2.2.2:6969:5432 -e POSTGRES_PASSWORD=123 -e POSTGRES_DB=parking postgres
Any help would be great. Thank you
I think you forget to add the command to run your application at the and of the dockerfile, when you run this image it just start the virtualenv and install all python dependencies at the requirements.txt, but the django application was not started. You need put at the end something like
CMD "python parking/manage.py runserver"
this will make your container still running at the choosed port and make you application accessible at 127.4.3.1:6969:8000.
Okay so I manage to figure it out, I have looked at #leonardo-alves-dos-santos answer and I come to the conclusion that I run CMD "python parking/manage.py runserver 0.0.0.0:8000" . Now I can access my Django app with Django container port 127.4.3.1:6969 and 172.18.0.2:8000 from docker network

gunicorn: command not found django

Using Docker to install gunicorn, I am unable to to use the gunicorn command.
To start Django, I have this line in my docker-compose.yaml:
command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn myproject.wsgi -b 0.0.0.0:8000"
This results in bash: gunicorn: command not found
When I build the Docker images it says gunicorn has been successfully installed.
My Dockerfile looks like:
FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN mkdir /config
ADD requirements.txt /config/
RUN pip install -r /config/requirements.txt
RUN mkdir /src;
WORKDIR /src
I've been using this http://ruddra.com/2016/08/14/docker-django-nginx-postgres/ as a guide.
If you are finding that gunicorn doesn't exist it could be because
docker image may use a cached layer of the requirements.txt which doesn't have gunicorn in it as a dependency.
Therefore it will result in not installing gunicorn meanwhile specifying pip install gunicorn in a seperate RUN command will work.
Solution:
Build docker image without caching when edits have been made to requirements.txt
docker build --no-cache .

Django running in Docker container: makemigrations and migrate do not see app's model on launch

I have Django running in a Docker container. The CMD of my Docker file simply runs a script, launch.sh, which inter alia has the following commands:
python manage.py makemigrations --no-input --verbosity 1
python manage.py migrate --no-input --verbosity 1
So, these commands make migrations on my Django project, and then perform the migrations (if any), whenever my container launches. This works as intended for the specifically project-level migrations.
However, inevitably, only the project-level migrations are made — that is, the app-level migrations are never made and so are never performed. But if I log into the container (with docker exec -it ... bash) and execute the same migration commands manually, the app-level migrations are made and performed.
Googling and numerous variations to my code haven't turned up any explanations for this behavior or any fix, so I'm stumped.
Any ideas?
P.S. Here is my project and app structure:
/django/
project/
app/
static/
manage.py
Also, I tried executing the same set of commands twice in succession in my script, and also running the same set of commands in succession but with my app specified as the target option, but these attempts still produced the same results: only the project migrations are made, not the app migrations.
As asked, here's my Dockerfile:
FROM python:3-slim
ENV PYTHONUNBUFFERED 1
ADD django-requirements.pip .
RUN pip install --upgrade pip && \
pip install --no-cache-dir -r django-requirements.pip
WORKDIR /
ADD launch.sh .
CMD ./launch.sh
My Django project is mounted at launch at /django, and my launch script CDs to /django before running the migration commands.
Check your Django app Dokerfile for WORKDIR
# In my case it is /app
WORKDIR /app
and change your launch.sh file
# manage.py will be inside working dir
python /app/manage.py migrate --noinput
UPDATE
It depends on where you copied launch.sh file inside the
container.
If you copied all files of Django app inside /app dir
COPY . /app
and copy your launch.sh file outside it like
COPY ./<path to launch file>/launch.sh /launch.sh
then inside launch.sh you have to use manage.py as
# should prepend `/app/`
python /app/manage.py migrate --noinput
But if you copied launch.sh inside /app/ as.
COPY ./<path to launch file>/launch.sh /app/launch.sh
Then you can use migrate command as the traditional way
python manage.py migrate --noinput
Now When you run the command using docker exec -it container-id, Then it runs
inside working DIR and locates manage.py file.
I had exactly the same problem.
I think there must be a migrations/__init__.py in your Django app dir.
Be sure that you copied it to your container.
My solution was to change a line in .dockerignore:
app/migrations/* to app/migrations/0*.

django-cookiecutter, docker-compose run: can't type in terminal

I'm having an issue with a project started with django cookiecutter.
To verify that the issue was not with my project, I'm testing on a blank django cookiecutter project.
The issue is that when I run:
docker-compose -f production.yml run --rm django python manage.py createsuperuser
I get the prompt but can't type in the terminal.
Same thing when I run:
docker-compose -f production.yml run --rm django python manage.py shell
I get the shell prompt, but I can't type.
The app is running on a machine on DigitalOcean created with the docker-machine create command.
Any thoughts on what the issue could be and how I could debug this?
to enable typing in docker-compose terminal, you need to specify the terminal session is interactive on the docker-compose.yml. Because by default, docker console is not interactive.
stdin_open: true
tty: true
bash can be accessed inside the docker container using the following command.
docker-compose -f production.yml exec django bash
This will give you full access to the container of your django server.
There you can run all your normal django commands with full interactivity.
Create Superuser
python manage.py createsuperuser
For Local ENV
docker-compose -f local.yml exec django bash

Django build is taking so much time in jenkins?

I am using jenkins for continuous integration from gitlab and continuous deployment. My "execute shell" consist of the following commands .
#!/bin/bash
source /my_env/bin/activate # Activate the virtualenv
cd /var/lib/jenkins/workspace/Operations_central
#pip install -r requirements.txt # Install or upgrade dependencies
python manage.py makemigrations
python manage.py migrate # Apply South's database
sudo service nginx restart
fuser -n tcp -k 8088
gunicorn applicationfile.wsgi:application --bind=myserverip:portno`