55
votes

I am working with Docker and when I first created a container, I managed to work with it properly. Today I rebooted my computer and set myself to work with the Docker container again.

I tried to "activate" the container like this:

docker run -it --rm -p 5000:5000 -v `pwd`:/app myproject

And this is the message that I get back:

WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : [8.8.8.8 8.8.4.4]
2014/07/28 12:34:32 Error: Cannot start container a152dd9040ab4be0f20ace96ba4a9b0a3c50fa89034512c2b9fcb986db912027: port has already been allocated

So I tried to find out what process is listening on port 5000 and kill it. Just like this:

# netstat -tulpn | grep :5000
tcp6       0      0 :::5000                 :::*                    LISTEN    3992/docker.io
# kill -9 3992

I also tried restarting the docker service, like this:

sudo service docker.io restart

But none of these things have worked out. I am still getting the same error message when trying to activate my container:

# docker run -it --rm -p 5000:5000 -v `pwd`:/app myproj
WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : [8.8.8.8 8.8.4.4]
2014/07/28 12:34:14 Error: Cannot start container b28ac5c4a425068ac1827bade6dd89e95be32f3e94c982300eba006822971ab1: port has already been allocated

Googling for a solution I found that this seems to be a known issue, but maybe you can help me find a way out of this issue.

UPDATE:

Just as requested by @Marcus Hughes , this is the output of docker ps:

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
3fdfc9ecf30f        7e0ca0814a33        /bin/sh -c composer    4 hours ago         Up About an hour    0.0.0.0:5000->5000/tcp   naughty_galileo  

And if I do docker ps -a, this is what I get:

$ sudo docker ps -a
CONTAINER ID        IMAGE                        COMMAND                CREATED              STATUS              PORTS                    NAMES
69400a0e21f5        stenocaption-server:latest   /bin/sh -c composer    About a minute ago   Exit 0                                       agitated_feynman           
a3c352d48ab5        stenocaption-server:latest   /bin/sh -c composer    6 minutes ago        Exit 0                                       grave_shockley             
4c3c823bda55        stenocaption-server:latest   /bin/sh -c composer    2 hours ago          Exit 0                                       condescending_heisenberg   
b9083a0af2cd        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       kickass_lumiere            
0d51ea8c4377        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       sick_ritchie               
a152dd9040ab        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       happy_thompson             
b28ac5c4a425        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       cocky_franklin             
d449f40487ca        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       evil_torvalds              
b485da98ca6d        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       tender_nobel6              
8b73b154c8aa        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       dreamy_ptolemy             
10d327c46d95        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       boring_morse               
a3397c0c4dfb        7e0ca0814a33                 /bin/sh -c composer    2 hours ago          Exit 0                                       thirsty_engelbart          
e6440629fac1        7e0ca0814a33                 /bin/sh -c composer    3 hours ago          Exit 0                                       distracted_bohr            
cb718204384e        7e0ca0814a33                 /bin/sh -c composer    3 hours ago          Exit 0                                       nostalgic_mclean           
ca62ccc30227        7e0ca0814a33                 /bin/sh -c composer    3 hours ago          Exit 0                                       evil_heisenberg            
83ed0727883f        7e0ca0814a33                 /bin/sh -c composer    3 hours ago          Exit 0                                       nostalgic_curie            
1100e926ef29        7e0ca0814a33                 /bin/sh -c composer    3 hours ago          Exit 0                                       tender_nobel               
1ed76e31d8f5        7e0ca0814a33                 /bin/sh -c composer    3 hours ago          Exit 0                                       grave_hawking              
ad429a4ab116        7e0ca0814a33                 /bin/sh -c composer    3 hours ago          Exit 0                                       condescending_nobel        
5790e0e9c21f        7e0ca0814a33                 /bin/sh -c composer    4 hours ago          Exit 0                                       boring_curie               
3fdfc9ecf30f        7e0ca0814a33                 /bin/sh -c composer    4 hours ago          Up About an hour    0.0.0.0:5000->5000/tcp   naughty_galileo            
7372b8327065        b0d5a13e5ea7                 /bin/sh -c apt-get-i   40 hours ago         Exit 127                                     high_torvalds              
d4482ca96df9        10aac624035d                 /bin/sh -c apt-get i   40 hours ago         Exit 0                                       sad_pare                   
0ae7489bd53e        6edd40908159                 /bin/sh -c apt-get i   41 hours ago         Exit 0                                       dreamy_davinci             
13c1a8670908        ubuntu:14.04                 /bin/sh -c apt-get u   41 hours ago         Exit 0                                       loving_babbage             
18e20ff7f4ec        ubuntu:14.04                 /bin/sh -c apt-get u   41 hours ago         Exit 1                                       stupefied_poincare         
0965776b2ee9        ubuntu:14.04                 /bin/sh -c apt-get u   41 hours ago         Exit 1                                       goofy_davinci              
aa3d871f6d1c        ubuntu:14.04                 /bin/sh -c apt-get u   41 hours ago         Exit 1                                       insane_pasteur             
cb266936771d        ubuntu:14.04                 /bin/sh -c apt-get u   41 hours ago         Exit 1                                       distracted_turing          
10d25a9c965b        ubuntu:14.04                 /bin/sh -c apt-get u   41 hours ago         Exit 1                                       kickass_ptolemy            
729afe65b3f5        ubuntu:14.04                 /bin/sh -c apt-get u   42 hours ago         Exit 1                                       dreamy_tesla               
a53c5707e667        ubuntu:14.04                 /bin/sh -c apt-get u   42 hours ago         Exit 1                                       ecstatic_bohr              
ec97aaae1bda        ubuntu:14.04                 /bin/sh -c apt-get u   42 hours ago         Exit 1                                       romantic_bell              
13fe8539439c        ubuntu:14.04                 /bin/sh -c apt-get u   42 hours ago         Exit 1                                       romantic_shockley          
deaff10c2ed3        ubuntu:14.04                 /bin/sh -c apt-get u   42 hours ago         Exit 1                                       trusting_shockley          
c49494c24595        ubuntu:14.04                 /bin/sh -c apt-get u   42 hours ago         Exit 1                                       loving_bell                
3972c67fbd92        ubuntu:14.04                 /bin/sh -c apt-get u   42 hours ago         Exit 1                                       stupefied_euclid
6
Can you show the output of docker ps?Marcus Hughes
I just updated my original post with the docker ps command output. Hope it throws some light on how to solve this.Xar
It looks like 3fdfc9ecf30f is using port 5000. Can you do docker kill 3fdfc9ecf30f? Or is the container important?Marcus Hughes
Thank you, that worked! The docker kill is just what I needed. If you want you can post the answer and I will mark it as valid.Xar
On Fedora I solved it by systemctl restart docker.serviceJiri Kremser

6 Answers

67
votes

From the docker ps output, there is a container which is listening on port 5000 as you can see from the 0.0.0.0:5000->5000/tcp under the ports column.

You can kill this container with docker kill container. At which point it will free up the port. In your case:

docker kill 3fdfc9ecf30f
14
votes

I've fixed it by restarting the docker service:

sudo service docker restart

Note: I'm using Linux Mint, by the way.

9
votes

One reason I've come across is that in some versions of docker (pre 1.2), there's a bug where if it detects a port is already assigned (perhaps even to a system, not docker run, program like nginx) it will continue to fail to reassign it even if you stop the conflicting thing, until you restart docker.

Check that nothing else is using the port, then restart docker. (service docker.io restart)

reference: https://github.com/docker/docker/issues/6476

3
votes

Use docker stop $(docker ps | grep ":PORT_NUMBER" | awk '{print $1}')

Just replace the port number by the port on which the docker container is running.

For example: Running docker stop $(docker ps | grep ":8082" | awk '{print $1}') will stop the container running at port 8082

1
votes

For Windows users, quoting from the Docker documentation:

On Windows systems, CTRL+C does not stop the container. So, first type CTRL+C to get the prompt back (or open another shell), then type docker container ls to list the running containers, followed by docker container stop <Container NAME or ID> to stop the container. Otherwise, you get an error response from the daemon when you try to re-run the container.

Additionally, this command stops all running containers:

docker stop $(docker ps -q)
0
votes

Stopping previous container, killing the process didn't work for me.

What worked was stopping docker daemon and starting it again.