Docker image not exposing - docker

I have a docker image which is running perfectly in file I have done
EXPOSE 8080
and I run my image using
sudo docker run -p 8080 <image-name> <Argument1> <Argument2>
Image runs but when I go to
localhost:8080
I get page not found error. Is there no way I can see some response or something on localhost:8080?

The option -p 8080 will expose the container:8080 port into a host:random-port.
The option --publish works as follow: -p ip:hostPort:containerPort. Using a -P| --publish-all will automatically bind any container-opened port into random-host port.
It is also possible to publish range of ports: -p 1234-1236:1222-1224.

Related

Run docker image on specific port

I am new to Docker.
I was trying to dockerize a simple static website using nginx base image.
The applicatio runs fine on local server when i run.
docker run -d -P <container-name>
So, here the app runs on some random port on which i am able to see my app running.
while, when i try to specify port by using the following command:
docker run -d -p 5000:5000 --restart=always --name app mkd63/leo-electricals
The page at localhost:5000 shows site cant be reached.
My Dockerfile is:
FROM nginx:alpine
COPY . /usr/share/nginx/html
EXPOSE 5000
By default, the nginx image listens on port 80 inside the container.
Publishing a port creates a port forward from the host into the container. This doesn't modify what port the application is listening on inside the container, so if you forward to an unused port, you won't connect to anything.
Exposing a port in the Dockerfile is documentation by the image creator to those running the image, but doesn't modify container networking or have any control over what the application running inside the container is doing. With docker, the -P flag uses that documentation to publish every exposed port.
To map port 5000 on the host to nginx listening on port 80 inside the container, use:
docker run -d -p 5000:80 --restart=always --name app mkd63/leo-electric

Is it possible to set a port under "host mode" in docker?

Is it possible to config the port number in docker under "host mode"?
I wanna bind the application to 5050 port instead of 80 port .
However, when I run below script, it will default bind in 80 port:
sudo docker run --name=myname --network host -d webapi:1.0.0 --restart=always
So I tried to run with "-p 5050", for example
sudo docker run --name=myname --network host -d webapi:1.0.0 --restart=always -p 5050
sudo docker run --name=myname --network host -d webapi:1.0.0 --restart=always -p 5050:5050
Unfortunately, my Linux terminal returned with :
WARNING: Published ports are discarded when using host network mode
The docker image looks like something like this:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-2.2
And my Dockerfile looks like this:
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /publish
COPY . .
EXPOSE 5050
ENTRYPOINT ["dotnet", "WebApi.dll"]
Easy way out would be to run your application on 5050 port. Once your container is up & running you must be able to access your app on port 5050 on Docker host. You need not to bind any kind of ports because you are using the host network itself.
Thanks user "#atline" for the answer.
Only 80 port is allowed.
More reference is found here:
https://docs.docker.com/network/host/

How to access docker container at digitalocean?

When I run following commands, I can access 127.0.0.1:80 at localhost successfully.
docker run -p 127.0.0.1:80:80 --name Mynginx -dt nginx
docker exec -it Mynginx bash
But If I run the commands at digitalocean's DROPLETS, how to access it now? ( I tried to access DROPLETS's IP address:80, but I get nothing.)
You need to EXPOSE the port. See the documentation for more information on how.
Running from the command-line
If you run the containers from the command-line, you can map the ports with the -p tag. You can map multiple ports.
docker run -dt -p 80:80 --name Mynginx nginx
or
docker run -dt -p 80:80 -p 443:443 --name Mynginx nginx
Docker-compose
If you're using docker-compose, you can add the EXPOSE tag in your yaml file.
version: '2.3'
services:
my_container:
container_name: "Mynginx"
image: nginx:latest
expose:
- "80"
You need to update your droplets firewall settings to allow incoming connections to port :80. To update this select your droplet.
Then go to Networking -> Manage Firewalls -> Create Firewall
Then under Inbound Rules create a new HTTP rule by selecting HTTP from the dropdown menu. Scroll down and apply this firewall to your droplet, then you should be able to receive inbound traffic on port :80. You will have to add a similar rule for any other ports you want to open up.
See here for more details.

Close Docker expose port from parent file

I have made my own Dockerfile for a apache server that starts with an standard parent file. See first line in my Dockerfile below:
FROM php:7.0-apache
EXPOSE 8080
This parent exposes port 80. Now have I exposed port 8080 in my Dockerfile. Only when I run it both ports are exposed.
It it posible to close the parent port 80 in my Dockerfile? As I cannot edit that file.
Unfortunately, this is currently impossible.
But you can follow this issue on Docker's GitHub. It's from 2014, but it is still Open. Never know...
As an alternative, rather than editing the configuration in the docker image you can also use port mapping to achieve the same result.
For example mapping port 8080 on the host to port 80 within the docker image, like so:
docker run -d -p 8080:80 image_name
Apart from telling docker to expose the port, you must configure apache to listen 8080, doing these two editions:
/etc/apache2/ports.conf
/etc/apache2/sites-enabled/000-default.conf
Dockerfile:
FROM php:7.0-apache
RUN sed -si 's/Listen 80/Listen 8080/' /etc/apache2/ports.conf
RUN sed -si 's/VirtualHost .:80/VirtualHost *:8080/' /etc/apache2/sites-enabled/000-default.conf
EXPOSE 8080
Then:
docker run -d -p 8080:8080 image_name
Edit:
About port 80. The port is not opened and it's not exposed if you use the previous command. If you don't see the ->80 symbol in docker ps, the port is not exposed:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
100fbb367226 php7 "docker-php-entryp..." 2 minutes ago Up 46 seconds 80/tcp, 0.0.0.0:8080->8080/tcp musing_snyder
And:
▶ docker inspect 100fbb367226 -f "{{json .NetworkSettings.Ports}}"
{"80/tcp":null,"8080/tcp":[{"HostIp":"0.0.0.0","HostPort":"8080"}]}

Docker run container on different port

I am new to docker. Just tried to run a container on port 80
docker run -p 80:80 kitematic/hello-world-nginx
and it seems to be working fine. I can get to the site at http://192.168.99.100/
but i tried changing the port to
docker run -p 70:50 kitematic/hello-world-nginx
and i cannot get to site at http://192.168.99.100:70. So how can i set a different port and connect to site please?
Please try:
docker run -p 70:80 kitematic/hello-world-nginx
binding port is probably mistyped in the command you provided.

Resources