mirror of
https://github.com/docker/compose.git
synced 2025-07-22 13:14:29 +02:00
Add network alias to container name
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
b264e1814b
commit
b3993230d1
@ -45,7 +45,7 @@ func (s *composeService) attach(ctx context.Context, project *types.Project, con
|
||||
|
||||
var names []string
|
||||
for _, c := range containers {
|
||||
names = append(names, getContainerName(c))
|
||||
names = append(names, getCanonicalContainerName(c))
|
||||
}
|
||||
fmt.Printf("Attaching to %s\n", strings.Join(names, ", "))
|
||||
|
||||
@ -61,7 +61,7 @@ func (s *composeService) attach(ctx context.Context, project *types.Project, con
|
||||
|
||||
func (s *composeService) attachContainer(ctx context.Context, container moby.Container, consumer compose.LogConsumer, project *types.Project) error {
|
||||
serviceName := container.Labels[serviceLabel]
|
||||
w := getWriter(serviceName, getContainerName(container), consumer)
|
||||
w := getWriter(serviceName, getCanonicalContainerName(container), consumer)
|
||||
|
||||
service, err := project.GetService(serviceName)
|
||||
if err != nil {
|
||||
|
@ -25,10 +25,11 @@ import (
|
||||
"github.com/docker/compose-cli/api/compose"
|
||||
|
||||
"github.com/compose-spec/compose-go/types"
|
||||
errdefs2 "github.com/docker/compose-cli/errdefs"
|
||||
moby "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/sanathkr/go-yaml"
|
||||
|
||||
errdefs2 "github.com/docker/compose-cli/errdefs"
|
||||
)
|
||||
|
||||
// NewComposeService create a local implementation of the compose.Service API
|
||||
@ -44,7 +45,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
|
||||
return errdefs2.ErrNotImplemented
|
||||
}
|
||||
|
||||
func getContainerName(c moby.Container) string {
|
||||
func getCanonicalContainerName(c moby.Container) string {
|
||||
// Names return container canonical name /foo + link aliases /linked_by/foo
|
||||
for _, name := range c.Names {
|
||||
if strings.LastIndex(name, "/") == 0 {
|
||||
|
@ -66,7 +66,7 @@ func (containers Containers) split(predicate containerPredicate) (Containers, Co
|
||||
func (containers Containers) names() []string {
|
||||
var names []string
|
||||
for _, c := range containers {
|
||||
names = append(names, getContainerName(c))
|
||||
names = append(names, getCanonicalContainerName(c))
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func (s *composeService) ensureScale(ctx context.Context, actual []moby.Containe
|
||||
missing := scale - len(actual)
|
||||
for i := 0; i < missing; i++ {
|
||||
number := next + i
|
||||
name := getContainerLogPrefix(project.Name, service, number)
|
||||
name := getContainerName(project.Name, service, number)
|
||||
eg.Go(func() error {
|
||||
return s.createContainer(ctx, project, service, name, number, false)
|
||||
})
|
||||
@ -109,7 +109,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
|
||||
|
||||
for _, container := range actual {
|
||||
container := container
|
||||
name := getContainerName(container)
|
||||
name := getCanonicalContainerName(container)
|
||||
|
||||
diverged := container.Labels[configHashLabel] != expected
|
||||
if diverged || service.Extensions[extLifecycle] == forceRecreate {
|
||||
@ -135,7 +135,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
|
||||
return eg.Wait()
|
||||
}
|
||||
|
||||
func getContainerLogPrefix(projectName string, service types.ServiceConfig, number int) string {
|
||||
func getContainerName(projectName string, service types.ServiceConfig, number int) string {
|
||||
name := fmt.Sprintf("%s_%s_%d", projectName, service.Name, number)
|
||||
if service.ContainerName != "" {
|
||||
name = service.ContainerName
|
||||
@ -218,12 +218,12 @@ func (s *composeService) createContainer(ctx context.Context, project *types.Pro
|
||||
|
||||
func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container) error {
|
||||
w := progress.ContextWriter(ctx)
|
||||
w.Event(progress.NewEvent(getContainerName(container), progress.Working, "Recreate"))
|
||||
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Working, "Recreate"))
|
||||
err := s.apiClient.ContainerStop(ctx, container.ID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := getContainerName(container)
|
||||
name := getCanonicalContainerName(container)
|
||||
tmpName := fmt.Sprintf("%s_%s", container.ID[:12], name)
|
||||
err = s.apiClient.ContainerRename(ctx, container.ID, tmpName)
|
||||
if err != nil {
|
||||
@ -241,7 +241,7 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Event(progress.NewEvent(getContainerName(container), progress.Done, "Recreated"))
|
||||
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Done, "Recreated"))
|
||||
setDependentLifecycle(project, service.Name, forceRecreate)
|
||||
return nil
|
||||
}
|
||||
@ -261,12 +261,12 @@ func setDependentLifecycle(project *types.Project, service string, strategy stri
|
||||
|
||||
func (s *composeService) restartContainer(ctx context.Context, container moby.Container) error {
|
||||
w := progress.ContextWriter(ctx)
|
||||
w.Event(progress.NewEvent(getContainerName(container), progress.Working, "Restart"))
|
||||
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Working, "Restart"))
|
||||
err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Event(progress.NewEvent(getContainerName(container), progress.Done, "Restarted"))
|
||||
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Done, "Restarted"))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -281,8 +281,8 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
|
||||
}
|
||||
id := created.ID
|
||||
for netName := range service.Networks {
|
||||
network := project.Networks[netName]
|
||||
err = s.connectContainerToNetwork(ctx, id, service.Name, network.Name)
|
||||
netwrk := project.Networks[netName]
|
||||
err = s.connectContainerToNetwork(ctx, id, netwrk.Name, service.Name, getContainerName(project.Name, service, number))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -290,9 +290,9 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, service string, n string) error {
|
||||
err := s.apiClient.NetworkConnect(ctx, n, id, &network.EndpointSettings{
|
||||
Aliases: []string{service},
|
||||
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, aliases ...string) error {
|
||||
err := s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
|
||||
Aliases: aliases,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@ -352,10 +352,10 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec
|
||||
}
|
||||
eg.Go(func() error {
|
||||
w := progress.ContextWriter(ctx)
|
||||
w.Event(progress.StartingEvent(getContainerName(container)))
|
||||
w.Event(progress.StartingEvent(getCanonicalContainerName(container)))
|
||||
err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
|
||||
if err == nil {
|
||||
w.Event(progress.StartedEvent(getContainerName(container)))
|
||||
w.Event(progress.StartedEvent(getCanonicalContainerName(container)))
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
@ -213,7 +213,7 @@ func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inher
|
||||
Resources: resources,
|
||||
}
|
||||
|
||||
networkConfig := buildDefaultNetworkConfig(s, networkMode)
|
||||
networkConfig := buildDefaultNetworkConfig(s, networkMode, getContainerName(p.Name, s, number))
|
||||
return &containerConfig, &hostConfig, networkConfig, nil
|
||||
}
|
||||
|
||||
@ -359,11 +359,11 @@ func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
|
||||
}
|
||||
}
|
||||
|
||||
func buildDefaultNetworkConfig(s types.ServiceConfig, networkMode container.NetworkMode) *network.NetworkingConfig {
|
||||
func buildDefaultNetworkConfig(s types.ServiceConfig, networkMode container.NetworkMode, containerName string) *network.NetworkingConfig {
|
||||
config := map[string]*network.EndpointSettings{}
|
||||
net := string(networkMode)
|
||||
config[net] = &network.EndpointSettings{
|
||||
Aliases: getAliases(s, s.Networks[net]),
|
||||
Aliases: append(getAliases(s, s.Networks[net]), containerName),
|
||||
}
|
||||
|
||||
return &network.NetworkingConfig{
|
||||
|
@ -94,7 +94,7 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
|
||||
for _, container := range containers {
|
||||
toDelete := container
|
||||
eg.Go(func() error {
|
||||
eventName := "Container " + getContainerName(toDelete)
|
||||
eventName := "Container " + getCanonicalContainerName(toDelete)
|
||||
w.Event(progress.StoppingEvent(eventName))
|
||||
err := s.apiClient.ContainerStop(ctx, toDelete.ID, nil)
|
||||
if err != nil {
|
||||
|
@ -21,9 +21,10 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/docker/compose-cli/api/compose"
|
||||
moby "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
|
||||
"github.com/docker/compose-cli/api/compose"
|
||||
)
|
||||
|
||||
func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.ContainerSummary, error) {
|
||||
@ -54,7 +55,7 @@ func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.
|
||||
|
||||
summary = append(summary, compose.ContainerSummary{
|
||||
ID: c.ID,
|
||||
Name: getContainerName(c),
|
||||
Name: getCanonicalContainerName(c),
|
||||
Project: c.Labels[projectLabel],
|
||||
Service: c.Labels[serviceLabel],
|
||||
State: c.State,
|
||||
|
Loading…
x
Reference in New Issue
Block a user