3

I have been trying to connect to a docker container via ip, but reamins unsuccessful. When I used "docker inspect container-id" I get this result.

Docker container port

My virtual box settings are by default:

Virtual box settings

Can someone help me resolving this issue?

Community
  • 1
  • 1
h_a86
  • 281
  • 1
  • 8
  • 16
  • When you ran docker, did you use the `-p ` parameter to make that port available (or use `expose` in the Dockerfile)? – Gerrat Jul 23 '14 at 13:15
  • No, I din't. Can you tell me how to run the same container with p parameter? – h_a86 Jul 23 '14 at 13:17

2 Answers2

9

When running docker, you can specify which port(s) you'd like to be accessible to the outside world. Basic syntax is:

docker run -p hostPort:containerPort imageName

or just

docker run -p hostPort imageName

In the first case, externally, hostPort will be used, but inside the container: containerPort will be used. In the second instance, you'd just be using that port both inside and outside your container.

You can also create an image with ports exposed by using the EXPOSE command in a Dockerfile.

Gerrat
  • 28,863
  • 9
  • 73
  • 101
  • I commit the container, stopped it, and then run the latest image by using docker run -p 2375 imageName, but still no result... – h_a86 Jul 23 '14 at 13:52
  • Does `docker inspect new_container_id` show the same thing? – Gerrat Jul 23 '14 at 14:10
  • Here you are specifying the image name, what if we have a container with modification and data in the database, and we want to expose a certain port on that container. – Bionix1441 Jun 20 '23 at 09:05
  • 1
    @Bionix1441: You can't easily do that, but there are some workarounds. [Here](https://stackoverflow.com/questions/19897743/exposing-a-port-on-a-live-docker-container/42071577#42071577) is one – Gerrat Jun 20 '23 at 13:53
2

You need to perform port forwarding or just simply expose port.

Port Forwarding:

docker run -p 2022:22 -p 2375:2375

Expose Port:

docker run -p 22 -p 2375

Community
  • 1
  • 1
Shriram Sharma
  • 599
  • 1
  • 5
  • 19