I am so new in Docker and I have dockerized a simple django application and this is Dockerfile :
FROM python:3.8-slim-buster
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY . .
RUN pip freeze > requirements.txt
RUN pip3 install -r requirements.txt
CMD ["python3","manage.py", "runserver","0.0.0.0:8000
and this 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:
- d
and this is the packages in requirements.txt :
asgiref==3.4.1
Django==3.2.9
pytz==2021.3
sqlparse==0.4.2
i have created image myapp 4 successfuly and when i tried to run this image with dcoker run myapp4 i got the following error :
Traceback (most recent call last):
File "manage.py", line 11, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 13, in main
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
what am i missing here?
Related
I added the codes to below i hope it is clear and precise
Dockerfile:
FROM python:3.8.5-alpine
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./ddc /app
WORKDIR /app
COPY ./entrypoint.sh /
ENTRYPOINT ["sh", "/entrypoint.sh"]
docker-compose.yml
version: '3.7'
services:
django_gunicorn:
volumes:
- static:/static
env_file:
- .env
build:
context: .
ports:
- "8000:8000"
nginx:
build: ./nginx
volumes:
- static:/static
ports:
- "80:80"
depends_on:
- django_gunicorn
volumes:
static:
entrypoint.sh
#!/bin/sh
python manage.py migrate --no-input
python manage.py collectstatic --no-input
gunicorn ddc.wsgi:application --bind 0.0.0.0:8000
First i use docker-compose up --build
and this is what i get:
Building django_gunicorn
Sending build context to Docker daemon 19.46kB
Step 1/8 : FROM python:3.8.5-alpine
---> 0f03316d4a27
Step 2/8 : RUN pip install --upgrade pip
---> Using cache
---> ac5d6a64af93
Step 3/8 : COPY ./requirements.txt .
---> Using cache
---> 8dfb848be8a4
Step 4/8 : RUN pip install -r requirements.txt
---> Using cache
---> 0dee442b9c0a
Step 5/8 : COPY ./ddc /app
---> 21c33e5463d8
Step 6/8 : WORKDIR /app
---> Running in 9153438d9466
Removing intermediate container 9153438d9466
---> d27b60805a1b
Step 7/8 : COPY ./entrypoint.sh /
---> e497fecdfb76
Step 8/8 : ENTRYPOINT ["sh", "/entrypoint.sh"]
---> Running in f6eb59759a71
Removing intermediate container f6eb59759a71
---> 6db361baa8e8
Successfully built 6db361baa8e8
Successfully tagged django-docker-compose_django_gunicorn:latest
Traceback (most recent call last):
File "docker-compose", line 3, in <module>
File "compose/cli/main.py", line 81, in main
File "compose/cli/main.py", line 203, in perform_command
File "compose/metrics/decorator.py", line 18, in wrapper
File "compose/cli/main.py", line 1186, in up
File "compose/cli/main.py", line 1182, in up
File "compose/project.py", line 664, in up
File "compose/service.py", line 348, in ensure_image_exists
File "compose/service.py", line 1133, in build
File "compose/service.py", line 1948, in build
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmp5cv0a52z'
[22049] Failed to execute script docker-compose
After that i just use
docker-compose up
and the system works as it is expected. What does this error means and how do i avoid it. I use the latest docker and compose which are:
docker:Docker version 19.03.13, build cd8016b6bc
docker-compose version 1.29.2, build 5becea4c
The issue is resolved after i change my installation from ubuntu snap to
https://docs.docker.com/engine/install/ubuntu/
https://docs.docker.com/compose/install/
./project
/site_ts
/web
Dockerfile
requirements.txt
docker-compose.yml
my docker-compose.yml:
version: '3'
services:
web:
build: ./web
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/site_ts
ports:
- '8000:8000'
my Dockerfile:
FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /site_ts
WORKDIR /site_ts
COPY requirements.txt /site_ts/
RUN pip install --upgrade pip && pip install -r requirements.txt
ADD . /site_ts/
i write docker-compose up and take this Error:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "docker-compose", line 3, in <module>
File "compose\cli\main.py", line 67, in main
File "compose\cli\main.py", line 123, in perform_command
File "compose\cli\command.py", line 69, in project_from_options
File "compose\cli\command.py", line 132, in get_project
File "compose\cli\docker_client.py", line 43, in get_client
File "compose\cli\docker_client.py", line 170, in docker_client
File "site-packages\docker\api\client.py", line 188, in __init__
File "site-packages\docker\api\client.py", line 213, in _retrieve_server_version
docker.errors.DockerException: Error while fetching server API version: (2, 'CreateFile', 'The specified file cannot be found.')
[9356] Failed to execute script docker-compose
I'm trying to create a Django app in a docker container. The app would use a postgres db with postgis extension, which I have in another database. I'm trying to solve this using docker-compose but can not get it working.
I can get the app working without the container with the database containerized just fine. I can also get the app working in a container using a sqlite db (so a file included without external container dependencies). Whatever I do, it can't find the database.
My docker-compose file:
version: '3.7'
services:
postgis:
image: kartoza/postgis:12.1
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- "${POSTGRES_PORT}:5432"
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
env_file:
- .env
web:
build: .
# command: sh -c "/wait && python manage.py migrate --no-input && python /code/app/manage.py runserver 0.0.0.0:${APP_PORT}"
command: sh -c "python manage.py migrate --no-input && python /code/app/manage.py runserver 0.0.0.0:${APP_PORT}"
# restart: on-failure
ports:
- "${APP_PORT}:8000"
volumes:
- .:/code
depends_on:
- postgis
env_file:
- .env
environment:
WAIT_HOSTS: 0.0.0.0:${POSTGRES_PORT}
volumes:
postgres_data:
name: ${POSTGRES_VOLUME}
My Dockerfile (of the app):
# Pull base image
FROM python:3.7
LABEL maintainer="yb.leeuwen#portofrotterdam.com"
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
# RUN pip install pipenv
RUN pip install pipenv
RUN mkdir /code/
COPY . /code
WORKDIR /code/
RUN pipenv install --system
# RUN pipenv install pygdal
RUN apt-get update &&\
apt-get install -y binutils libproj-dev gdal-bin python-gdal python3-gdal postgresql-client
## Add the wait script to the image
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
RUN chmod +x /wait
# set work directory
WORKDIR /code/app
# RUN python manage.py migrate --no-input
# CMD ["python", "manage.py", "migrate", "--no-input"]
# RUN cd ${WORKDIR}
# If we want to run docker by itself we need to use below
# but if we want to run from docker-compose we'll set it there
EXPOSE 8000
# CMD /wait && python manage.py migrate --no-input
# CMD ["python", "manage.py", "migrate", "--no-input"]
# CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
My .env file:
# POSTGRES
POSTGRES_PORT=25432
POSTGRES_USER=username
POSTGRES_PASSWORD=pass
POSTGRES_DB=db
POSTGRES_VOLUME=data
POSTGRES_HOST=localhost
# GEOSERVER
# DJANGO
APP_PORT=8000
And finally my in my settings.py of the django app:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': os.getenv('POSTGRES_DBNAME'),
'USER': os.getenv('POSTGRES_USER'),
'PASSWORD': os.getenv('POSTGRES_PASS'),
'HOST': os.getenv("POSTGRES_HOST", "localhost"),
'PORT': os.getenv('POSTGRES_PORT')
}
}
I've tried quite a lot of things (as you see in some comments). I realized that docker-compose doesn't seem to wait until postgres is fully up, spinning and accepting requests so I tried to build in a waiting function (as suggested on the website). I first had migrations and running the server inside the Dockerfile (migrations in the build process and runserver as the startup command), but that requires postgres and as it wasn't waiting for it it didn't function. I finally took it all out to the docker-compose.yml file but still can't get it working.
The error I get:
web_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 | TCP/IP connections on port 25432?
web_1 | could not connect to server: Cannot assign requested address
web_1 | Is the server running on host "localhost" (::1) and accepting
web_1 | TCP/IP connections on port 25432?
Does anybody have an idea why this isn't working?
I see that in your settings.py of the django app, you are connecting to Postgres via
'HOST': os.getenv("POSTGRES_HOST", "localhost"),
While in .env you are setting the value of to POSTGRES_HOST to localhost. This means that the web container is trying to reach the Postgres server postgis at localhost which should not be the case.
In order to solve this problem, simply update your .env file to be like this:
POSTGRES_PORT=5432
...
POSTGRES_HOST=postgis
...
The reason is that in your case, the docker-compose brings up 2 containers: postgis and web inside the same Docker network and they can reach each other via their DNS name i.e. postgis and web respectively.
Regarding the port, web container can reach postgis at port 5432 but not 25432 while your host machine can reach the database at port 25432 but not 5432
you can not use localhost for the docker containers, it will be pointing to the container itself, not to the host of the containers. Instead switch to use the service name.
to fix the issue, change your env to
# POSTGRES
POSTGRES_PORT=5432
POSTGRES_USER=username
POSTGRES_PASSWORD=pass
POSTGRES_DB=db
POSTGRES_VOLUME=data
POSTGRES_HOST=postgis
# DJANGO
APP_PORT=8000
and you compose file to
version: '3.7'
services:
postgis:
image: kartoza/postgis:12.1
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
env_file:
- .env
web:
build: .
# command: sh -c "/wait && python manage.py migrate --no-input && python /code/app/manage.py runserver 0.0.0.0:${APP_PORT}"
command: sh -c "python manage.py migrate --no-input && python /code/app/manage.py runserver 0.0.0.0:${APP_PORT}"
# restart: on-failure
ports:
- "${APP_PORT}:8000"
volumes:
- .:/code
depends_on:
- postgis
env_file:
- .env
environment:
WAIT_HOSTS: postgis:${POSTGRES_PORT}
volumes:
postgres_data:
name: ${POSTGRES_VOLUME}
Getting error while installing GeoDjango on docker with postgres db.
I'm completely new to docker and i'm setting up my project on docker. I don't know, is this error is regarding django or postgres.
Found this error
AttributeError: /usr/lib/libgdal.so.1: undefined symbol:
OGR_F_GetFieldAsInteger64 While installing in Docker
docker-compose.yml
version: '3'
services:
postgres:
restart: always
image: postgres:alpine
volumes:
- ./postgres_gis/gis_db:/home/dev/gis_db.sql
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: Dev#mishra123
POSTGRES_DB: gis_db
expose:
- "5432"
web:
build: ./HomePage
restart: always
expose:
- "8000"
volumes:
- ./HomePage:/home/dev/app/HomePage
depends_on:
- postgres
environment:
- DEBUG=1
web/Dockerfile
from python:3.6.2-slim
RUN groupadd dev && useradd -m -g dev -s /bin/bash dev
RUN echo "dev ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN mkdir -p /home/dev/app/HomePage
RUN chown -R dev:dev /home/dev/app
RUN chmod -R +x+r+w /home/dev/app
WORKDIR /home/dev/app/HomePage
RUN apt-get update && apt-get -y upgrade
COPY requirements.txt /home/dev/app/HomePage
RUN apt-get install -y python3-dev python3-pip
RUN apt-get install -y libpq-dev
RUN apt-get install -y binutils libproj-dev gdal-bin
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
USER dev
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
web/docker-entrypoint.sh
#!/bin/sh
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
docker-compose ps :
root#BlackCat:/home/dev/Project-EX/django-PR# docker-compose up
Starting django-pr_postgres_1 ... done
Starting django-pr_web_1 ... done
Attaching to django-pr_postgres_1, django-pr_web_1
postgres_1 | 2019-12-12 16:34:43.907 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2019-12-12 16:34:43.908 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2019-12-12 16:34:44.099 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2019-12-12 16:34:44.672 UTC [18] LOG: database system was shut down at 2019-12-11 18:45:45 UTC
postgres_1 | 2019-12-12 16:34:44.912 UTC [1] LOG: database system is ready to accept connections
web_1 | Traceback (most recent call last):
web_1 | File "manage.py", line 22, in <module>
web_1 | execute_from_command_line(sys.argv)
web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
web_1 | utility.execute()
web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 377, in execute
web_1 | django.setup()
web_1 | File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
web_1 | app_config.import_models()
web_1 | File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
web_1 | self.models_module = import_module(models_module_name)
web_1 | File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
web_1 | return _bootstrap._gcd_import(name[level:], package,
web_1 | func = self.__getitem__(name)
web_1 | File "/usr/local/lib/python3.6/ctypes/__init__.py", line 366, in __getitem__
web_1 | func = self._FuncPtr((name_or_ordinal, self))
web_1 | AttributeError: /usr/lib/libgdal.so.1: undefined symbol: OGR_F_GetFieldAsInteger64
web_1 | Watching for file changes with StatReloader
The problem you are encountering is that your version of GDAL is too old. Your Dockerfile is built on python:3.6.2-slim, which is based off Debian Jessie, and installs gdal version 1.10.1. The OGR_F_GetFieldAsInteger64 variable was introduced in v. 2.0.0
According to the GDAL package page at debian.org, you need a newer version of Debian (stretch, buster, bullseye will all work). As such, I would recommend that you change your Dockerfile to use python:3.8.0-slim-buster or newer. Please check the hub.docker.com python page for more information
Also, as mentioned in the comments, your Dockerfile should only have one of CMD or ENTRYPOINT, but not both. Since your entrypoint.sh does what CMD does and more, I'd just remove CMD and stick with ENTRYPOINT
Disclosure: I work for EnterpriseDB (EDB)
I had a similar issue, without Docker. I fixed it with this:
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt-get update
sudo apt-get install gdal-bin
More info: https://developers.planet.com/planetschool/gdal-qgis-installation-setup/
I'm having an issue in my docker bash, i'm trying to create a super user on django using docker-compose exec web python manage.py createsuperuser but I have this error below.
Traceback (most recent call last):
File "docker-compose", line 3, in <module>
File "compose\cli\main.py", line 68, in main
File "compose\cli\main.py", line 118, in perform_command
File "compose\cli\main.py", line 431, in exec_command
File "compose\cli\main.py", line 1236, in call_docker
File "distutils\spawn.py", line 220, in find_executable
File "ntpath.py", line 85, in join
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 8: ordinal not in range(128)
Failed to execute script docker-compose
I think it's because my database Postgresql is encoded in 'ascii' instead of utf-8, what are the commands to encode my psql database to utf-8?
Dockerfile
FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN mkdir /config
ADD /config/requirements.pip /config/
RUN pip install -r /config/requirements.pip
RUN mkdir /src;
WORKDIR /src
Docker-compose.yml
version: '2'
services:
nginx:
image: nginx:latest
container_name: NGINX
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static
- /media:/media
depends_on:
- web
web:
restart: always
build: .
container_name: DJANGO
command: bash -c "python manage.py collectstatic --noinput && python manage.py makemigrations && python manage.py migrate && gunicorn oqtor.wsgi -b 0.0.0.0:8000"
depends_on:
- db
volumes:
- ./src:/src
- /static:/static
- /media:/media
expose:
- "8000"
db:
image: postgres:latest
container_name: PSQL
You have a tilde character ("é" ) in your docker-compose.yml
Edit. Probably you have accents in the involved paths and probably you are facing some python bug in your host. You can try updating python in the host (docker-compose is made in python).