Search code examples
dockerdocker-composedocker-stack

Adding Docker commands into my Docker container


I have an issue that people have gone over, but not to this extent.

I am making a Docker Swarm Health checker that regularly posts the health of the Docker nodes in my environment. I was running into the issue of how to run my Docker commands from within the container. I found a post where you can run this command:

docker run -it -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker image_name bash

And it would work like how I wanted. The problem is that I want this in a container that was run from a file in a stack. I have tried every combination of volume mounts that there is. This was what I did most recently and it still didn't work. Am I missing an obvious step to get Docker into my container?

service_name:
  image: imaged_name
  volumes:
    - sock-docker:/var/run/docker.sock
    - bin-docker:/usr/bin/docker
  deploy:
    placement:
      constraints:
        - node.role == manager
    restart_policy:
      condition: any
      delay: 10s
      max_attempts: 50
      window: 600s
volumes:
  sock-docker:
    external:
      name: /var/run/docker.sock
  bin-docker:
    external:
      name: usr/bin/docker

Solution

  • Some thoughts:

    1. I do not know about mounting v /usr/bin/docker:/usr/bin/docker as you do. It is obviously working for you, but my habit is to use https://hub.docker.com/_/docker/. I suspect my approach is better, but I don't really know. Regardless, I will not comment further on that aspect of your question.
    2. I think you need to replace - sock-docker:/var/run/docker.sock with - /var/run/docker.sock:/var/run/docker.sock in your YAML file. Does not the top level volume in the YAML file refer to volume mounts and not bind mounts? (I don't know.)
    3. swarm suggests to me distributed. The Docker socket running on a node may/may not be the same as the one serving the swarm. You can bypass this issue by using ENV DOCKER_HOST=tcp://.... The DOCKER_HOST can be on another machine.