Docker windows Ports are not available: - docker

New to Docker. I am running Visual Studio 2019 community on Win 10 machine. Installed Docker desktop and created two solutions (service1 and service2). I am trying to run both of the solutions on their own containers.
I was able to build and run service1 using:
docker run -it --rm -p 3000:80 --name mymicroservicecontainer mymicroservice
Question what is 3000:80? is 80 a port? because I was able to run my api using http://localhost:3000/api/product/1 from browser.
Next, I am trying to run service2 on it's own container by:
docker run -it --rm -p 2000:80 --name myanotherservicecontainer myanotherservice
Since the port is 2000, I guess it should work however I get following error:
docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:2000: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
time="2020-04-08T14:22:41-04:00" level=error msg="error waiting for container: context canceled"
Is that because I have :80 same as service1? What is the solution? I am running commands on admin mode in command prompt.
Please help. Thank you.

docker run -it --rm -p 3000:80 --name mymicroservicecontainer mymicroservice
Answer to your first question is YES, 80 is a port.
Basically what -p 3000:80 does is that it maps TCP port 80 in the container to port 3000 on the Docker host.
The error you are getting for services is because port 2000 is occupied some other process. It's clearly mentioned in the error message as well.
docker: Error response from daemon: Ports are not available
If you try to map it to some other port(that is free on your machine), then it would work as expected.
Maybe try -p 1111:80 or -p 1234:80
Read this for more detail on docker container networking.

Related

Docker: Failed to connect to localhost port 8080 after 8 ms: Connection refused

I pulled a python docker image using:
docker pull python:3.8
Then I started a simple http server with:
docker run -ti -p 8080:8080 900972ffeecd python -m http.server 8080
If I docker exec into the container and run curl localhost:8080 I get the response I expect, but if I run the same curl command from the host machine it fails.
Shouldn't using -p 8080:8080 be enough to expose the port?
Potentially relevant info: I'm using colima on MacOS.
I hate to admit, but restarting my machine sent the issue away.

Cannot connect to docker container webapp from different system

If I run my docker container as
docker run -ti --privileged=true -p 5010:5000 myapp
I cannot connect to myapp by https://:5010
But if I run my docker container as
docker run -ti --privileged=true -p 5000:5000 myapp
I can connect to myapp by https://:5000 from different machine
What can be the issue? What option should I use to map container port to host port with different number?
output of nestat
Interestingly I can connect to my web server from same machine by wget command
This is the output of the netstat -ln when my docker is running.
This has been finally identified as firewall issue and the ports can be accessed if the firewall rule is changed.

running docker gives Ports are not available error

docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1
docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/sdk:3.1
docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/aspnet:3.1
When I run any of the above docker commands to create a container, I get the following error. And I get this for both for linux as well as windows.
C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
time="2020-03-24T17:20:44+05:30" level=error msg="error waiting for container: context canceled"
I tried the suggestion given in this SO ans to find the process Id and kill it.
Further I got the process hacker as suggested here to observe whats that process. Looks like its a system process.
Can anybody suggest what can be done?
-p 8080:80 says "forward port 8080 on the host to port 80 in the container". Port 80 is determined by the container image. Port 8080 is arbitrary—it's a port you're choosing.
So instead do -p 8081:80, and now you point your browser at localhost:8081 instead of localhost:8080.
If that doesn't work then maybe it's your firewall?
(See https://pythonspeed.com/articles/docker-connection-refused/ for diagrams of how port forwarding works).
You assign the same host port 8080 multiple times, which is not allowed - on any operating system.
After running this command
docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1
the first container gets immediately the port 8080 assigned on the host machine, what we can also see in the console screenshot that you provided. And others fail, because they simply don't get the port they want. So that all containers can be started, you should use a different port for each container, a la
docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1
docker run --rm -it -p 8081:80 mcr.microsoft.com/dotnet/core/sdk:3.1
docker run --rm -it -p 8082:80 mcr.microsoft.com/dotnet/core/aspnet:3.1
You should then be able to access those containers via the respective ports 8080, 8081, and 8082 (on localhost or local network IP of ur machine, e.g. 192.168.1.20).
This answer solves two errors I believe.
Error 1 (if the wrong port is specified in the Windows Defender Firewall for an existing rule for Docker):
Unable to find image 'docker102tutorial:latest' locally docker: Error
response from daemon: pull access denied for docker102tutorial,
repository does not exist or may require 'docker login': denied:
requested access to the resource is denied.
Error 2 (if no Windows Defender Firewall rule at all and #:8080 is specified in docker run command in the -p parameter):
Error response from daemon: Ports are not available: listen tcp
0.0.0.0:8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
In Windows 10, you will need to allow this through the Windows Defender Firewall. You'll get this dialog. You might be able to restrict the port for either the TCP by default (or UDP) line in the Windows Defender Firewall. The table screen shot of rules was taken before the port was modified and the last error was corrected. I believe the client in this case is WSL 2 and the server is Windows which means the incoming port needs to be opened on the server.
Allow Local Port 8080 in the Windows Defender Firewall so it matches the port after the ":" in the run command:
You will then get this error.
To correct, change from "Defer to user" to "Defer to application"
You can assign external port in the following ways:
Add an EXPOSE instruction in the Dockerfile such as EXPOSE 8080
Use the –expose flag at runtime to expose a port like the following: docker -expose=8080 test
Use the -p flag or -P flag in the Docker run string to publish a port as mentioned above, i.e. docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1
But you are allowed to do it only ONCE
Open PowerShell in administrator mode and enter the command
net stop http
The command lists (and gives you the option to stop) all services currently using port 80.
You can now start your docker container with port 80.

starting docker container with host mount to container

I am beginner and just started using docker, before posting here I google a lot but a lot of mixed confusing result.
I started docker with this command
docker run -itd --name dockWeb2 -v /var/www/wordpress/ -p 80:80 atozchevara/rpi-apache-php5
hoping I would be able to directly mount wordpress installation onto container , as by default it picks internal path of container /var/www/index.php, to override it I used -v flag. but it doesn't work.
I tried using multiple ports by passing -p arguments again for each port but that too gives error
docker run -itd --name dockWeb3 -v /var/www/wordpress/ -p 80:80 -p 22:22 atozchevara/rpi-apache-php5
66a959e4e99af8122705913005fcae12e2e8a5203da7b77ff1717751314fca28
docker: Error response from daemon: driver failed programming external connectivity on endpoint dockWeb3 (eb42a619a8c79961d35d59e0d8930a92541a20132525055afb3b0d2d87483e7f): Bind for 0.0.0.0:80 failed: port is already allocated.
otherwise Could have uploaded my wordpress using ssh to container's /var/www/ location.
For the first issue if you want to mount a volume from the host you need to use Bind mount a volume
docker run -itd --name dockWeb2 -v your_project_path:/var/www/wordpress/ 0.0.0.0:80 failed: port is already allocated. atozchevara/rpi-apache-php5
For the post using -p 80:80 you are publishing container port 80 to the host port 80, and if the host port is already in use you got an error 0.0.0.0:80 failed: port is already allocated. try to use a different port -p 9090:80.

trying to start an image in docker

I am trying to do this lab and type in the following command:
sudo docker run -it --name bdu_spark2 -P -p 4040:4040 -p 4041:4041 -p 8080:8080 -p 8081:8081 bigdatauniversity/spark2:latest /etc/bootstrap.sh -bash
But I get the following error. Is there a conflict between port 8080 of docker using it and other software trying to use it? I have restarted docker and made sure no other containers are running. Thanks for all the input.
Error response from daemon: Cannot start container 3c62472fe5f8481e5ee957550078f06106b45fc6bffe25669272e2ea924b5f36: failed to create endpoint bdu_spark2 on network bridge: Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use
This is usually caused because another container is using 8080 port on your docker host.
You can see your running containers by running: $ sudo docker ps
Either stop the other container, or choose a different host port to map your container's 8080 to.
In my case doing this with MySQL, I didn't realize it was because I already had a native MySQL running on that port.
docker run --name db --detach --env="MYSQL_ROOT_PASSWORD=123" --publish=3306:3306 mysql:latest
I did a netstat --all --numeric --program --inet --timers | grep 3306 and noticed it gave me 1418/mysqld. Then I did a ps aux | grep mysql and noticed that was the same process number started by /usr/sbin/mysqld which was my local MySQL instance on my host, nothing to do with containers.
Double check nothing is using those ports, especially 8080, which is very common for stand alone web servers, like those that ship with IDEs.
If you are using that port, you can use the --publish option to specify the host port to be different but still use the same port on the container. i.e. --publish=8081:8080, hostport:containerport.

Resources