Câu hỏi phỏng vấn Docker
Câu hỏi

How to link containers?

Câu trả lời

To link Docker containers, you can use the legacy --link flag or modern Docker networking features. Here’s a detailed explanation of both methods:

The --link flag is a legacy feature that allows you to link multiple containers together, enabling them to share information securely. Here’s how you can link containers using this method:

  1. Create the Source Container:
    First, create a container that will act as the source. For example, a PostgreSQL database container:

    bash Copy
    $ docker run -d --name db training/postgres
  2. Remove the Existing Recipient Container:
    If you have an existing container that you want to link, remove it first:

    bash Copy
    $ docker container rm -f web
  3. Create and Link the Recipient Container:
    Create a new container and link it to the source container using the --link flag:

    bash Copy
    $ docker run -d -P --name web --link db:db training/webapp python app.py

    In this command, --link db:db links the web container to the db container, allowing web to access information about db.

  4. Inspect the Link:
    You can inspect the link to verify the connection:

    bash Copy
    $ docker inspect -f "{{ .HostConfig.Links }}" web

    This should show the link between the web and db containers.

  5. Accessing Linked Container Information:
    The recipient container can access the source container’s information via environment variables and the /etc/hosts file. For example, the web container can access the db container’s IP address and other details without exposing ports externally[1][4].

Using Modern Docker Networking

Modern Docker networking provides more flexible and powerful ways to connect containers. Here’s how you can achieve container communi...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is virtualisation?

senior

Is it good practice to run stateful applications on Docker? What are the scenarios where Docker best
fits in?

expert

Why Docker compose does not wait for a container to be ready before moving on to start next service
in dependency order?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào