Skip to content

Commit 16ab25c

Browse files
committed
fixing bug for renaming...container stop before rename
1 parent cf8ce25 commit 16ab25c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

docker/docker.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,26 @@ func (cl *Client) ContainerReplace(containerID string, image string, tag string)
539539
return err
540540
}
541541

542+
// stopping old container
543+
killAfter := time.Second * 5
544+
stopErr := cl.ContainerStop(containerID, &killAfter)
545+
if stopErr != nil {
546+
if cl.log != nil {
547+
cl.log.Error("failed to stop old container", containerID, stopErr)
548+
}
549+
return stopErr
550+
}
551+
542552
originalContainerName := originalContainer.Name
543553
tempContainerName := originalContainerName + "_temp"
544554
rErr := cl.ContainerRename(originalContainer.ID, tempContainerName)
545555
if rErr != nil {
556+
sErr := cl.ContainerStart(originalContainer.ID)
557+
if sErr != nil {
558+
if cl.log != nil {
559+
cl.log.Error("failed to start an old container back up after failing to rename", sErr)
560+
}
561+
}
546562
return rErr
547563
}
548564

@@ -554,6 +570,7 @@ func (cl *Client) ContainerReplace(containerID string, image string, tag string)
554570
if ccErr != nil {
555571
// revert renaming back the old container
556572
rbErr := cl.ContainerRename(containerID, originalContainerName)
573+
rbErr = cl.ContainerStart(containerID)
557574
if rbErr != nil {
558575
return rbErr
559576
}
@@ -570,6 +587,7 @@ func (cl *Client) ContainerReplace(containerID string, image string, tag string)
570587
}
571588
// undo previous changes to origial container and remove newly created container
572589
cerr := cl.ContainerRename(containerID, originalContainerName)
590+
cerr = cl.ContainerStart(containerID)
573591
cerr = cl.ContainerRemove(newlyCreatedContainer.ID)
574592
if cerr != nil {
575593
return cerr
@@ -578,16 +596,6 @@ func (cl *Client) ContainerReplace(containerID string, image string, tag string)
578596
return sErr
579597
}
580598

581-
// removing old container
582-
killAfter := time.Second * 5
583-
stopErr := cl.ContainerStop(containerID, &killAfter)
584-
if stopErr != nil {
585-
if cl.log != nil {
586-
cl.log.Error("failed to stop old container", containerID, stopErr)
587-
}
588-
return stopErr
589-
}
590-
591599
_, remErr := cl.ContainersPrune(filters.NewArgs())
592600
if remErr != nil {
593601
if cl.log != nil {

docker/docker_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func TestContainerReplace(t *testing.T) {
4848
t.Fatal(err)
4949
}
5050
}
51+
break
5152
}
5253
}
5354

0 commit comments

Comments
 (0)