https://res.cloudinary.com/brightsoftwares/image/upload/t_BSBlogImage/v1651943394/david-knox-0blbblsyf3o-unsplash_r6a1ng.jpg

Can Docker connect to database?

nordvpn best offer nordvpn best offer

There are many situations where you need to connect a external database because database in a container doesn’t support all the features that is required by the application or you need persistent data across your cluster environment.

In this post we are going to connect external SQL database to docker web container and external database can be hosted on dedicated SQL server or any cloud RDS.

In this post my SQL database is hosted on dedicated SQL server on a Virtual Machine. The web application is hosted on a docker container. It then connects to the SQL database on the VM.

Let’s begin!

The setup

My application’s database is hosted on this SQL machine and this is the application database.

This is the second machine on which I am running the docker container for the web application.

This is the second machine

If you need any help about application code or how to set up the application on a container you can check this post on minikube.

I configured the database information in web config file of the application.

web config file

I used the IP address of my MS SQL database and port number. In the screenshot above you have the database information.

You can also use fully qualified domain name of your SQL database if this domain name is getting resolved from the docker container itself.

Note: If you are hosting your database on any cloud RDS like edge or or AWS you can use similar connection string to connect your RDS.

Setup the docker-compose stack

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Lean more here.

This is my docker compose file. I am going to use this file to launch my web container.

I am ready to launch my web container

My docker container is ready to be used. This is the container and you can access the application on port and on the browser.

Find the docker host’s IP address

To be able to connect to the docker container that runs our application, we need to have it’s host’s IP address.

To ge the IP address of the host, run this command from the host computer.

ipconfig

On a windows machine, here is the result.

Access the application through the browser

Let’s access the application on the browser by using the hosts IP address.

Success! The application is successfully able to connect MS SQL database on a dedicated VM. Let’s do some database transaction on the application.

For example I want to book a single room on 2nd September single room has been confirmed.

Test on container crash

Now let’s suppose that the web container has crashed. To mimic that, I will kill the container manually.

No container is running.

If I check my application, it is down.

This proves that the link to the SQL is through that container.

Let me start again my container to bring the service back.

A new container has been launched and my application should be available on the browser. Let me check if my reservation is still there (stored on the SQL database).

Our room reservation is available for the 2nd September because we are stored our data into the persistent volume (the SQL database).

Conclusion

This is the complete demonstration of how you can connect your external database to your docker web container to store your data into persistent volume in the cluster environment.

You might also like these blog posts

Full Bright

Full Bright

A professional and sympathic business man.

Contact

Contact Us

To order one of our services, navigate to the order service page

Address

10 rue de Penthièvre,
75008 Paris

Email Us

hello at bright-softwares dot com

Open Hours

Monday - Friday
9:00AM - 05:00PM