Skip to content

Commit d667e2f

Browse files
Merge pull request #26202 from Luap99/compat-base-hosts
compat API: respect base_hosts_file containers.conf option
2 parents 4120115 + 66bf98e commit d667e2f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

pkg/api/handlers/compat/containers_create.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) {
120120
// moby always create the working directory
121121
localTrue := true
122122
sg.CreateWorkingDir = &localTrue
123-
// moby doesn't inherit /etc/hosts from host
124-
sg.BaseHostsFile = "none"
123+
// moby doesn't inherit /etc/hosts from host, but only overwrite if not set in containers.conf
124+
if rtc.Containers.BaseHostsFile == "" {
125+
sg.BaseHostsFile = "none"
126+
}
125127

126128
ic := abi.ContainerEngine{Libpod: runtime}
127129
report, err := ic.ContainerCreate(r.Context(), sg)

test/apiv2/20-containers.at

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,35 @@ for endpoint in containers/create libpod/containers/create; do
677677
done
678678

679679
stop_service
680+
# Create a temporary containers.conf with custom base_hosts_file set
681+
testdir=$(mktemp -d)
682+
cat > $testdir/containers.base_hosts_file.conf <<EOF
683+
[containers]
684+
base_hosts_file = "$testdir/hosts"
685+
EOF
686+
687+
cat > $testdir/hosts <<EOF
688+
10.0.0.2 testname
689+
192.168.1.1 abc
690+
EOF
691+
692+
CONTAINERS_CONF_OVERRIDE=$testdir/containers.base_hosts_file.conf start_service
693+
694+
t POST containers/create \
695+
Image=$IMAGE \
696+
Cmd='["cat","/etc/hosts"]' \
697+
Tty=true \
698+
201 \
699+
.Id~[0-9a-f]\\{64\\}
700+
cid=$(jq -r '.Id' <<<"$output")
701+
702+
t POST containers/${cid}/start 204
703+
704+
t GET "containers/${cid}/logs?follow=true&stdout=true&stderr=true" 200
705+
like "$(<$WORKDIR/curl.result.out)" ".*10.0.0.2[[:space:]]testname.*192.168.1.1[[:space:]]abc.*" "contains containers.conf base_hosts_file hosts"
706+
707+
stop_service
708+
rm -rf "$testdir"
680709
start_service
681710

682711
# Our states are different from Docker's.

0 commit comments

Comments
 (0)