Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit aeb446e

Browse files
authored
Merge pull request #334 from docker/fix_rm_compose_container
Fix rm compose container
2 parents c4c0c24 + da3c3da commit aeb446e

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

Diff for: azure/backend.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ import (
4242
"github.com/docker/api/errdefs"
4343
)
4444

45-
const singleContainerName = "single--container--aci"
45+
const (
46+
singleContainerName = "single--container--aci"
47+
composeContainerSeparator = "_"
48+
)
4649

4750
// ErrNoSuchContainer is returned when the mentioned container does not exist
4851
var ErrNoSuchContainer = errors.New("no such container")
@@ -135,7 +138,7 @@ func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.C
135138
if *container.Name == singleContainerName {
136139
containerID = *containerGroup.Name
137140
} else {
138-
containerID = *containerGroup.Name + "_" + *container.Name
141+
containerID = *containerGroup.Name + composeContainerSeparator + *container.Name
139142
}
140143
status := "Unknown"
141144
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
@@ -155,6 +158,10 @@ func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.C
155158
}
156159

157160
func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
161+
if strings.Contains(r.ID, composeContainerSeparator) {
162+
return errors.New(fmt.Sprintf("invalid container name. ACI container name cannot include %q", composeContainerSeparator))
163+
}
164+
158165
var ports []types.ServicePortConfig
159166
for _, p := range r.Ports {
160167
ports = append(ports, types.ServicePortConfig{
@@ -204,7 +211,7 @@ func (cs *aciContainerService) Stop(ctx context.Context, containerName string, t
204211
}
205212

206213
func getGroupAndContainerName(containerID string) (groupName string, containerName string) {
207-
tokens := strings.Split(containerID, "_")
214+
tokens := strings.Split(containerID, composeContainerSeparator)
208215
groupName = tokens[0]
209216
if len(tokens) > 1 {
210217
containerName = tokens[len(tokens)-1]
@@ -258,7 +265,11 @@ func (cs *aciContainerService) Logs(ctx context.Context, containerName string, r
258265
}
259266

260267
func (cs *aciContainerService) Delete(ctx context.Context, containerID string, _ bool) error {
261-
cg, err := deleteACIContainerGroup(ctx, cs.ctx, containerID)
268+
groupName, containerName := getGroupAndContainerName(containerID)
269+
if groupName != containerID {
270+
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))
271+
}
272+
cg, err := deleteACIContainerGroup(ctx, cs.ctx, groupName)
262273
if err != nil {
263274
return err
264275
}
@@ -315,9 +326,16 @@ func (cs *aciComposeService) Up(ctx context.Context, opts cli.ProjectOptions) er
315326
}
316327

317328
func (cs *aciComposeService) Down(ctx context.Context, opts cli.ProjectOptions) error {
318-
project, err := cli.ProjectFromOptions(&opts)
319-
if err != nil {
320-
return err
329+
var project types.Project
330+
331+
if opts.Name != "" {
332+
project = types.Project{Name: opts.Name}
333+
} else {
334+
fullProject, err := cli.ProjectFromOptions(&opts)
335+
if err != nil {
336+
return err
337+
}
338+
project = *fullProject
321339
}
322340
logrus.Debugf("Down on project with name %q\n", project.Name)
323341

Diff for: azure/backend_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package azure
1818

1919
import (
20+
"context"
2021
"testing"
2122

23+
"github.com/docker/api/containers"
24+
2225
"github.com/stretchr/testify/suite"
2326

2427
. "github.com/onsi/gomega"
@@ -42,6 +45,22 @@ func (suite *BackendSuiteTest) TestGetContainerName() {
4245
Expect(container).To(Equal("service1"))
4346
}
4447

48+
func (suite *BackendSuiteTest) TestErrorMessageDeletingContainerFromComposeApplication() {
49+
service := aciContainerService{}
50+
err := service.Delete(context.TODO(), "compose-app_service1", false)
51+
52+
Expect(err).NotTo(BeNil())
53+
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"))
54+
}
55+
56+
func (suite *BackendSuiteTest) TestErrorMessageRunSingleContainerNameWithComposeSeparator() {
57+
service := aciContainerService{}
58+
err := service.Run(context.TODO(), containers.ContainerConfig{ID: "container_name"})
59+
60+
Expect(err).NotTo(BeNil())
61+
Expect(err.Error()).To(Equal("invalid container name. ACI container name cannot include \"_\""))
62+
}
63+
4564
func TestBackendSuite(t *testing.T) {
4665
RegisterTestingT(t)
4766
suite.Run(t, new(BackendSuiteTest))

Diff for: example/backend.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"context"
2323
"errors"
2424
"fmt"
25-
"github.com/compose-spec/compose-go/cli"
2625
"io"
2726

27+
"github.com/compose-spec/compose-go/cli"
28+
2829
"github.com/docker/api/context/cloud"
2930

3031
"github.com/docker/api/backend"

Diff for: tests/aci-e2e/e2e-aci_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func (s *E2eACISuite) TestACIBackend() {
297297
})
298298

299299
s.T().Run("shutdown compose app", func(t *testing.T) {
300-
s.NewDockerCommand("compose", "down", "-f", composeFile, "--project-name", "acidemo").ExecOrDie()
300+
s.NewDockerCommand("compose", "down", "--project-name", "acidemo").ExecOrDie()
301301
})
302302

303303
s.T().Run("switches back to default context", func(t *testing.T) {

0 commit comments

Comments
 (0)