searain
searain

Reputation: 3301

Docker for Mac. docker run -d -p 80:80 --name webserver nginx shows another container with this name. but docker ps shows empty list

I am learning "Docker for Mac"

$ docker run -d -p 80:80 --name webserver nginx

docker: Error response from daemon: Conflict. The name "/webserver" is already in use by container 728da4a0a2852869c2fbfec3e3df3e575e8b4cd06cc751498d751fbaa75e8f1b. You have to remove (or rename) that container to be able to reuse that name..

But when I run

$ docker ps

It shows no containers listed.

But due to the previous error message tells me that there is this container 728da....

I removed that container

$ dockder rm 728da4a0a2852869c2fbfec3e3df3e575e8b4cd06cc751498d751fbaa75e8f1b

Now I run this statement again

$ docker run -d -p 80:80 --name webserver nginx

It is working fine this time.

And then I run $ docker ps, I can see this new container is listed

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

3ecc0412fd31 nginx "nginx -g 'daemon off" 19 seconds ago Up 17 seconds 0.0.0.0:80->80/tcp, 443/tcp webserver

Note:

I am using "Docker for Mac".

But I had "Docker Box" installed on the Mac before. I don't know if that is the invisible "webserver" container comes from.

Upvotes: 0

Views: 642

Answers (2)

searain
searain

Reputation: 3301

The Docker Getting Started document asks the learners trying two statements first.

docker run hello-world

and

docker run -d -p 80:80 --name webserver nginx

I was wondering why I can run

docker run hello-world

many times but if I run

docker run -d -p 80:80 --name webserver nginx

the second time, I got the name conflicts error. Many beginners would be wondering too.

With your help and I did more search, now I understand

docker run hello-world,

we did not use --name, in this case, a random name was given so there will be no name conflicts error.

Thanks!

Upvotes: 0

BMitch
BMitch

Reputation: 263637

As activatedgeek says in the comments, the container must have been stopped. docker ps -a shows stopped containers. Stopped containers still hold the name, along with the contents of their RW layer that shows any changes made to the RO image being used. You can reference containers by name or container id which can make typing and scripting easier. docker start webserver would have restarted the old container. docker rm webserver would remove a stopped container with that name.

You can also abbreviate the container id's to the shortest unique name to save typing or a long copy/paste. So in your example, docker rm 728d would also have removed the container.

Upvotes: 1

Related Questions