Hosting Docker Internal in Linux
Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container to a host machine.
Linux equivalent to the host.docker.internal command
Docker versions before 18.03 only support the command on Mac and Windows systems host.docker.internal
. However, in December 2020, Docker released version 20.10.0, and subsequent versions support using the host.docker.internal command to connect to the host on Linux machines.
According to the Docker documentation, if the host's IP address keeps changing, we can host.docker.internal
connect to the host using , which resolves to the host's internal IP address. However, this should only be used in a development environment.
In Windows and Mac, host.docker.internal
it is much easier to connect to the host using . Let's say we want to connect to the MySQL service running on the default port 3606 on the host.
We can use the following command to let the Docker container connect to this service.
Code:
host.docker.internal:3306
On Linux systems, on the other hand, we need to add an extra flag to successfully connect to the host. This is done by docker run
adding the --add-host flag next to the command. This only works with Docker versions after 20.10.0.
Code:
$ docker run -d --add-host host.docker.internal:host-gateway new_container
This command adds a new entry to the etc/hosts directory, host.docker.internal
mapping to the host gateway. Alternatively, we could have used 172.17.0.1, also known as localhost, which is the gateway address for the default bridge network in Docker.
Docker networking is a broad topic and the commands or methods above can be used in different ways depending on what you want to accomplish.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Setting the working directory in Docker
Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM
-
If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR
How to get IP address in CentOS
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for
Updating YUM in Linux
Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM
-
This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux
Installing Deb Files in Linux
Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM
-
In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy
lsof Command in Linux
Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM
-
In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U
How to solve the problem of not being able to execute binary files in Linux
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
In this article, we will learn how to execute binary files in Linux. We will also learn how to troubleshoot the error if Linux fails to execute the binary file. Usually, this error occurs when we run shell scripts in Linux. This article wil
Error in Linux Mesg: Ttyname Failed: Inappropriate Ioctl for Device Error
Publish Date:2025/03/23 Views:178 Category:OPERATING SYSTEM
-
In this article, we will learn how to fix the error mesg: ttyname failed: Inappropriate ioctl for device in Linux . We will discuss some of the causes of this error and show you how to fix it. Let's start with what causes this error in Linu
ps aux command in Linux
Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM
-
If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou
NTP in Linux
Publish Date:2025/03/23 Views:58 Category:OPERATING SYSTEM
-
NTP is a core protocol used by most IT infrastructures. Its purpose is to synchronize date and time information. However, it is very important to configure NTP for your servers, clients, and other network devices. NTP uses a UDP port number