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

What is the difference between “expose” and “publish” in Docker?

Câu trả lời

In Docker, the terms "expose" and "publish" refer to different aspects of port management for containerized applications. Here is a detailed explanation of the differences between the two:

Expose

  1. Definition: The EXPOSE instruction in a Dockerfile or the --expose flag in the docker run command is used to indicate that a container listens on a specified port or ports. This is primarily a documentation mechanism that informs users and other containers about which ports the application inside the container is set to use.

  2. Functionality: Exposing a port does not make it accessible from outside the Docker host. It only makes the port available for inter-container communication within the same Docker network. This is useful for services that need to communicate with each other within a Docker environment.

  3. Usage:

    • In a Dockerfile:
      EXPOSE 8080
    • Using the docker run command:
      docker run --expose 8080 my_container
  4. Purpose: The primary purpose of EXPOSE is to document which ports the containerized application will use, making it easier to manage and understand the container's configuration.

Publish

  1. Definition: The --publish (or -p) flag in the docker run command is used to map a container's port to a port on the Docker host. This makes the container's service accessible from outside the Docker host, allowing external clients to connect to the containerized application.

  2. Functionality: Publishing a port creates a firewall rule on the Docker host that maps a container port to a host port. This allows external traffic to reach the container through the specified host port.

  3. Usage:

    • Using the docker run command:
      docker run -p 8080:80 my_container
      This command maps port 80 in the container to port 8080 on the Docker host.
  4. Purpose: The primary purpose of --publish is to enable external access to the containerized application by binding a container port to a host port.

Key Differences

  • Scope: EXPOSE is used for inter-container communication within the same Docker network, while --publish is used to make a container's port accessible from outside the Docker host.
  • **Implementat...
middle

middle

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

middle

Which problems does a Container Orchestration solve?

junior

What is Docker image?

entry

What is Docker?

Bình luận

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

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