Two docker's container see each others in the same machine

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:

  • error: repository [dockerimage] not found: does not exist or no pull access-docker
  • Weird behaviour of boto inside docker
  • Can't run a docker container on kubernetes in the interactive mode
  • Making REST calls to flask service running on different port
  • API HOST name from env variable node js
  • How to create Postgres backups from docker container
  •  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.

  • ENTRYPOINT & CMD commands with mongod results in unknown option error
  • Sorl4 failing to index when using EBS volume in ECS
  • linux machine using on docker for windows
  • How do I connect a Docker container running in boot2docker to a network service running on another host?
  • Docker stop exiting with 0
  • Not able to access shared data volume
  • 2 Solutions collect form web for “Two docker's container see each others in the same machine”

    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.


    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;

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.