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:

  • Setup notification for private docker registry
  • Migrating dockerized redis to another server
  • Rebuilding docker image is failing although using the same Dockerfile as shown in the hub
  • Babelrc file in Docker builds
  • Unable to access a file copied in a container
  • How to setup sentry with docker
  •  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.

  • {“message”:“No such image: jenkins_slave_1:latest”}
  • Running linux container on docker windows
  • Docker when deployed on marathon is failing continously
  • Is there a way to watch the change of service`tasks in docker swarm mode?
  • Docker: communication between web container and php container
  • Can 'docker-compose down' be somehow prevented from running in production?
  • 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.