DEV Community

Cover image for Connect your local resources from inside your Docker Container
Abhigyan Gautam
Abhigyan Gautam

Posted on

Connect your local resources from inside your Docker Container

Docker containers are like magic, allowing you to run full fledged applications without going through all the steps and dependencies. The best part is that the experience is same throughout. But sometimes you want to connect to the local resources from inside the container.

Use case

If you are new to Docker, check out Creating your first Docker Container - A beginner's Guide. Suppose you have a custom script that creates a database, creates the tables and the relationships and adds data. Since the script may require some dependencies, the best way to ensure that there are no issues for others is to dockerize the entire setup.

Code Folder Structure

The following is a simple code structure for the above use case.



- dbstuff.py
- requirements.txt
- Dockerfile
- requirements.txt
- .env


Enter fullscreen mode Exit fullscreen mode
  • dbstuff.py contains the custom python script that handles all the DML and DDL parts.
  • requirements.txt has the dependencies used by the project.
  • Dockerfile contains the layers required to create the image.
  • .env contains the environment variables which include database connection details ```

DATABASE_USER=devto
DATABASE_PASSWORD=dbpass
DATABASE_HOST=localhost
DATABASE_NAME=devtotest
...

## Dockerize the image
Once you create a docker image using `docker build` and run the container using `docker run`, you will face an issue connecting tot the database. This is because the container is trying to connect to the database instance inside it and not on the local machine.


![Normal Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fj5w1jrpqqpu7gv2kww.jpg)

## The solution
We need to tell docker to connect to the host's local resources rather than the container's resources. We can do this by changing the .env as follows
Enter fullscreen mode Exit fullscreen mode

...
DATABASE_HOST=host.docker.internal
...

![Host Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bclomlpxgr8rqgqzm2ta.jpg)
This will now point to the `localhost` of the host. Problem solved!

Create another image and run the container and this should now work as intended! Congratulations πŸŽ‰πŸŽ‰! You have successfully connected to your local Database from inside your docker container

Photo by [Manuel Geissinger] (https://www.pexels.com/photo/black-server-racks-on-a-room-325229/)



Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
tbroyer profile image
Thomas Broyer

host.docker.internal is Docker Desktop specific. For those of us using Docker Engine right on their Linux machine, the right approach is the host-gateway special --add-host value, so for maximum compatibility add a --add-host=host.docker.internal:host-gateway to the docker run command (or a similar entry in extra_hosts for Docker Compose)

Collapse
 
shishsingh profile image
Shish Singh

Simple and easy...πŸ‘ŒπŸ‘ŒπŸ‘Œ