如何在容器中添加hosts记录

Last Updated on 2024-02-21 by likun.gong

有时候需要在容器中配置指定 hosts ,这里记录一下各个场景的配置方式

Docker run

docker run --add-host=xxx.example.com:10.0.0.8 --name debian -it debian

Docker compose

version: '3'
services:
  web:
    image: nginx
    extra_hosts:
      - "docker:10.0.0.10"
      - "another-host:10.0.0.11"

Kubernetes

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
  containers:
  - name: cat-hosts
    image: busybox:1.28
    command:
    - cat
    args:
    - "/etc/hosts"

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注