Service makemigrations has neither an image nor a build context specified - django

Hello I am trying to make workable a project. But when I run the following instruction :
docker-compose up I got the following error :
ERROR: The Compose file is invalid because:
Service makemigrations has neither an image nor a build context specified. At least one must be provided.
Here is the yml file docker-compose.yml :
version: '3'
services:
db:
image: postgres
expose:
- "5432"
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/myproject/
ports:
- "8000:8000"
- "3000:3000"
depends_on:
- db
- makemigrations
- migrations
makemigrations:
command: python manage.py makemigrations --noinput
volumes:
- .:/myproject/
depends_on:
- db
migrations:
command: python manage.py migrate --noinput
volumes:
- .:/myproject/
depends_on:
- db
Could you help me please ?

Related

Database not working running tests in Django with PostgreSQL in docker compose

I hope you are doing well!
I am having a problem. I am trying to run my Django tests inside of my container with docker compose with the command line sudo docker compose run --rm app sh -c 'python3 manage.py test', but I am receiving these logs
I am not pretty sure what is happening here. I am not noticing something weird in my docker-compose file either. I have tried cleaning the volumes, deleting all the images, and building again just in case I made something wrong through the process but it didn't fix my problem. I will let you know the file just in case.
version: "3.9"
services:
app:
build:
context: .
args:
- DEV=true
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=dev
- DB_USER=devuser
- DB_PASS=changeme
container_name: django_container
depends_on:
- db
db:
image: postgres
container_name: postgresql_db
restart: always
volumes:
- dev-db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=dev
- POSTGRES_USER=devuser
- POSTGRES_PASSWORD=changeme
volumes:
dev-db-data:

django.db.utils.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution on highload

I have the django project which works with PostGresql db, both in docker containers.
It works Ok, but on highload sometimes gives the
django.db.utils.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution
error.
Is there a way to tune the django db connector for more retries or more timeout to solve these case?
docker-compose.yml (with some changes) :
version: '3.5'
services:
django:
build:
context: ./django
dockerfile: Dockerfile
command:
python manage.py runserver 0.0.0.0:8000
volumes:
django_volume:/home/django/django/files/
ports:
8000:8000
env_file:
env.django
depends_on:
db
db:
build:
context: ./db
dockerfile: Dockerfile
volumes:
db_volume:/var/lib/postgresql/data/
env_file:
env.db
volumes:
django_volume:
db_volume:
django container's entrypoint.sh contains the cycle:
while ! nc -x $DB_HOST $DB_PORT ; do
sleep 0.1
done
exec "$#"
To be sure that db completely started before django start.

Seed Django + Postgres with .sql file

I have a .sql file which I'd like to seed my django + postgres app with on build. That way I'll have some data in my app during development.
Here's my docker stuff:
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip3 install -r requirements.txt
COPY . /code/
docker-compose.yml
version: "3.9"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Django supports this usecase with fixtures: https://docs.djangoproject.com/en/3.2/howto/initial-data/
You can serialize your .sql database into a json file and feed that during build with the loaddata command.

Migrations doesn't starts for dockerized Django project

I've created e simple hello world project with Django and Docker. At the end of Dockerfile there is the command below:
ENTRYPOINT ["/app/web/entrypoint.sh"]
that activate this script:
#!/bin/sh
echo " ---> DB Connection Parameters: \
DB name: $DB_NAME \
DB host: $DB_HOST \
DB port: $DB_PORT"
poetry run python3 website/manage.py migrate --noinput
poetry run python3 website/manage.py collectstatic --noinput
poetry run python3 website/manage.py createsuperuser --noinput
echo " ---> Django Project Port: $PROJECT_PORT"
poetry run python3 website/manage.py runserver 0.0.0.0:"$PROJECT_PORT"
exec "$#"
The project starts but the migration isn't run and I'm forced to log inside the container and use python3 manage.py migrate to start the migration.
Why the migration command doesn't run and collectstatic runs automatically?
Below the docker-compose:
version: '3.7'
services:
db:
container_name: dev_db
image: postgis/postgis
restart: always
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_USER: ${DB_USER}
POSTGRES_DB: ${DB_NAME}
volumes:
- db-data:/var/lib/postgresql/data
website:
image: maxdragonheart/${PROJECT_NAME}
build:
context: ./web
dockerfile: Dockerfile
environment:
PROJECT_NAME: ${PROJECT_NAME}
PROJECT_PORT: ${PROJECT_PORT}
SECRET_KEY: ${SECRET_KEY}
DB_ENGINE: ${DB_ENGINE}
DB_NAME: ${DB_NAME}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_PORT: ${DB_PORT}
DB_HOST: ${DB_HOST}
DEBUG: ${DEBUG}
ALLOWED_HOSTS: ${ALLOWED_HOSTS}
container_name: dev_website
restart: always
ports:
- ${PROJECT_PORT}:${PROJECT_PORT}
volumes:
- website-static-folder:/app/web/static-folder
- website-media-folder:/app/web/media-folder
- logs:/app/logs
depends_on:
- db
volumes:
db-data:
website-static-folder:
website-media-folder:
logs:
Docker runs manage.py collectstatic --noinput automatically, you don't have to run command for it. For custom commands, you can try adding this:
website:
...
command: bash -c "python3 website/manage.py migrate --noinput &&
python3 website/manage.py createsuperuser --noinput &&
python3 website/manage.py runserver 0.0.0.0:[YOUR PORT]"
...
to your docker-compose.yml file.

What is the best way to volume existing database to the container?

Need to run project via docker containers. I need to mount existing database to postgres container. Have the next in my docker-compose.yml
services:
web:
build: .
env_file: .env
command: bash -c "python manage.py collectstatic --no-input && python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
ports:
- "8000:8000"
depends_on:
- redis
- postgres
restart: always
volumes:
- static:/static
expose:
- 8000
environment:
- .env
links:
- postgres
- redis
redis:
image: "redis:alpine"
postgres:
image: "postgres:10"
env_file:
- .env
volumes:
- POSTGRES_DATA:/var/lib/postgresql/data
ports:
- "5433:5432"
expose:
- 5433
volumes:
POSTGRES_DATA:
static:
From my .env file
POSTGRES_NAME=dbname
POSTGRES_USER=dbuser
POSTGRES_PASSWORD=dbpassword
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DATA=/var/lib/postgresql/10/main
But inside my web container I have next logs
File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: database "dbname" does not exist
It means that databse mount failed. But I really can not find reason why it happens.
I'm not sure, but you can try this command: docker-compose down -v to remove the volumes along with the containers