0

I m new to docker. I ve downloaded a docker image at docker pull lambci/lambda:build-python3.8
which is a aws image to run lambda functions.

I ve a Dockerfile that is : Dockerfile:

FROM lambci/lambda:build-python3.8
WORKDIR /var/task
EXPOSE 8000
RUN echo 'export PS1="\[\e[36m\]zappashell>\[\e[m\] "' >> /root/.bashrc
CMD ["bash"]

I've run

docker build -t zappa-docker-image .

docker run -ti -p 8000:8000 -e AWS_PROFILE=zappa -v "$(pwd):/var/task" -v ~/.aws/:/root/.aws --rm zappa-docker-image

When I run django inside my docker image (python manage.py runserver), i get the following error:

OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
    Is the server running on that host and accepting TCP/IP connections?

I guess it's related to postgresql in my django settings :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbtest', 
        'USER': 'postgres', 
        'PASSWORD': '1234',
        'HOST': 'localhost', 
        'PORT': '5432',
    }
}

When I run the django server outside docker i dont get the error ... How can i solve this error in docker?

kr1p
  • 135
  • 6
  • When you run the program inside the container, the container becomes a new `localhost`. Depending on your host OS, you'll need to modify the Django `'HOST'` setting to connect back to the database on your host system; the linked question has details. – David Maze May 25 '22 at 17:27
  • (Don't forget to `COPY` your application into the image, and set its `CMD` to start the application. You should be able to `docker run` the image without having the application source code on your host system and without involving an interactive shell.) – David Maze May 25 '22 at 17:28

0 Answers0