Debug School

rakesh kumar
rakesh kumar

Posted on • Edited on

Different Ways to Link Docker Containers

Legacy Linking with the --link Flag
Modern Linking with Docker Networks and Compose
Manual Network Creation (Advanced)
When running multi-container applications, containers often need to communicate with each other—for example, a web app container connecting to a database container. There are two primary ways to link containers in Docker: the legacy --link flag and the modern Docker networking approach (primarily via Docker Compose).

Legacy Linking with the --link Flag

Legacy Linking with the --link Flag
How it works:

You start one container (e.g., MySQL) and then run another container (e.g., PHP app) with the --link flag referencing the first.

Docker injects environment variables and updates the /etc/hosts file in the linked container, allowing it to discover the other container by an alias.

Example:

docker run --name db -e MYSQL_ROOT_PASSWORD=pass -d mysql:8
docker run --name app --link db:mysql -p 8080:80 -d my-php-app
Enter fullscreen mode Exit fullscreen mode

Here, the app container can access the database using the hostname mysql.

Limitations:

The --link flag is deprecated and not recommended for new projects.

It does not work across user-defined networks or with Docker Compose v2+.

Environment variables are injected automatically, which can be unpredictable.

==========================EXAMPLE==============================

Linking with the --link Flag (Legacy, Not Recommended for Production)
Step 1: Run MySQL Container

docker run --name mymysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=motoshare -d mysql:8
Enter fullscreen mode Exit fullscreen mode

Step 2: Run PHP+Apache+Motoshare Container and Link
Suppose you built your image as motoshare-app:latest:

docker run --name motoshare-app --link mymysql:mysql -p 8080:80 -d motoshare-app:latest
Enter fullscreen mode Exit fullscreen mode

--link mymysql:mysql links the MySQL container to your app container with the alias mysql.

Your PHP app can access MySQL using the hostname mysql and the environment variables injected by Docker (e.g., MYSQL_PORT_3306_TCP_ADDR).

What Happens?
Docker injects MySQL connection info into the PHP+Apache container as environment variables and /etc/hosts entries

================ANOTHER EXAMPLE==================
How to Run Your motoshare Image with MySQL Linking

You want to run your custom Docker image named motoshare and link it to a MySQL container using the legacy --link flag. Here’s how you should do it:

  1. Start the MySQL Container First, ensure your MySQL container is running. Example:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=password -d mysql:8
--name mymysql: Names the container mymysql.
Enter fullscreen mode Exit fullscreen mode

-e MYSQL_ROOT_PASSWORD=password: Sets the root password.

-d mysql:8: Runs MySQL 8 in detached mode.

  1. Run Your PHP+Apache+Motoshare Container and Link to MySQL Since your image is named motoshare, use:
docker run --name motoshare-app --link mymysql:mysql -p 8080:80 -d motoshare
Enter fullscreen mode Exit fullscreen mode

Explanation:

--name motoshare-app: Names your app container.

--link mymysql:mysql: Links to the MySQL container (mymysql) and gives it the alias mysql inside the app container.

-p 8080:80: Maps port 80 in the container to port 8080 on your host (so you can access your app at http://localhost:8080).

-d motoshare: Runs your image named motoshare in detached mode.
Enter fullscreen mode Exit fullscreen mode
  1. How Linking Works Your app container will have environment variables and /etc/hosts entries for the MySQL container, allowing it to connect using the hostname mysql and the credentials you set.

This method is legacy; for new projects, Docker Compose is recommended for linking and networking.

Image description

Image description

Access the Website
Open your browser and go to:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

This will connect to your Apache server running inside the motoshare-app container

Modern Linking with Docker Networks and Compose

docker-compose-install-and-configuration-tutorial
collection-example-of-docker-compose-program
docker-compose-wait-for-container-one-before-starting-container-second
docker-compose.yml Example

version: '3.8'

services:
  db:
    image: mysql:8
    container_name: mymysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: motoshare
    volumes:
      - db_data:/var/lib/mysql

  app:
    image: motoshare-app:latest
    container_name: motoshare-app
    depends_on:
      - db
    environment:
      MYSQL_HOST: db
      MYSQL_DATABASE: motoshare
      MYSQL_USER: root
      MYSQL_PASSWORD: password
    ports:
      - "8080:80"
    volumes:
      - ./motoshare-web:/var/www/html
Enter fullscreen mode Exit fullscreen mode

volumes:
db_data:
How to Use
Build your PHP+Apache+Motoshare image:

docker build -t motoshare-app:latest .
Enter fullscreen mode Exit fullscreen mode

Start both containers with Compose:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

What Happens?
Docker Compose creates a shared network for both containers.

Your app can access MySQL using the hostname db (the service name).

No need for --link or manual environment variable parsing.

Manual Network Creation (Advanced)

docker network create mynet
docker run --name db --network mynet -e MYSQL_ROOT_PASSWORD=pass -d mysql:8
docker run --name app --network mynet -p 8080:80 -d my-php-app
Enter fullscreen mode Exit fullscreen mode

Linking PHP+Apache+Motoshare and MySQL Containers with a Custom Network

Step 1: Create a Custom Docker Network

docker network create motonet
Enter fullscreen mode Exit fullscreen mode

This creates a user-defined bridge network named motonet. Containers on this network can communicate by name.

Step 2: Run the MySQL Container

docker run --name motodb --network motonet \
  -e MYSQL_ROOT_PASSWORD=motopass \
  -e MYSQL_DATABASE=motoshare \
  -e MYSQL_USER=moto_user \
  -e MYSQL_PASSWORD=moto_pass \
  -d mysql:8
--network motonet: Attaches the container to the motonet network.

--name motodb: Names the container motodb (you’ll use this as the DB host).

Enter fullscreen mode Exit fullscreen mode

Environment variables set up the database, user, and passwords.

Step 3: Run the PHP+Apache+Motoshare Container
Assume your image is named motoshare-app:latest and your site code is in the image.

docker run --name motoshare-app --network motonet \
  -p 8080:80 \
  -e MYSQL_HOST=motodb \
  -e MYSQL_DATABASE=motoshare \
  -e MYSQL_USER=moto_user \
  -e MYSQL_PASSWORD=moto_pass \
  -d motoshare-app:latest
--network motonet: Places this container on the same network as MySQL.

Enter fullscreen mode Exit fullscreen mode

-p 8080:80: Maps port 80 in the container to port 8080 on your host (so you can access it via http://localhost:8080).

The -e flags pass database connection details to your PHP app (adjust as needed for your code).

Step 4: How Linking Works
Both containers are on the motonet network.

Your PHP app connects to MySQL using motodb as the database host (not localhost or an IP).

No legacy --link flag is needed; Docker networking handles service discovery.

Step 1: Create a Custom Docker Network

docker network create motonet
Enter fullscreen mode Exit fullscreen mode

This creates a user-defined bridge network named motonet. Containers on this network can communicate by name.

Step 2: Run the MySQL Container

docker run --name motodb --network motonet \
  -e MYSQL_ROOT_PASSWORD=motopass \
  -e MYSQL_DATABASE=motoshare \
  -e MYSQL_USER=moto_user \
  -e MYSQL_PASSWORD=moto_pass \
  -d mysql:8
--network motonet: Attaches the container to the motonet network.

--name motodb: Names the container motodb (you’ll use this as the DB host).
Enter fullscreen mode Exit fullscreen mode

Environment variables set up the database, user, and passwords.
Step 3: Run the PHP+Apache+Motoshare Container
Assume your image is named motoshare-app:latest and your site code is in the image.

docker run --name motoshare-app --network motonet \
  -p 8080:80 \
  -e MYSQL_HOST=motodb \
  -e MYSQL_DATABASE=motoshare \
  -e MYSQL_USER=moto_user \
  -e MYSQL_PASSWORD=moto_pass \
  -d motoshare-app:latest
Enter fullscreen mode Exit fullscreen mode

--network motonet: Places this container on the same network as MySQL.

-p 8080:80: Maps port 80 in the container to port 8080 on your host (so you can access it via http://localhost:8080).

The -e flags pass database connection details to your PHP app (adjust as needed for your code).

Step 4: How Linking Works
Both containers are on the motonet network.

Your PHP app connects to MySQL using motodb as the database host (not localhost or an IP).

No legacy --link flag is needed; Docker networking handles service discovery.

Step 4: How Linking Works
Both containers are on the motonet network.

Your PHP app connects to MySQL using motodb as the database host (not localhost or an IP).

No legacy --link flag is needed; Docker networking handles service discovery.

Step 5: Access Your Website
Open your browser and go to:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

You should see your Motoshare website running, connected to the MySQL database.

Top comments (0)