mirror of
https://github.com/docker/compose.git
synced 2025-07-24 06:04:57 +02:00
Merge pull request #334 from docker/fix_rm_compose_container
Fix rm compose container
This commit is contained in:
commit
aeb446ef4f
@ -42,7 +42,10 @@ import (
|
|||||||
"github.com/docker/api/errdefs"
|
"github.com/docker/api/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
const singleContainerName = "single--container--aci"
|
const (
|
||||||
|
singleContainerName = "single--container--aci"
|
||||||
|
composeContainerSeparator = "_"
|
||||||
|
)
|
||||||
|
|
||||||
// ErrNoSuchContainer is returned when the mentioned container does not exist
|
// ErrNoSuchContainer is returned when the mentioned container does not exist
|
||||||
var ErrNoSuchContainer = errors.New("no such container")
|
var ErrNoSuchContainer = errors.New("no such container")
|
||||||
@ -135,7 +138,7 @@ func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.C
|
|||||||
if *container.Name == singleContainerName {
|
if *container.Name == singleContainerName {
|
||||||
containerID = *containerGroup.Name
|
containerID = *containerGroup.Name
|
||||||
} else {
|
} else {
|
||||||
containerID = *containerGroup.Name + "_" + *container.Name
|
containerID = *containerGroup.Name + composeContainerSeparator + *container.Name
|
||||||
}
|
}
|
||||||
status := "Unknown"
|
status := "Unknown"
|
||||||
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
|
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
|
||||||
@ -155,6 +158,10 @@ func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
||||||
|
if strings.Contains(r.ID, composeContainerSeparator) {
|
||||||
|
return errors.New(fmt.Sprintf("invalid container name. ACI container name cannot include %q", composeContainerSeparator))
|
||||||
|
}
|
||||||
|
|
||||||
var ports []types.ServicePortConfig
|
var ports []types.ServicePortConfig
|
||||||
for _, p := range r.Ports {
|
for _, p := range r.Ports {
|
||||||
ports = append(ports, types.ServicePortConfig{
|
ports = append(ports, types.ServicePortConfig{
|
||||||
@ -204,7 +211,7 @@ func (cs *aciContainerService) Stop(ctx context.Context, containerName string, t
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getGroupAndContainerName(containerID string) (groupName string, containerName string) {
|
func getGroupAndContainerName(containerID string) (groupName string, containerName string) {
|
||||||
tokens := strings.Split(containerID, "_")
|
tokens := strings.Split(containerID, composeContainerSeparator)
|
||||||
groupName = tokens[0]
|
groupName = tokens[0]
|
||||||
if len(tokens) > 1 {
|
if len(tokens) > 1 {
|
||||||
containerName = tokens[len(tokens)-1]
|
containerName = tokens[len(tokens)-1]
|
||||||
@ -258,7 +265,11 @@ func (cs *aciContainerService) Logs(ctx context.Context, containerName string, r
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciContainerService) Delete(ctx context.Context, containerID string, _ bool) error {
|
func (cs *aciContainerService) Delete(ctx context.Context, containerID string, _ bool) error {
|
||||||
cg, err := deleteACIContainerGroup(ctx, cs.ctx, containerID)
|
groupName, containerName := getGroupAndContainerName(containerID)
|
||||||
|
if groupName != containerID {
|
||||||
|
return errors.New(fmt.Sprintf("cannot delete service %q from compose app %q, you must delete the entire compose app with docker compose down", containerName, groupName))
|
||||||
|
}
|
||||||
|
cg, err := deleteACIContainerGroup(ctx, cs.ctx, groupName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -315,9 +326,16 @@ func (cs *aciComposeService) Up(ctx context.Context, opts cli.ProjectOptions) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciComposeService) Down(ctx context.Context, opts cli.ProjectOptions) error {
|
func (cs *aciComposeService) Down(ctx context.Context, opts cli.ProjectOptions) error {
|
||||||
project, err := cli.ProjectFromOptions(&opts)
|
var project types.Project
|
||||||
if err != nil {
|
|
||||||
return err
|
if opts.Name != "" {
|
||||||
|
project = types.Project{Name: opts.Name}
|
||||||
|
} else {
|
||||||
|
fullProject, err := cli.ProjectFromOptions(&opts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
project = *fullProject
|
||||||
}
|
}
|
||||||
logrus.Debugf("Down on project with name %q\n", project.Name)
|
logrus.Debugf("Down on project with name %q\n", project.Name)
|
||||||
|
|
||||||
|
@ -17,8 +17,11 @@
|
|||||||
package azure
|
package azure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/api/containers"
|
||||||
|
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
@ -42,6 +45,22 @@ func (suite *BackendSuiteTest) TestGetContainerName() {
|
|||||||
Expect(container).To(Equal("service1"))
|
Expect(container).To(Equal("service1"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *BackendSuiteTest) TestErrorMessageDeletingContainerFromComposeApplication() {
|
||||||
|
service := aciContainerService{}
|
||||||
|
err := service.Delete(context.TODO(), "compose-app_service1", false)
|
||||||
|
|
||||||
|
Expect(err).NotTo(BeNil())
|
||||||
|
Expect(err.Error()).To(Equal("cannot delete service \"service1\" from compose app \"compose-app\", you must delete the entire compose app with docker compose down"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *BackendSuiteTest) TestErrorMessageRunSingleContainerNameWithComposeSeparator() {
|
||||||
|
service := aciContainerService{}
|
||||||
|
err := service.Run(context.TODO(), containers.ContainerConfig{ID: "container_name"})
|
||||||
|
|
||||||
|
Expect(err).NotTo(BeNil())
|
||||||
|
Expect(err.Error()).To(Equal("invalid container name. ACI container name cannot include \"_\""))
|
||||||
|
}
|
||||||
|
|
||||||
func TestBackendSuite(t *testing.T) {
|
func TestBackendSuite(t *testing.T) {
|
||||||
RegisterTestingT(t)
|
RegisterTestingT(t)
|
||||||
suite.Run(t, new(BackendSuiteTest))
|
suite.Run(t, new(BackendSuiteTest))
|
||||||
|
@ -22,9 +22,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/compose-spec/compose-go/cli"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/compose-spec/compose-go/cli"
|
||||||
|
|
||||||
"github.com/docker/api/context/cloud"
|
"github.com/docker/api/context/cloud"
|
||||||
|
|
||||||
"github.com/docker/api/backend"
|
"github.com/docker/api/backend"
|
||||||
|
@ -297,7 +297,7 @@ func (s *E2eACISuite) TestACIBackend() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
s.T().Run("shutdown compose app", func(t *testing.T) {
|
s.T().Run("shutdown compose app", func(t *testing.T) {
|
||||||
s.NewDockerCommand("compose", "down", "-f", composeFile, "--project-name", "acidemo").ExecOrDie()
|
s.NewDockerCommand("compose", "down", "--project-name", "acidemo").ExecOrDie()
|
||||||
})
|
})
|
||||||
|
|
||||||
s.T().Run("switches back to default context", func(t *testing.T) {
|
s.T().Run("switches back to default context", func(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user