Allow discovering non-running Docker containers
This commit is contained in:
parent
5c489c05fc
commit
10be359327
10 changed files with 600 additions and 11 deletions
|
|
@ -32,7 +32,7 @@ func (s *DockerSuite) TearDownSuite() {
|
|||
}
|
||||
|
||||
func (s *DockerSuite) TearDownTest() {
|
||||
s.composeStop("simple", "withtcplabels", "withlabels1", "withlabels2", "withonelabelmissing", "powpow")
|
||||
s.composeStop("simple", "withtcplabels", "withlabels1", "withlabels2", "withonelabelmissing", "powpow", "nonRunning")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestSimpleConfiguration() {
|
||||
|
|
@ -222,3 +222,59 @@ func (s *DockerSuite) TestRestartDockerContainers() {
|
|||
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 60*time.Second, try.BodyContains("powpow"))
|
||||
require.NoError(s.T(), err)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestDockerAllowNonRunning() {
|
||||
tempObjects := struct {
|
||||
DockerHost string
|
||||
DefaultRule string
|
||||
}{
|
||||
DockerHost: s.getDockerHost(),
|
||||
DefaultRule: "Host(`{{ normalize .Name }}.docker.localhost`)",
|
||||
}
|
||||
|
||||
file := s.adaptFile("fixtures/docker/simple.toml", tempObjects)
|
||||
|
||||
s.composeUp("nonRunning")
|
||||
|
||||
// Start traefik
|
||||
s.traefikCmd(withConfigFile(file))
|
||||
|
||||
// Verify the container is working when running
|
||||
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
||||
require.NoError(s.T(), err)
|
||||
req.Host = "non.running.host"
|
||||
|
||||
resp, err := try.ResponseUntilStatusCode(req, 3*time.Second, http.StatusOK)
|
||||
require.NoError(s.T(), err)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(s.T(), err)
|
||||
assert.Contains(s.T(), string(body), "Hostname:")
|
||||
|
||||
// Verify the router exists in Traefik configuration
|
||||
err = try.GetRequest("http://127.0.0.1:8080/api/http/routers", 1*time.Second, try.BodyContains("NonRunning"))
|
||||
require.NoError(s.T(), err)
|
||||
|
||||
// Stop the container
|
||||
s.composeStop("nonRunning")
|
||||
|
||||
// Wait a bit for container stop to be detected
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Verify the router still exists in configuration even though container is stopped
|
||||
// This is the key test - the router should persist due to allowNonRunning=true
|
||||
err = try.GetRequest("http://127.0.0.1:8080/api/http/routers", 10*time.Second, try.BodyContains("NonRunning"))
|
||||
require.NoError(s.T(), err)
|
||||
|
||||
// Verify the service still exists in configuration
|
||||
err = try.GetRequest("http://127.0.0.1:8080/api/http/services", 1*time.Second, try.BodyContains("nonRunning"))
|
||||
require.NoError(s.T(), err)
|
||||
|
||||
// HTTP requests should fail (502 Bad Gateway) since container is stopped but router exists
|
||||
req, err = http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
||||
require.NoError(s.T(), err)
|
||||
req.Host = "non.running.host"
|
||||
|
||||
err = try.Request(req, 3*time.Second, try.StatusCodeIs(http.StatusServiceUnavailable))
|
||||
require.NoError(s.T(), err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,3 +35,9 @@ services:
|
|||
labels:
|
||||
traefik.http.Routers.Super.Rule: Host(`my.super.host`)
|
||||
traefik.http.Services.powpow.LoadBalancer.server.Port: 2375
|
||||
|
||||
nonRunning:
|
||||
image: traefik/whoami
|
||||
labels:
|
||||
traefik.http.Routers.NonRunning.Rule: Host(`non.running.host`)
|
||||
traefik.docker.allownonrunning: "true"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue