How to connect from Docker container to the host

Docker is a SaaS (Software as a Service) product that utilizes OS virtualization to create containers. These containers are a stand-alone software package. They contain all the necessary files to run their specific application. These include code, system tools, system libraries, and custom software installed on the container. Docker containers provide a lightweight and portable experience for users who wish to deploy and test multiple independent systems concurrently.

Although Docker containers run independently, we can connect them to our local machines. This allows us to utilize services, resources, and processes on the local machines through our docker container. This may be helpful when communication between the two is necessary, such as when file-sharing or accessing host services. However, we must take precautions while doing so. This is because Docker containers can have access to privileged resources on the local host, and any misuse can cause risk to the host.

Connectivity options between a Docker container and the host
Connectivity options between a Docker container and the host

The following code shows the implementation steps of these three options which allow us to connect our Docker Container to its respective resource from the host.

Connect to a running service

To connect our Docker container to our host's service, we will first start a Docker container. Then we will update and install curl in our container. Finally, we will connect to the service by replacing <ip_address> and <port> with the appropriate values:

# Install Docker (if needed)
sudo apt update
sudo apt install docker.io
# Option 1: Connect to a Running Service
# Start a docker container
docker run -it ubuntu:latest bash
# Install Curl (if needed)
apt-get update
apt-get install curl -y
# Connect to Service
curl <ip_address>:<port>
Commands to Connect Docker Container to Host's Service

Connect to directory/file

The starting process to connect our Docker container to our host's directory is very similar. We first start a Docker container and copy the directory or file into our container. For this, we need to replace the path/to/desktop and /container/path to the path from your host directory and your Docker container respectively. After that, we can retrieve the contents of the file by replacing /container/path/file.txt with the actual path followed by the filename and file type as shown below.

# Install Docker (if needed)
sudo apt update
sudo apt install docker.io
# Option 2: Connect to Directory/File
# Start a docker container and copy directory/file
docker run -it -v /path/to/desktop:/container/path ubuntu:latest bash
# Retrieve contents of file
curl file:///container/path/file.txt
Commands to connect Docker container to host's directory/file

Connect to network

For this connection, we will first start a Docker container and connect to the host network. Then we will connect our container to the network service by replacing <host_ip_address> and <port> with the appropriate values.

# Install Docker (if needed)
sudo apt update
sudo apt install docker.io
# Option 3: Connect to Network
# Start a docker container and connect to host network
docker run -it --network host ubuntu:latest bash
# Connect to Network
curl <host_ip_address>:<port>
Commands to Connect Docker Container to Host's Network

Please note that in each option, additional steps or modifications may be required based on your specific use case and environment.

Copyright ©2024 Educative, Inc. All rights reserved