Skip to content

docker rename fix to address the issue of renaming with the same name#23360

Merged
vdemeester merged 1 commit intomoby:masterfrom
sainath14:rename_error
Jun 24, 2016
Merged

docker rename fix to address the issue of renaming with the same name#23360
vdemeester merged 1 commit intomoby:masterfrom
sainath14:rename_error

Conversation

@sainath14
Copy link
Copy Markdown

@sainath14 sainath14 commented Jun 8, 2016

Fixes #23319

- What I did
docker rename cont_name cont_name , using same name to rename creates an inconsistent nameIndex for daemon. Added a check in daemon rename code to see if oldname is same as newname. If so return an error

- How to verify it
Before the fix
docker run -itd --name=old busybox
docker rename old old - successful without any error
docker rename old old - should complain there is no container with name old

After the fix
docker run -itd --name=old busybox
docker rename old old - error "Renaming a container with the same name as its current name"
docker rename old old - error "Renaming a container with the same name as its current name"

- Description for the changelog
A check in the docker daemon rename function to see if the current name of the container is same as the passed in new name. If the names match, daemon returns an error saying "names matched"

Signed-off-by: Sainath Grandhi [email protected]

Comment thread daemon/rename.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably error out instead of returning nil.

@cpuguy83
Copy link
Copy Markdown
Member

cpuguy83 commented Jun 8, 2016

Thanks for working on this!
Can you also add a test?

@sainath14 sainath14 force-pushed the rename_error branch 2 times, most recently from 1ac36c9 to fefced8 Compare June 8, 2016 02:23
@sainath14
Copy link
Copy Markdown
Author

sainath14 commented Jun 8, 2016

@cpuguy83 regarding returning nil, it is safe to silently ignore renaming with same name. Silently ignoring would become a problem in case a user tries using old name and new name that are very close and makes a typo to make them same. He will not realize that he made a typo :)

if we decide on how to handle this, i would proceed writing a test case accordingly

@cpuguy83
Copy link
Copy Markdown
Member

cpuguy83 commented Jun 8, 2016

No we should return an error.

@vdemeester
Copy link
Copy Markdown
Member

Agree with @cpuguy83, we should return an error. Client is then free to handle or ignore it 😇

@thaJeztah
Copy link
Copy Markdown
Member

hm, still slightly in favor of ignoring (there is no error, because the operation basically succeeded), but looks like I'm the minority 😅

@justincormack
Copy link
Copy Markdown
Contributor

justincormack commented Jun 8, 2016

What is the justification for returning an error? I really think it should return success. rename just means "I want this container to be called x", no reason to fail if it already is. Idempotency FTW.

@cpuguy83
Copy link
Copy Markdown
Member

cpuguy83 commented Jun 8, 2016

Because it's odd to rename something to the same name and is most likely a mistake.
Signaling to the user "hey, you screwed something up" is a good thing.

@justincormack
Copy link
Copy Markdown
Contributor

justincormack commented Jun 8, 2016

I don't think this PR is actually addressing the underlying issue, as the rename is currently also failing if I use the container id not the name:

whale:com.docker.driver.amd64-linux justin$ docker run --name=test -d -P nginx
284fb05acdd7d30d7d893ffed5e9eb157c179f55061cc7349de15a321788c171
whale:com.docker.driver.amd64-linux justin$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
284fb05acdd7        nginx               "nginx -g 'daemon off"   2 seconds ago       Up 1 seconds        0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   test
whale:com.docker.driver.amd64-linux justin$ docker rename 284fb05acdd7 test
whale:com.docker.driver.amd64-linux justin$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
284fb05acdd7        nginx               "nginx -g 'daemon off"   19 seconds ago      Up 17 seconds       0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   
whale:com.docker.driver.amd64-linux justin$ docker rename 284fb05acdd7 test
whale:com.docker.driver.amd64-linux justin$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
284fb05acdd7        nginx               "nginx -g 'daemon off"   25 seconds ago      Up 23 seconds       0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   
whale:com.docker.driver.amd64-linux justin$ docker rename 284fb05acdd7 test2
whale:com.docker.driver.amd64-linux justin$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
284fb05acdd7        nginx               "nginx -g 'daemon off"   31 seconds ago      Up 30 seconds       0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   test2
whale:com.docker.driver.amd64-linux justin$ docker rename 284fb05acdd7 test
whale:com.docker.driver.amd64-linux justin$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
284fb05acdd7        nginx               "nginx -g 'daemon off"   47 seconds ago      Up 46 seconds       0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   test

@cpuguy83
Copy link
Copy Markdown
Member

cpuguy83 commented Jun 8, 2016

This is because the name registrar doesn't return an error if the name is already reserved to the passed in reference.

Could update it to go ahead and return an error there or just explicitly check if the new name matches the container's ID/Name...
I think the latter is likely less work.

@sainath14
Copy link
Copy Markdown
Author

I agree @cpuguy83. Only other places where they call Reserve API from registrar package are in the new container creation flow. So it might not have made sense to check the name against the passed in id.

@sainath14 sainath14 force-pushed the rename_error branch 2 times, most recently from 6a246ca to 3e8c16e Compare June 8, 2016 22:32
@sainath14
Copy link
Copy Markdown
Author

ping @cpuguy83 @thaJeztah @vdemeester

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented Jun 21, 2016

LGTM

@vdemeester
Copy link
Copy Markdown
Member

LGTM 🐯

@vdemeester
Copy link
Copy Markdown
Member

@sainath14 could you update the "changelog" part of the PR description so that it matches the current behaviour ? 👼

@sainath14
Copy link
Copy Markdown
Author

@vdemeester change log updated.

@thaJeztah thaJeztah added this to the 1.12.0 milestone Jun 24, 2016
Comment thread daemon/rename.go
container.Lock()
defer container.Unlock()

if oldName == newName {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this before the container.Lock()?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just discussed this with @vdemeester and we saw some other things that may need to be improved (not related to this PR), so he's going to do a follow up

@vdemeester vdemeester merged commit 88d1ee6 into moby:master Jun 24, 2016
vdemeester added a commit to vdemeester/moby that referenced this pull request Jun 24, 2016
During the renaming of a container, no need to call `container.Lock()`
if `oldName == newName`.

This is a follow-up from moby#23360 (commit 88d1ee6)

Signed-off-by: Vincent Demeester <[email protected]>
tiborvass pushed a commit to tiborvass/docker that referenced this pull request Jul 1, 2016
During the renaming of a container, no need to call `container.Lock()`
if `oldName == newName`.

This is a follow-up from moby#23360 (commit 88d1ee6)

Signed-off-by: Vincent Demeester <[email protected]>
(cherry picked from commit 7e1ec8d)
liusdu pushed a commit to liusdu/moby that referenced this pull request Oct 30, 2017
… issue moby#23319

----
Fix DTS2017050408229
Cherry-pick from moby#23360
Issue: moby#23319

Signed-off-by: Sainath Grandhi <[email protected]>
Signed-off-by: majiuyue 00385406 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docker rename "oldname" "oldname" bug

8 participants