0

I don’t know whether you would have seen an issue like this before but when I’m using Laravel which is what I use all the time with Docker I have an issue.

For anyone who knows Laravel knows you get the .env file. In this env file we have to set the DB_HOST. Now locally the DB_HOST is 127.0.0.1 whereas within the Docker container the host is actually just database.

Obviously putting 127.0.0.1 does not allow the Docker container to connect to the database and putting database does not like the host machine connect to that’ll database.

I need to put 127.0.0.1 but also for Docker to recognise this and point it to the database container.

  • 1
    What I normally do is to add an entry to [the hosts file](https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/) so that you can use `database` on both the host and the container. – vstm Aug 12 '18 at 09:11
  • Can you literally just put ‘database 127.0.0.1’ inside of the hosts file? –  Aug 12 '18 at 09:17
  • Sorry ‘127.0.0.1 database’ –  Aug 12 '18 at 09:18
  • 1
    No you can't. So basically 127.0.0.1 means the ip the container. Your database is not at ip 127.0.0.1 (from what I can grasp), it lives behind the host name `database`. So you'll basically need to make sure that the database container and the laravel container are on the same network. – byrnedo Aug 12 '18 at 11:24
  • Possible duplicate of [What does localhost means inside a Docker container?](https://stackoverflow.com/questions/50278632/what-does-localhost-means-inside-a-docker-container) – David Maze Aug 12 '18 at 12:05
  • @tallent123: Yes, you can do that as long as the port is exposed (which I assumed it is since it worked on the host if you used 127.0.0.1). There probably already is an entry in your hosts file for "localhost" but you can just append your other hosts separated by spaces so: `127.0.0.1 localhost database`. – vstm Aug 12 '18 at 13:52
  • if the container name of the database is 'database', then in the .env file the DB_HOST can be 'database'. In my example the container name is 'mysql' and in my .env file my DB_HOST=mysql. Works like a charm – UnderDog Jan 14 '19 at 18:46

0 Answers0