0

my objective: run mongodb and and my fastapi app in two docker containers. they should be able to talk to each other, and I should be able to access fastapi API on my localhost:8000

i want to run my api server locally using docker container. normally i run my mongo db using docker, and using

docker run --rm -p 27017:27017 mongo

then I run my fastapi application using uvicorn api.main:app

now, i am running my fastapi application within docker container. here is the Dockerfile

FROM python:3.11
ENV VENV_PATH="/venv"
ENV PATH="$VENV_PATH/bin:$PATH"
WORKDIR /app
RUN apt-get update && \
    apt-get install -y --no-install-recommends apt-utils && \
    apt-get upgrade -y && \
    apt-get autoclean
RUN pip install --upgrade poetry
RUN python -m venv /venv
COPY . .
RUN poetry build && \
    /venv/bin/pip install --upgrade pip wheel setuptools &&\
    /venv/bin/pip install dist/*.whl
EXPOSE 8000

CMD uvicorn api.main:app

after building the image, I run

docker run -it --env-file .env --network=host my-api

when I do the above, my application running inside docker container is able to connect to mongo db database at localhost:27017, but the 8000 port of the container is not exposed to my localhost's 8000

if i do this then my application running in docker can easily communicate with MongoDB server exposed at localhost:27017

but I need to expose my docker container's 8000 port to my local host. that I generally do with

docker run -it --env-file .env -p 8000:8000/tcp my-api

in that case the app running inside container cant access mongo db at localhost:27017

when i apply both the network and p(publish) flag, i get

WARNING: Published ports are discarded when using host network mode

i tried using two p flags, 8000:8000 and 27017:27017, and i got

Docker: Error response from daemon: driver failed programming external connectivity on endpoint goofy_ptolemy

(f67673328ee02111a16080a7f3588146981aa7974d2ea8a071815f1ae7ad774f): Bind for 0.0.0.0:27017 failed: port is already allocated.
aahnik
  • 1,661
  • 1
  • 11
  • 29
  • 1
    `--network=host` is rarely necessary; it generally disables Docker's networking layer entirely, so features like published ports and being able to refer to other containers using their container names as host names don't work. If you have both `-p` and `--network=host` and both are accepted then it could definitely cause the "port is already allocated" error; remove `--network=host`. – David Maze Mar 19 '23 at 10:37
  • 1
    Reading your initial description, are you looking for some combination of [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname) and maybe [Connecting to Postgresql in a docker container from outside](https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside) or [What is the difference between "expose" and "publish" in Docker?](https://stackoverflow.com/questions/22111060/what-is-the-difference-between-expose-and-publish-in-docker)? – David Maze Mar 19 '23 at 10:44
  • learning the usage of docker compose, totally solves my issue – aahnik Mar 24 '23 at 08:38

0 Answers0