Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker container connection

Connecting Docker containers allows them to communicate with each other, share data, or interact over a network. In this tutorial, we will explore different methods to connect Docker containers using networks, links, and volume sharing.

  • Docker Networks:

Docker networks enable isolated communication between containers. You can use the default networks (bridge, host, and none) or create custom networks for specific use cases.

a. Default bridge network:

By default, Docker creates a bridge network named bridge. All containers without a specified network are connected to this network, allowing them to communicate with each other using their IP addresses or container names.

b. Custom bridge network:

To create a custom network, use the docker network create command:

docker network create my-network

To connect containers to the custom network, use the --network option with docker run:

docker run -d --name container1 --network my-network my-image
docker run -d --name container2 --network my-network my-image

Containers in the same network can communicate using their names or IP addresses.

c. Overlay network:

Overlay networks are useful for multi-host or swarm deployments. To create an overlay network, use the docker network create command with the --driver option:

docker network create --driver overlay my-overlay-network

You can then deploy services on this network using docker service create or by defining the network in your docker-compose.yml file.

  • Docker Links (deprecated):

Docker links are a legacy method of connecting containers. It is recommended to use networks for new projects, as they provide better isolation and functionality. However, for reference, here's how to use links:

docker run -d --name container1 my-image
docker run -d --name container2 --link container1 my-image

This command links container2 to container1, allowing container2 to reference container1 using its name and an alias.

  • Volume Sharing:

Shared volumes allow containers to access the same data on the host system, enabling data exchange between containers.

To create a shared volume, use the docker volume create command:

docker volume create my-shared-volume

To mount the shared volume in containers, use the -v option with docker run:

docker run -d --name container1 -v my-shared-volume:/data my-image
docker run -d --name container2 -v my-shared-volume:/data my-image

Both container1 and container2 can now read and write data to the /data directory, which is backed by the my-shared-volume on the host system.

In summary, there are multiple methods for connecting Docker containers, including networks, links (deprecated), and shared volumes. Docker networks are the recommended method for enabling communication between containers, while shared volumes can facilitate data exchange.

  1. Docker Container Port Mapping:

    • Description: Port mapping allows exposing container ports to the host, enabling external access.
    • Code: Example of port mapping in Docker run command:
      docker run -p 8080:80 my_web_app
      
  2. Connecting Docker Containers with Docker Compose:

    • Description: Docker Compose simplifies container networking, automatically creating a bridge network for services defined in the docker-compose.yml file.
    • Code: Example of defining services in Docker Compose:
      version: '3'
      services:
        web:
          image: nginx:latest
        db:
          image: postgres:latest
      
  3. Docker Container DNS Resolution:

    • Description: Docker provides built-in DNS resolution for container names, making it easy to refer to other containers by name.
    • Code: Example of using container name for DNS resolution:
      ping my_other_container
      
  4. External Connectivity for Docker Containers:

    • Description: Containers can be accessed externally by mapping container ports to host ports and configuring firewall rules.
    • Code: Example of external access through port mapping:
      docker run -p 8080:80 my_web_app