Start Learning Docker Updated on December 22nd, 2017 in docker Docker Tip 35: Connect to a Database Running on Your Docker Host Once in a while you may want to connect a container to a database or service running on your Docker host.
Use Terminal For Localhost Database Using Docker How To Do ItHeres how to do it.
Looking for a more updated version of this post Check out Docker Tip 65. I get it, you might not trust Docker well enough to run your database in a container (with a Docker volume to deal with persistence). Fortunately you can easily have a container connect to any service thats installed on your Docker host. This means you could install your database service directly on your Docker host and then connect to it from a running Docker container. You can figure out your local network IP address by looking for the IP address that belongs to the same subnet as your router (assuming youre using one). Its very likely going to be a 192.x.x.x or 10.x.x.x address. Find Your Local Network IP Address on MacOS Linux: ifconfig. Well just run an Alpine image, drop into a shell, install the ping utility and ping the Docker host. If you were running a database, you would use 192.168.1.3 (in my case) as part of your connection string, and then configure your database to bind on 0.0.0.0. ![]() Details on how to do this will depend on what database service youre using. But Nick, What If My Local IP Address Changes In development this could happen a lot, but in production not so much. In either case the above way will get the job done but there is a cleaner way to do it. ![]() Create a custom bridge Docker network: docker network create -d bridge --subnet 192.168.0.024 --gateway 192.168.0.1 mynet. In the end, after running this command youll be able to access your Docker host by the IP address of 192.168.0.1 regardless of what your real local IP address is. Confirm it works again, but well use the new network: Start the Alpine container, but this time well use our custom network. Use Terminal For Localhost Database Using Docker Free Intro ToNice Plot twist: If you decide to run your own database, I would personally use Docker Free Intro to Docker Email Course Email Address Learn Docker Over 5 days youll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days youll have hands on experience using Docker to serve a website. Tweet Share Share Comments Please enable JavaScript to view the comments powered by Disqus. Nick Janetakis Follow nickjanetakis.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2020
Categories |