Docker Community Forums

Share and learn in the Docker community.

Container to container communication

docker

(Marcaw) #1

Hi there,

i am searching since two days for a in my opinion very simple problem.
I want to execute a ls from one container on an other one.
They both run on a Docker for Windows .
I have a docker-compose.yml:

C1:
  image: debian
  container_name: c1
  networks:
    showcase:
      ipv4_address: 172.20.0.5

C2:
  image: debian
  container_name: c2
  networks:
    showcase:
      ipv4_address: 172.20.0.6  

I can access c1

docker exec -it c1 /bin/bash 

in the c1 container i can do a ping c2 that is not the problem but i want to do something like ls c2 or a mkdir on c2 from container c1


(David Maze) #2

You can’t do that. The containers are isolated from each other; they have separate filesystems and process spaces, among other things. That’s one of the key goals of Docker.


(Marcaw) #3

is there any “easy” option doing that via SSH or whatever?


(Christian Kniep) #4

In case you do not need to access a directory, which is part of the OS, you could use a common volume to access the same filesystem.

version: '3.2'
volumes:
  mydata:
services:
  c1:
    image: debian
    command: tail -f /dev/null
    volumes:
      - type: volume
        source: mydata
        target: /data

  c2:
    image: debian
    command: tail -f /dev/null
    volumes:
      - type: volume
        source: mydata
        target: /data

Start the services.

$ docker stack deploy -c docker-compose.yml test
Creating network test_default
Creating service test_c1
Creating service test_c2

Now you can access /data/ from both containers.

$ docker exec -ti test_c2.1.8zfvzp9dkadgsxe4ugvrwnomv touch /data/test
$ docker exec -ti test_c1.1.bxal1ljjfd5918d1mcukuy5s4 ls /data/
test

I would advise against sshd in containers. running ssh just to execute processes?
You could potentially map the /var/run/docker.sock into the container so that they can use the DockerAPI to execute processes; but that is also risky, as this provides all the docker power to the container.


(Marcaw) #5

Thank you that is what I was looking for :slight_smile: