Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute command in linked docker container

Tags:

docker

Is there any way posible to exec command from inside one docker container in the linked docker container? I don't want to exec command from the host.

like image 540
Vladimir Fejsov Avatar asked Apr 27 '15 23:04

Vladimir Fejsov


2 Answers

As long as you have access to something like the docker socket within your container, you can run any command inside any docker container, doesn't matter whether or not it is linked. For example:

# run a container and link it to `other`
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock \
           --link other:other myimage bash -l
bash$ docker exec --it other echo hello

This works even if the link was not specified.

like image 135
Abdullah Jibaly Avatar answered Oct 05 '22 09:10

Abdullah Jibaly


With docker-compose:

version: '2.1'

services:

  site:
    image: ubuntu
    container_name: test-site
    command: sleep 999999

  dkr:
    image: docker
    privileged: true
    working_dir: "/dkr"
    volumes:
      - ".:/dkr"
      - "/var/run/docker.sock:/var/run/docker.sock"
    command: docker ps -a

Then try:

docker-compose up -d site
docker-compose up dkr

result:

Attaching to tmp_dkr_1
dkr_1   | CONTAINER ID        IMAGE                             COMMAND                  CREATED                  STATUS                   PORTS                     NAMES
dkr_1   | 25e382142b2e        docker                            "docker-entrypoint..."   Less than a second ago   Up Less than a second                              tmp_dkr_1

Example Project

https://github.com/reduardo7/docker-container-access

like image 32
Eduardo Cuomo Avatar answered Oct 05 '22 07:10

Eduardo Cuomo