可以通过下面两种方法把hosts配置到docker容器上

一、启动容器的时候加上“--add-host”把hosts配置上

# docker run --add-host=www.baidu.com:127.0.0.1 xxx -it /bin/bash

上面的容器启动之后,会把 “www.baidu.com  127.0.0.1” 这个配置写到容器的 /etc/hosts中

进入到容器中验证下

# docker ps -a
# docker exec xxxx -it /bin/bash    #xxx是上面执行后的容器id,CONTAINER ID
# cat /etc/hosts

二、通过 docker-compose.yaml 文件启动

通过 extra_hosts 将hosts配置到容器中

k8s记录-Docker配置主机
    





            
docker容器配置hosts
php72:
  container_name: "php72"
  hostname: "php72"
  image: "xxxx"
  extra_hosts:
    - "www.baidu.com:127.0.0.1"
- "www.google.com:127.0.0.1" volumes: - xxx:xxx
k8s记录-Docker配置主机
    





            
docker容器配置hosts
 

相关文章:

  • 2021-09-19
  • 2021-08-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-10-26
  • 2021-01-03
猜你喜欢
  • 2021-07-26
  • 2021-07-16
  • 2021-05-31
  • 2022-12-23
  • 2021-05-17
  • 2021-04-05
  • 2021-11-29
相关资源
相似解决方案