0

I composed a Mongodb service with Docker on my local machine, and the hostname is configured in docker-compose.yml with syntax container_name: mongodb, then with the spirit of Microservice I started a RESTful API service on a lightweight embedded server, say Jetty, also locally.

The problem is that the local RESTful API service cannot communicate with the Mongodb service by using the defined Mongodb container name - mongodb - on a local Docker container due to different network.

Questions:

  1. Is this a good practice, say by starting the RESTful API service just on an embedded server, esp. in production, without another Docker container differing from the Mongodb container?
  2. If this is a good practice, how to configure to make the RESTful API on the local embedded server have access to the Mongodb on a local Docker container?
Rui
  • 3,454
  • 6
  • 37
  • 70

1 Answers1

0

About running databases in Docker - this is totally nice. You probably would like to mount volumes from your host to configure database to store data there, as soon as storing data in container is not always convenient. Configuration is normally done by environment variable, passed to docker and config files, located in shared volumes.

Network itself should not be an issue, there are different network modes, supported by Docker, which allow you to communicate from host. The simplest, but not recommended for production purposes is just using --network=host. This prevents Docker from creating new networks and container shares your localhost. That means you are able to access all services from container just using localhost as a hostname.

Why not recommended: because this eliminates the level of protection/isolation, normally granted by Docker as soon as all you localhost networking becomes available in container.

grapes
  • 8,185
  • 1
  • 19
  • 31
  • Thanks a lot for your answer. But I still would like to know **if using the embedded server without a container is also a good practice**, esp. in production? Moreover, as you told `--network=host` is not recommended in production, so if embedded server is also used in production, how could this embedded server communicate with the Mongodb Docker container? – Rui Dec 24 '18 at 08:56
  • Running database in **non-conteinerized** format is absolutely ok. It's up to you to choose mode depending on what you are going to achieve. Want k8s? Then sorry, use containers. Just on-site installation? Can go without containers and etc. About networking - `host` mode is easy to use and considered as a quick start. You can easily use `bridged` mode and access containers from your host PC by exposing proper ports – grapes Dec 24 '18 at 10:02
  • For on-site installation, my worry is that embedded server is usually light-weight, so if the web application is big and heavy in production, is the deployment with pure embedded server without any real server or Dockerfile rcontainer still OK? – Rui Dec 24 '18 at 11:01
  • I tried to modify the code of my web application on local server to set the mongodb hostname to IP address, then the application on embedded server can access the mongodb on the container, but then I tried also the `--network=host` option in *docker-compose* by setting `network_mode: "host"`, the web application still can not access the mongodb on the Docker container – Rui Dec 24 '18 at 17:28