podman 容器内访问host主机的端口

podman支持跟docker一样,设置一个hosts配置,让你可以访问到主机的端口

 

秘密就在于启动容器的时候,加上这个 

 --network slirp4netns:allow_host_loopback=true

然后就可以访问了

 

 curl localhost
hello
[email protected]:~> podman run --network slirp4netns:allow_host_loopback=true -it alpine /bin/sh  -c " grep containers /etc/hosts"
10.0.2.2 host.containers.internal
[email protected]:~> podman run --network slirp4netns:allow_host_loopback=true -it alpine /bin/sh  -c " curl host.containers.internal""
> 
> 
> "
/bin/sh: curl: not found
[email protected]:~> podman run --network slirp4netns:allow_host_loopback=true -it alpine /bin/sh  -c "apk add curl &&  curl host.containers.internal"
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ca-certificates (20211220-r0)
(2/5) Installing brotli-libs (1.0.9-r5)
(3/5) Installing nghttp2-libs (1.46.0-r0)
(4/5) Installing libcurl (7.80.0-r0)
(5/5) Installing curl (7.80.0-r0)
Executing busybox-1.34.1-r3.trigger
Executing ca-certificates-20211220-r0.trigger
OK: 8 MiB in 19 packages
hello

在podman-compose里面,我们可以这样写

version: "3.4"
services:
  casdoor:
    restart: always
    image: casbin/casdoor
    network_mode: "slirp4netns:allow_host_loopback=true"
    ports:
      - "127.0.0.1:8087:8000"
    environment:
      RUNNING_IN_DOCKER: "true"
    volumes:
      - ./conf:/conf/
    networks:
      - internal
    restart: unless-stopped
networks:
  internal:
    attachable: true

 

您也可以修改全局配置项

# vim network_cmd /usr/share/containers/containers.conf
修改这一行,加上allow_host_loopback=true就可以了
network_cmd_options = ["allow_host_loopback=true"]
 
 

分类: 默认 标签: 发布于: 2022-01-29 15:19:01, 更新于: 2022-03-16 16:05:54