Vladimir Fejsov
Vladimir Fejsov

Reputation: 589

Execute command in linked docker container

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.

Upvotes: 19

Views: 9373

Answers (3)

Mohammad Golmoradi
Mohammad Golmoradi

Reputation: 41

As "Abdullah Jibaly" said you can do that but there is some security issues you have to consider, also there is sdk docker to use, and for python applications can use Docker SDK for Python

Upvotes: 0

Eduardo Cuomo
Eduardo Cuomo

Reputation: 18937

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

Upvotes: 1

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

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.

Upvotes: 5

Related Questions