Docker: User localhost instead of db to connect to PostgresDatabase












0















I'm running into problems when using Docker. To connect from Django application to the Postgres database I have to use:



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}


However to run tests via pytest in my Pipenv shell I have to change 'Host': from db to localhost. Is there a way that I can always use localhost?



Docker-Compose:



version: '3'

services:
db:
image: postgres
ports:
- "5432:5432"
web:
build: .
env_file: .env
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
container_name: test


Dockerfile:



# Pull base image
FROM python:3

# Set environment varibles
ENV PYTHONUNBUFFERED 1

# Set work directory
RUN mkdir /code
WORKDIR /code

# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev

# Define ENTRYPOINT
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

# Copy project
COPY . /code/









share|improve this question























  • May I ask why you don't want to use docker postgres db for testing?

    – ruddra
    Nov 23 '18 at 8:12











  • Why aren't you executing your tests by executing python manage.py test? Django will create a test database on its own.

    – Red Cricket
    Nov 23 '18 at 8:14













  • It would actually be my preferred approach @ruddra I tried to run pytest by accessing docker exec -it test bash However, the tests seem to have no access to the database either. (Or have no access to create a test database.) It always fails.

    – Joey Coder
    Nov 23 '18 at 8:31






  • 1





    @JonProgrammer I have similar setup as yours, I face no problem running tests from shell. My setup is available in github

    – ruddra
    Nov 23 '18 at 8:46











  • Thank you for sharing it ruddra. Do you also use pytest in your shell?

    – Joey Coder
    Nov 23 '18 at 9:02
















0















I'm running into problems when using Docker. To connect from Django application to the Postgres database I have to use:



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}


However to run tests via pytest in my Pipenv shell I have to change 'Host': from db to localhost. Is there a way that I can always use localhost?



Docker-Compose:



version: '3'

services:
db:
image: postgres
ports:
- "5432:5432"
web:
build: .
env_file: .env
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
container_name: test


Dockerfile:



# Pull base image
FROM python:3

# Set environment varibles
ENV PYTHONUNBUFFERED 1

# Set work directory
RUN mkdir /code
WORKDIR /code

# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev

# Define ENTRYPOINT
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

# Copy project
COPY . /code/









share|improve this question























  • May I ask why you don't want to use docker postgres db for testing?

    – ruddra
    Nov 23 '18 at 8:12











  • Why aren't you executing your tests by executing python manage.py test? Django will create a test database on its own.

    – Red Cricket
    Nov 23 '18 at 8:14













  • It would actually be my preferred approach @ruddra I tried to run pytest by accessing docker exec -it test bash However, the tests seem to have no access to the database either. (Or have no access to create a test database.) It always fails.

    – Joey Coder
    Nov 23 '18 at 8:31






  • 1





    @JonProgrammer I have similar setup as yours, I face no problem running tests from shell. My setup is available in github

    – ruddra
    Nov 23 '18 at 8:46











  • Thank you for sharing it ruddra. Do you also use pytest in your shell?

    – Joey Coder
    Nov 23 '18 at 9:02














0












0








0








I'm running into problems when using Docker. To connect from Django application to the Postgres database I have to use:



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}


However to run tests via pytest in my Pipenv shell I have to change 'Host': from db to localhost. Is there a way that I can always use localhost?



Docker-Compose:



version: '3'

services:
db:
image: postgres
ports:
- "5432:5432"
web:
build: .
env_file: .env
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
container_name: test


Dockerfile:



# Pull base image
FROM python:3

# Set environment varibles
ENV PYTHONUNBUFFERED 1

# Set work directory
RUN mkdir /code
WORKDIR /code

# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev

# Define ENTRYPOINT
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

# Copy project
COPY . /code/









share|improve this question














I'm running into problems when using Docker. To connect from Django application to the Postgres database I have to use:



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}


However to run tests via pytest in my Pipenv shell I have to change 'Host': from db to localhost. Is there a way that I can always use localhost?



Docker-Compose:



version: '3'

services:
db:
image: postgres
ports:
- "5432:5432"
web:
build: .
env_file: .env
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
container_name: test


Dockerfile:



# Pull base image
FROM python:3

# Set environment varibles
ENV PYTHONUNBUFFERED 1

# Set work directory
RUN mkdir /code
WORKDIR /code

# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev

# Define ENTRYPOINT
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

# Copy project
COPY . /code/






django docker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 8:07









Joey CoderJoey Coder

32210




32210













  • May I ask why you don't want to use docker postgres db for testing?

    – ruddra
    Nov 23 '18 at 8:12











  • Why aren't you executing your tests by executing python manage.py test? Django will create a test database on its own.

    – Red Cricket
    Nov 23 '18 at 8:14













  • It would actually be my preferred approach @ruddra I tried to run pytest by accessing docker exec -it test bash However, the tests seem to have no access to the database either. (Or have no access to create a test database.) It always fails.

    – Joey Coder
    Nov 23 '18 at 8:31






  • 1





    @JonProgrammer I have similar setup as yours, I face no problem running tests from shell. My setup is available in github

    – ruddra
    Nov 23 '18 at 8:46











  • Thank you for sharing it ruddra. Do you also use pytest in your shell?

    – Joey Coder
    Nov 23 '18 at 9:02



















  • May I ask why you don't want to use docker postgres db for testing?

    – ruddra
    Nov 23 '18 at 8:12











  • Why aren't you executing your tests by executing python manage.py test? Django will create a test database on its own.

    – Red Cricket
    Nov 23 '18 at 8:14













  • It would actually be my preferred approach @ruddra I tried to run pytest by accessing docker exec -it test bash However, the tests seem to have no access to the database either. (Or have no access to create a test database.) It always fails.

    – Joey Coder
    Nov 23 '18 at 8:31






  • 1





    @JonProgrammer I have similar setup as yours, I face no problem running tests from shell. My setup is available in github

    – ruddra
    Nov 23 '18 at 8:46











  • Thank you for sharing it ruddra. Do you also use pytest in your shell?

    – Joey Coder
    Nov 23 '18 at 9:02

















May I ask why you don't want to use docker postgres db for testing?

– ruddra
Nov 23 '18 at 8:12





May I ask why you don't want to use docker postgres db for testing?

– ruddra
Nov 23 '18 at 8:12













Why aren't you executing your tests by executing python manage.py test? Django will create a test database on its own.

– Red Cricket
Nov 23 '18 at 8:14







Why aren't you executing your tests by executing python manage.py test? Django will create a test database on its own.

– Red Cricket
Nov 23 '18 at 8:14















It would actually be my preferred approach @ruddra I tried to run pytest by accessing docker exec -it test bash However, the tests seem to have no access to the database either. (Or have no access to create a test database.) It always fails.

– Joey Coder
Nov 23 '18 at 8:31





It would actually be my preferred approach @ruddra I tried to run pytest by accessing docker exec -it test bash However, the tests seem to have no access to the database either. (Or have no access to create a test database.) It always fails.

– Joey Coder
Nov 23 '18 at 8:31




1




1





@JonProgrammer I have similar setup as yours, I face no problem running tests from shell. My setup is available in github

– ruddra
Nov 23 '18 at 8:46





@JonProgrammer I have similar setup as yours, I face no problem running tests from shell. My setup is available in github

– ruddra
Nov 23 '18 at 8:46













Thank you for sharing it ruddra. Do you also use pytest in your shell?

– Joey Coder
Nov 23 '18 at 9:02





Thank you for sharing it ruddra. Do you also use pytest in your shell?

– Joey Coder
Nov 23 '18 at 9:02












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442774%2fdocker-user-localhost-instead-of-db-to-connect-to-postgresdatabase%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442774%2fdocker-user-localhost-instead-of-db-to-connect-to-postgresdatabase%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?