How I connnected my Dockerized App to MariaDB database installed in Synology Server

Project preview

I was working on the project where my client wanted to me put all the crawler and application in his own Synology NAS. It was new thing for me. He only new that he was able to run the containers on his server. Rest was my task to figure it out. While working on this project Among alot of problems in each steps I came to this as well which I will share here?

Why Do We Need to Connect Dockerized Apps to Local Databases?

I found synology has it own package of MariaDB server. Running an application in Docker provides a consistent and isolated environment. However, for development purposes, you might want your Dockerized app to connect to a local database to avoid the overhead of managing a database inside Docker or to leverage existing local data.

Connecting to a Local Database on Windows and macOS

Docker provides a built-in DNS name that resolves to the host machine, making it easy to connect to services running on the host. On Windows and macOS, you can use host.docker.internal to refer to your local machine from within the Docker container.

Here’s how you can do it:

  1. Ensure Docker is Running: Start Docker Desktop on your machine.

  2. Modify Your Database Connection String: In your application configuration, change the database host to host.docker.internal.

    For example, if you’re using a MySQL database, your connection string might look like this:

    mysql://user:[email protected]:3306/database_name
    
  3. Run Your Docker Container: Use your normal Docker run command to start the container. No special network settings are required.

    docker run -d --name my_app_container my_app_image
    

With this setup, your Dockerized application should be able to connect to the local database running on your host machine.

Connecting to a Local Database on Linux

For Linux users, Docker doesn’t provide a direct equivalent to host.docker.internal. Instead, you can use the --net=host option when running your Docker container. This option makes the container use the same network as the host machine, effectively allowing it to access services running on the host.

Here’s how to set it up:

  1. Run Docker with --net=host: When starting your Docker container, include the --net=host option in your docker run command.

    docker run -d --net=host --name my_app_container my_app_image
    
  2. Modify Your Database Connection String: In your application configuration, set the database host to localhost or 127.0.0.1.

    For example:

    mysql://user:password@localhost:3306/database_name
    

    Since the container shares the network with your host, it can communicate with services running on localhost.

Since synology uses NAS server After some research and try I was able do adding this simple —net=host .

Conclusion

Connecting a Dockerized application to a local database can be straightforward with the right setup. Whether you’re on Windows, macOS, or Linux, following the steps outlined above should help you seamlessly integrate your Dockerized app with your local database, enabling a more efficient development workflow.

Subscribe to My Newsletter

I frequently write about techology.