In my workplace docker is running behind firewall, only the port that is meant to serve webpage is excluded by rule.
The container starts but website does not open for same port.
If I host the website from machine running container using python -m SimpleHTTPServer it works.
docker container run --restart=always -p 8081: 8082 -it vue-js-app: latest
From the Docker documentation:
Publish or expose port (-p, --expose)
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of
the host machine. You can also specify udp and sctp ports. The Docker
User Guide explains in detail how to manipulate ports in Docker.
$ docker run --expose 80 ubuntu bash
This exposes port 80 of the container without publishing the port to
the host system’s interfaces.
And, from the Docker User Guide:
You also saw how you can bind a container’s ports to a specific port
using the -p flag. Here port 80 of the host is mapped to port 5000 of
the container:
$ docker run -d -p 80:5000 training/webapp python app.py
So, as an example of how to expose the ports you can use:
docker container run --restart always -p 8081:8082 -it vue-js-app:latest
Related
I'm learning docker and I'm testing running containers. It works fine only when I run a container listening on port 80.
Example:
Works OK:
docker run -d --name fastapicontainer_4 -p **8090**:80 fastapitest
docker run -d --name fastapicontainer_4 -p **8050**:80 fastapitest
Don´t work OK::
docker run -d --name fastapicontainer_4 -p **8050**:**8080** fastapitest
When I change the port where the program listens in the container and put a port different than 80, the page didn't work. Someone knows if it's possible to use a different port from 80? and how can I do it? I'm using fastapi.
Thanks,
Guillermo
The syntax of the -p argument is <host port>:<container port>. You can make the host port be anything you want, and Docker will arrange for it to redirect to the container port, but you cannot set the container port to an arbitrary value. There needs to be a service in the container listening on that port.
So if you have a web server in the container running on port 80, then the <container port> part of the -p option must always be 80, unless you change the web server configuration to listen on a different port.
What you are doing:
docker run -d --name fastapicontainer_4 -p 8050:8080 fastapitest
Explanation: What this is doing is forwarding the Host port 8050 to the container port 8080. In case your fastapi service is not listening on the port 8080, the connection will fail.
Host 8050 -> Container 8080
Correct way of doing it:
docker run -d --name fastapicontainer_4 -p 8080:80 fastapitest
Explanation: This is forwarding the host port 8080 to the container port 80
Host 8080 -> Container 80
Note: Docker doesn't validate the connection when you share a port, it just opens the gate so you can do whatever you want with that open port, so even if your service is not listening on that port, docker doesn't care.
You need to specify the custome port you want to use to run fastapi.
e.g.
uvicorn.run(app, host="0.0.0.0", port=8050)
Now if you run mapping 8050(or ay other) port on host with 8050 on container, it should work:
docker run -d --name fastapicontainer_4 -p 8050:8080 fastapitest
With virtualbox i can redirect a port through this menu:
Is there something similar that i can do in Docker for windows while using Hyper-V?
it's possible.
To expose a container’s internal port, you can start the container with the -P or -p flag. The exposed port is accessible on the host and the ports are available to any client that can reach the host.
example
docker run -d -p 8080:80 my_image nginx -g 'daemon off;'
your port 8080 in the host will be mapped to container internal port 80.
I'm new using docker.
I was asking me if is possible to run many containers on the same aws ec2 instance, triggering all port of the containers on one sigle port on the ec2 instance.
Suppose that we have 3 container:
container1 that run apache2 on port 80
container2 that run nginx on port 80
container3 with tomcat on port 8080
How can access to these services from my pc?
To do this I read that I need to expose ports by typing option -p externport : containerport but its not working
so i thought to change network and then I use option --network=host to trig all port to the same ip but it doesn't work.
I'd like just to accesso to these container in this way:
my-ec2-instance-public-dns:8080 -> container1
my-ec2-instance-public-dns:8081 -> container2
my-ec2-instance-public-dns:8082 -> container3
Can anyone help me?
It is not possible to map two services to the same port. You can map container ports to host ports using the -p flag, formatted hostPort:containerPort when you use container networking mode.
In your case, it could be
docker run -p 8080:80 nginx
docker run -p 8081:80 apache2
docker run -p 8082:8080 tomcat
Make sure you set the AWS security group of your virtual machine to allow traffic from your IP to ports 8080-8082.
I have apache installed inside a running ubuntu:14.04 container. How to access this in the browser of the host machine? The address showing inside the container is, 172.17.0.2. Please help.
By default, the apache httpd image exposes the port 80
docker run -it --rm --name my-apache-app -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4
So http://localhost should be enough.
In your case, make sure:
the httpd is actually running (docker exec -it <yourContainer> bash: ps -eaf),
you have mapped the port you are running Apache in your container to the host (-p 80:80 for instance).
By default, the apache image exposes the port 80, but you need config this in run command (-p):
docker run -d -p 80:80 httpd
The first number is port of Docker Host and the second one is port of container. This configuration will map all connections to port tcp 80 of docker host to the same port of container.
After that you can access your application in your browser, using 127.0.0.1, localhost or other IP Address of your interface.
What is the different between the following commands when creating a container in docker?
docker run -d -p 8080 sample/image
and
docker run -d -p 8080:8080 sample/image
I have seen majority of them use the second command, but I am not sure if they mean different things, or if the first is shorthand.
I couldn't find any material on this.
docker run -d -p 8080 sample/image
Exposes port 8080 of the container as an arbitrary port on the host. Which port that is is up to Docker.
Whereas,
docker run -d -p 8080:8080 sample/image
Exposes port 8080 of the container as port 8080 on the host.
In both cases, you can see the mapping using docker inspect, or even docker ps:
380af8c2bcc6 ubuntu "bash" 15 seconds ago Up 13 seconds 0.0.0.0:32768->1234/tcp elegant_meitner
In this case, port 1234 of the container is exposed as port 32768 on the host.