在 Docker-Compose 中向服务添加主机名
Docker 中的每个服务容器都加入默认网络。 该网络上的其他容器可以访问它,并且它们可以使用与容器名称相同的主机名来发现它。
本文将讨论在 docker-compose 中将主机名添加到我们的容器服务。
在 Docker-Compose 中向服务添加主机名
作为 docker-compose 3.0 版,我们可以使用主机名键并将其添加到我们的 YAML 文件中。 确保我们已经明确定义了 docker-compose 在运行我们的服务时使用的版本。
要确定版本,请使用值为 3 或 3.0 的版本密钥。
version: "3.0"
services:
sampleservice:
hostname: service-hostname
如果没有版本密钥,docker-compose 将在运行服务时使用版本 1 的默认值。 对于版本 1,我们不会正确设置主机名。
但是,存在一个已知问题,如果我们执行 docker run
命令,主机名将对其他容器不可见。 我们可以定义一个别名,并为容器分配一个名称作为解决方法。
要定义别名,请先通过运行以下命令启用已定义的别名。
$ docker-compose run --use-aliases
运行后,我们可以使用别名密钥并将其包含在我们的 YAML 文件中。
version: "3.0"
services:
sampleservice:
networks:
samplenetwork:
aliases:
- alias1
- alias2
之后,使用以下命令手动将别名分配给服务。
$ docker-compose run --name alias1 sampleservice
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
发布时间:2025/03/26 浏览次数:97 分类:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
发布时间:2025/03/26 浏览次数:124 分类:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
发布时间:2025/03/26 浏览次数:202 分类:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
发布时间:2025/03/26 浏览次数:87 分类:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
发布时间:2025/03/26 浏览次数:140 分类:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application