Unable to connect to http port when 'docker run -it --rm -p 8080:8080 my_image' is used

I have created an image ‘my_image’. When I run the container using ‘docker run -d -p 8080:8080 my_image’ I can connect to the http port from my host at ‘172.17.0.1:8080’. But, when I run the container using ‘docker run -it --rm -p 8080:8080 my_image /bin/bash’ I am unable to connect to the http port. My docker version is ‘19.03.05’ and my host is Ubuntu 18.04. I appreciate any guidance to correct the problem.

Probably your application starts with CMD in the Dockerfile and you overwrite this command when you start the container with /bin/bash.
Start the container as usual in the background, then connect to it with

docker container exec -it CONTAINER_ID bash

The CONTAINER_ID is displayed then it is created. It is easier when give it a --name at startup.

Thank you very much. Your suggestion worked for me. If I want to start my container in ‘/bin/bash’ all the time is their any alternative to ‘CMD’ in the Dockerfile while building the image?

No, you don’t want to do that. :grin:
If your container hosts your application, this is the foreground process all the time. There are some reasons to open a bash shell in it from time to time. But why always? If it’s about editing your code, you could mount a local folder into the container and edit the code locally.

1 Like