20
linuxxx
5y

"Let's quickly make sure that the docker container can access a host database"

I guess I can put that quickly up my ass.

How motherfucking difficult can this be?!

Comments
  • 9
    I mean, it's not best practice, but have you considered running the DB as another docker container? :P

    And otherwise either use the host ip stuff provided by docker or pass the socket to the container.

    What might be an issue, when using the ip based method, is that a lot of DBs Block access by default, if it's not comming from localhost, so make sure, that's not the case.
  • 1
    As said above, double check that traffic from your containers ip is allowed (it doesn't come from local host!) and that the container knows the ip to connect to
  • 3
    @epse @Wack Whitelist on database level you mean?

    And the container can ping the bound IP address but when doing an nmap from inside the container, it just does not show the mongo port
  • 4
    @linuxxx yep! By default mysql etc only allow localhost logins and the containers traffic comes from a different ip over a virtual network
  • 2
    @epse I'll look into that! Also, see my previous comment edit 😄
  • 3
    Oooof mongo. Good luck :P

    Are you able to successfully connect to mongo from your host?

    Edit: if you're using ip based: make sure to try to connect to 127.0.0.1 on your host, as it might fall back on socket if no ip/localhost is given.
    If host to mongo works, open the port to the web and try from another computer. If that works, try again from the container. If it works, close the port again and try it again from the container
  • 2
    @Wack Yup that works just fine! But I'm really wondering why I can't even see the port as open while it's listening on that goddamn ip..
  • 2
    Doesn't mongo have a Unix socket? That would be an easy bind on your run command, unless your client doesn't support it.
  • 1
    @sploders101 Think it does, I'll try that!
  • 1
    If you access something not by ip but by dns name from inside a docker container it doesn't always work because docker has its own dns server that does not respect all local dns resolver settings.
    But can't remember how I fixed that...
  • 1
    Been there. Docker has some predefined hostname to access host system. Not sure what it’s called though.
  • 2
    📌
  • 1
    I gave up for now, can use my time better 😅
  • 1
    Your container website cannot connect to db on your host machine? well by default, containers go on a different network 172.xx.xx.xx

    Either move database to a container and make sure you map data folder to docker volume or every restart of that docker will reset the state of your database.

    And also make sure your website container resolve DB container by name rather than by IP
  • 0
    You can use host.docker.internal as the host if the db is on the local machine running the container.
  • 0
    @LMagnus Tried that, its unreachable/doesn't exist according to the container 😅
  • 1
    Can you ping other domains? Like can you ping duckduckgo or something Like that from inside the container? I kind of get the feeling, the containers networking is fucked...
  • 1
    @Wack I'll try tomorrow!
Add Comment