Connect Frontend & Backend + expose db#6
Conversation
…base in host at port 3307
roschaefer
left a comment
There was a problem hiding this comment.
@matthib if you docker-compose exec someservice bash then you can ping another service by it's name. If and only if these services are in the same network. E.g. you could ping coc-api from within the cli service of the frontend.
With named networks, a feature of docker-compose version >= 3.7, you can start both frontend and backend in succession and they will join the same network.
Beware, that the domain will be different from the point of view of your host system and your docker container. So you can ping http://localhost:1337 from your host, but http://coc-api:1337 from your frontend container.
Feel free to ask me if you have further questions.
| ``` | ||
| Your API Key: secret | ||
| API base url: http://localhost:1337 | ||
| API base url: http://host.docker.internal:1337 |
There was a problem hiding this comment.
🤔 what domain is that? I don't have any domains like that available on my machine when I start docker-compose up. The only thing that is available is localhost and the exposed ports by docker.
There was a problem hiding this comment.
The url host.docker.internal can be used in Docker to resolve the IP address of the host machine (https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds).
As the port 1337 from the CoC API Server is exposed to the host machine, it can be accessed through it. So in the end the host ist the link between Backend and Frontend.
This works just fine for me. The feature is also available for Mac. Unfortunately I just discovered, that the feature is not OS agnostic and is not supported on Linux environments at the moment. But there is a Github issue for this at docker/for-linux#264 and there was apparently a fix for it commited about 24h hours ago. So it could be fixed very soon.
Thanks for the response! I tried putting Frontend and Backend into the same network. The biggest problem with this is that is that both the API Server and Wordpress rely on a MySQL database which will run on the same port 3306. I did not find a way to change the internal port for the MySQL service, so that both services are not blocking each other. Also tried using a reverse proxy, to connect both services, but this was also very complicated and not easily achievable. The solution by using |
|
@matthib could you let frontend and backend use the same database? |
@roschaefer |
|
@matthib can you explain me why forwarding the port to 3307 on the host system for one of the MySQL containers does not work? |
|
@roschaefer You mean that you cannot access the Wordpress database under localhost:3307, like specified in the docker-compose.yml file? I am a bit confused, because that is working just fine for me with this pull request, just like specified in the docker-compose.yml file. If you mean, why forwarding the internal port 3306 to the host machine as 3307 for one of the MySQL services if both are on the same network does not work - well of course this works! But this is not the problem, since both still have the internal port 3306 and as they are on the same network, they are blocking each other. Haven't figured out how to change the internal port for a MySQL service yet... |
|
@matthib two different instances of mysql on separate containers in the same network should not block each other. Even if they listen on the same port number, if they are on different containers they are like different computers in a network. You can access them from within the network (ie. from another container) with a different domain name (e.g. If you expose the services to your host system you have to forward to different port numbers (on your host system). Then you have change your db connection of the client (e.g. |
|
@roschaefer Thanks for the explanation, will try this approach again then! If it does not work correctly I will commit the work in progress code, so we can take a look at it together and get it to work. |
|
@roschaefer Proceeded like you suggested. Now Frontend and Backend are in the same network (coc_network). The Backend can thus be accessed by the url http://coc-api:1337. |
Close #5
Change url for API server to connect Frontend & Backend + expose database in host at port 3307