Dino Geek, try to help you

How to connect a Docker container to a local database?


Connecting a Docker container to a local database involves three steps:

  1. Step 1: Host your Database

Ensure that your database is hosted on your local system. This might be a MySQL or PostgreSQL database. The main priority is that the database is visible and accessible in your local network.

  1. Step 2: Set up Docker Container

Your Docker container should be set up to connect to a database. This is usually done in the Dockerfile or docker-compose file.

If you are using the Dockerfile, you add commands to connect to your database, typically using environment variables for the database URL, name, username, and password. Example for MySQL:

```
ENV MYSQL_DATABASE=mydatabase \ MYSQL_USER=myuser \ MYSQL_PASSWORD=mypassword \ MYSQL_ROOT_PASSWORD=myrootpassword
```

If you are using docker-compose, it’s similar but the setup is in yaml format:

```
version: ‘3’
services: db: image: mysql:5.7 volumes: – db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: myrootpassword MYSQL_DATABASE: mydatabase MYSQL_USER: myuser MYSQL_PASSWORD: mypassword
volumes: db_data:
```

  1. Step 3: Connect Docker Container to Database

When you run your Docker container, it needs to know where to find your database. In development mode, this is often your localhost, i.e., `127.0.0.1`. However, Docker containers have their own network, so your localhost (`127.0.0.1`) inside a Docker container is the container itself, not your machine.

Therefore, you cannot use `localhost` or `127.0.0.1` as your host to connect from your application in Docker to your local machine.

The solution is to use `host.docker.internal` instead. This hostname is the gateway between your Docker container and local system.

Note: `host.docker.internal` works on Docker for Windows and Docker for Mac, but as of Docker 20.10, it has also been added to Docker for Linux. For older versions of Docker for Linux, you must use the IP address of your machine.

So in your application database configuration, you can use `host.docker.internal` as your database host. Following is an example for a connection string in a Node.js application for MySQL database.

```
const connection = mysql.createConnection({ host: ‘host.docker.internal’, user: ‘myuser’, password: ‘mypassword’, database: ‘mydatabase‘
});
```

And this is it. With this setup, you should be able to connect your Docker container to a local database.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use