In this case, you can first commit the container so that the current state is saved into a new image: It could be the case that from the time you ran the container you already executed many commands and the idea of restarting the container and repeating them gives you the creeps. You'll just need to stop the container and run it again by providing the proper -p flag to map a local port to the target port of the container. Well, the first case is a no brainer and you most probably didn't land into this post if that's the case.
In this case, for example, the container's port 6739 has been mapped to the host's port 32770. For example, we can either run Redis and map its port to the same one on the host or to another one: On the other hand, publishing a port maps that port on a container to another one on the host, and as you most probably know already this can be done using the -p. One thing to be noted is that a container’s port is not accessible by the host, but it is accessible by other containers running in the same network. So if we run a container with Redis without mapping it to any port, we can see that we cannot reach it from the host even if in the Dockerfile it is marked as exposed. As an example, checkout the Dockerfile for Redis that exposes port 6379. Indeed it's a good practice in a Dockferfile to show what are the ports that are used by the container by exposing them. Exposing a port actually does nothing with regard to networking and it should be interpreted as a way of documenting the ports used by the container. One important thing to keep in mind is to understand the difference between an exposed port and a published one.Ī port can be exposed either using the -expose flag when running docker run or by using the EXPOSE command in a Dockerfile. Thus if you want to run for example a Redis container you won’t be able to use it from the host unless you’ll explicitly instruct the container to publish the port (port number 6379 in this case) and map it to one available port on the host.
#DOCKER RUN PORT MAP FREE#
Let's first have a look at how port mapping works first, feel free to skip this section if you're already familiar with the topic.īy default Docker containers are completely isolated from the host, this means that also the network is isolated.