Docker Community Forums

Share and learn in the Docker community.

Problem of maping localhost port number to Docker container inside Dockerfile


This is my Docker file looks like for tomcat image

ENV env dev
ENV name chetan
EXPOSE 8080:8080
CMD [“”, “run”]

The problem I’m facing is that port 8080 that is defined inside my Dockerfile is cannot able to map with my localhost machine can anyone please tell my EXPOSE 8080:8080 ? is correct or not?

I have to write this everytime

docker run -itd -p 8080:8080 my_tomat_image

I want that port 8080 that is defined inside my docker file is mapped with my localhost automatically I don’t need to define -p 8080:8080 when I run the docker image using docker run command

Should be EXPOSE 8080

I have done this but my localhost couldn’t be mapped with my container port

Did you try -P
it will map the exposed port from the docker file EXPOSE Automagically. I think it tries to match the port, but iff it can’t I think it picks an open port…
Also did you check that the port is not in use before deploy?

Can you show use the ouput of docker ps -a. I am currious if the container actualy keeps running…

Thanks for replay but after my research what I found is that there is no way to map my localhost port to the container port in Docker file that only expose the port but for mapping I have to pass argument inside docker run command there is no alternative for this thing

nope docker containers are not running as docker port doesn’t mapped to localhost

Steps to reproduce the behavior
Create ASP.NET Core 1.1 web application with a project name AspNetCoreWebApp1
Create Docker image using these commands
FROM microsoft/dotnet:nanoserver
ARG source=./bin/Release/PublishOutput/
COPY $source /app/
ENTRYPOINT [“dotnet”, “AspNetCoreWebApp1.dll”]

Build the image by calling docker build . -t AspNetCoreWebApp1 from the project root directory.
Run container from the image using docker run -it -d -p 88:8282 AspNetCoreWebApp1
Open http://locahost:88 in the browser on the host

Yes that’s work but I don’t want to add this -p in docker run command I want that the mapping will works inside dockerfile

docker run -it -d -p 88:8282 AspNetCoreWebApp1

There is no such thing as a mapping inside a Dockerfile.
EXPORT is merly an instruction with documentation character… it does absolutly nothing :wink:

1 Like

You need to do this in a compose file. Under your service, you can have something like:
- “8080:80”
to map the internal port 80 to 8080 on the host.

oky thnx Iet me try this