Skip to content

Connect Frontend & Backend + expose db#6

Merged
roschaefer merged 4 commits intomasterfrom
5-connect-backend-frontend
Mar 17, 2019
Merged

Connect Frontend & Backend + expose db#6
roschaefer merged 4 commits intomasterfrom
5-connect-backend-frontend

Conversation

@matboehm
Copy link
Copy Markdown
Contributor

@matboehm matboehm commented Mar 6, 2019

Close #5


Change url for API server to connect Frontend & Backend + expose database in host at port 3307

@matboehm matboehm self-assigned this Mar 6, 2019
@matboehm matboehm requested a review from roschaefer March 6, 2019 08:51
Copy link
Copy Markdown
Contributor

@roschaefer roschaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Comment thread README.md Outdated
```
Your API Key: secret
API base url: http://localhost:1337
API base url: http://host.docker.internal:1337
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@matboehm
Copy link
Copy Markdown
Contributor Author

matboehm commented Mar 9, 2019

@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.

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 docker.host.internal to resolve the host IP and connect the services through the host machine, seems to be the easiest solution for me. Only that the Linux support for this function is missing currently (see my comment below)...

@roschaefer
Copy link
Copy Markdown
Contributor

@matthib could you let frontend and backend use the same database?

@matboehm
Copy link
Copy Markdown
Contributor Author

matboehm commented Mar 10, 2019

@matthib could you let frontend and backend use the same database?

@roschaefer
This could possibly work in development. But we have to consider that this is contrary to the current system architecture in production. Backend and Frontend run on different servers on production, which means we would have a different architecture in development and production. This would probably also make deploying via Docker more complicated.
But if it is the only solution, it is of course worth a shot!

@roschaefer
Copy link
Copy Markdown
Contributor

@matthib can you explain me why forwarding the port to 3307 on the host system for one of the MySQL containers does not work?

@matboehm
Copy link
Copy Markdown
Contributor Author

@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...
See for example: https://stackoverflow.com/questions/49059109/docker-compose-run-two-instance-of-mysql

@roschaefer
Copy link
Copy Markdown
Contributor

@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. mysql:3306 and anothermysql:3306)

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. localhost:3306 and localhost:3307).

@matboehm
Copy link
Copy Markdown
Contributor Author

matboehm commented Mar 13, 2019

@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.

@matboehm
Copy link
Copy Markdown
Contributor Author

@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.
Please also see the pull request in the Backend: Human-Connection/Human-Connection-Clock-API#31

Copy link
Copy Markdown
Contributor

@roschaefer roschaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! 👍

@roschaefer roschaefer merged commit 922a4df into master Mar 17, 2019
@roschaefer roschaefer deleted the 5-connect-backend-frontend branch March 17, 2019 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants