I came across the command docker run -d -p 80:80 docker/getting-started, which appeared to be a demonstration command to initialize a container. However, I am curious as to what the 80:80 does in regard to the overall command. What does this do? (If an answer to my question can be found in their documentation or some other resource, please do link it as I have done a good deal of searching around to no avail and am more than willing to do the reading myself. Thanks!)
The -p HOST_PORT:CONTAINER_PORT flag binds your container port to the host port. In your case it's 80:80, which the containers port 80 is bound to the port 80 of the host. (default is TCP)
https://docs.docker.com/config/containers/container-networking/
based on docker run -d -p 80:80 docker/getting-started
docker run:start your Container
-d detach your container when you start it
-p 80:80: map your container port to your host port that's mean when you connect to 80 port host you are connect to 80 port container.Architecture is -p {host_port}:{container_port}
docker/getting-started:is your image name
Related
I create an Docker's image with name is sample, then I installed nginx on both of them that listen to port 80 and it shows simple index.html.
then I use below commands to run contianers:
docker run -it -p 80:80 --name sample1 sample
docker run -it -p 81:80 --name sample2 sample
and I successfully see the index.html from main OS from two containers, but when I go inside container sample1 I couldn't see the index.html of sample2 and It does not work conversely either.
The -p option is the shortform for ports. When you do -p you are binding the container's port 80 to its host's port 80.
So container sample1 and sample2 are just merely binding their respective port 80 to the host's port 80 and 81, hence there is no direct linkage between them.
To make the containers visible to each other, first you will have to use the --link option and then do an --expose to allow the containers to see each other through the exposed port.
Example:
docker run -it -p 80:80 --name sample1 sample
docker run -it -p 81:80 --link=sample1 --expose="80" --name sample2 sample
Essentially --link means to allow the container to see the link value's container
--expose means the linked containers are able to communicate through that expose port.
Note: linking the containers is not sufficient, you need to expose ports for them to communicate.
You might want refer to the docker-compose documentation for more details;
https://docs.docker.com/compose/compose-file/
While the documentation is for docker-compose but the options are pretty much the same as the raw docker binary, and everything is nicely put on 1 page. That's why I prefer looking at there.
In Docker you can bind container's port to docker machine (Machine installed with docker) port using
docker run -it -p 80:80 image
Then you can use docker machine Ip and port inside the another container.
I'm following the following tutorial on how to start a basic nginx server in a docker container. However, the example's nginx docker container runs on localhost (0.0.0.0) as shown here:
Meanwhile, when I run it it for some reason it runs on the IP 10.0.75.2:
Is there any particular reason why this is happening? And is there any way to get it to run on localhost like in the example?
Edit: I tried using --net=host but had no results:
The default network is bridged. The 0.0.0.0:49166->443 shows a port mapping of exposed ports in the container to high level ports on your host because of the -P option. You can manually map specific ports by changing that flag to something like -p 8080:80 -p 443:443 to have port 8080 and 443 on your host map into the container.
You can also change the default network to be your host network as you've requested. This removes some of the isolation and protections provided by the container, and limits your ability to configure integrations between containers, which is why it is not the default option. That syntax would be:
docker run --name nginx1 --net=host -d nginx
Edit: from your comments and a reread I see you're also asking about where the 10.0.75.2 ip address comes from. This is based on how you launch the docker daemon. That IP binding is assigned when you pass the --ip flag to the daemon documentation here. If you're running docker in a vm with docker-machine, I'd expect this to be the IP of your vm.
A good turnaround is to set using -p flag (--publish short)
docker run -d -p 3000:80 --name <your_image_name> nginx:<version_tag>
What is the different between the following commands when creating a container in docker?
docker run -d -p 8080 sample/image
and
docker run -d -p 8080:8080 sample/image
I have seen majority of them use the second command, but I am not sure if they mean different things, or if the first is shorthand.
I couldn't find any material on this.
docker run -d -p 8080 sample/image
Exposes port 8080 of the container as an arbitrary port on the host. Which port that is is up to Docker.
Whereas,
docker run -d -p 8080:8080 sample/image
Exposes port 8080 of the container as port 8080 on the host.
In both cases, you can see the mapping using docker inspect, or even docker ps:
380af8c2bcc6 ubuntu "bash" 15 seconds ago Up 13 seconds 0.0.0.0:32768->1234/tcp elegant_meitner
In this case, port 1234 of the container is exposed as port 32768 on the host.
Is it possible to set a different host port than the container's exposed port in docker? For example docker run -name some_container -p 80:8080 -i -t some_img If so, is it -p host:container or -p container:host? I've looked through the docs and haven't found any examples of this nor details on the publish option for docker run.
Additionally, I don't want to use the same port as the container because that is where Kubernete's api-server is listening.
It is host:container and it is possible to set different port on the host.
This Link has some good examples.
Yes you can map any host port to container port unless it is being used by other application
docker run -p 80:8080 --name=centos centos:latest
it is host:container format.
I'm trying to set up some docker container demo blogs but I'm having problems when I try to access more than one:
docker run --volumes-from my-data -p 80:8080 --name site1 tutum/wordpress
docker run --volumes-from my-data -p 80:8081 --name site2 tutum/wordpress
I can access the first one from myhost:8080 but I can't access the second one from myhost:8081
Is there anything obvious I'm missing?
Yes. The -p argument tells docker how to map external addresses to internal (container) addresses. You are instructing it to map port 80 of all host interfaces to port 8080/8081 of the respective container. Assuming the container processes really listen on port 8080/8081 you might want to try -p 8080:8080 / -p8081:8081. If the containers run standard webservers on port 80, you might want to use -p 8080:80 / -p 8081:80 instead. The proper port mapping will make the container service accessible on port 8080/8081 of all host interfaces.