Search code examples
dockernetwork-programmingdocker-composedocker-machine

Running Docker from a Docker container


Doing a project for my dissertation which involves an angular frontend which talks to a node API backend which needs to use docker to create containers from images run a few lines of code for the console output.

I have used docker-compose to create two docker containers for my frontend & backend.

my backend (api) container needs a way to connect to docker using dockerode and I'm not sure how to do this.

version: "3"
services:
  app:
    build: ./frontend
    ports:
      - "3001:4200"
    volumes:
      - ./frontend:/code
    depends_on:
      - api
  api:
    build: ./backend
    ports:
      - "3000:8080"
    volumes:
      - ./backend:/code

I have tried :

1 using docker-machine on my host (windows 10) but whenever I try to connect from the docker container (backend) via dockerode 'ReferenceError: docker is not defined' but am unsure if docker-machine is running correctly on my host as I'm on windows 10 using hyper-v.

const docker = new Docker({host: 'http://192.168.0.23', port: 3000});

2 installing docker into the docker container which has failed every time and when successfully installed it won't recognize 'docker' as running on the system.

3 adding the docker image to the docker-compose file as I thought could that work? for me, no but don't know if that's because of the networking of not the intended use for the docker image

  docker:
   image: docker
   ports: 
    - "2376:2376"
   stdin_open: true
   tty: true
   volumes:
    - /var/run/docker.sock:/var/run/docker.sock

I am fairly new to docker especially the networking side and have been looking at things like docker-machine, docker-swarm & Kubernetes to find the best solution.

any insight into this would be greatly appreciated as the biggest hurdle for this project is getting my backend API to talk with docker in a deployed environment.


Solution

  • I don't know if this will work for everyone but adding dind to my project worked for what I need.

    docker-in-docker:
      image: docker:dind
      environment:
        DOCKER_TLS_CERTDIR: ""
      privileged: true
      volumes:
        - /var/lib/docker
      expose:
        - 2375
        - 2376
    

    and for any container in docker-compose that will connect to "docker-in-docker" needs this in their service. the "DOCKER_TLS_CERTDIR" is to remove TLS

      links:
        - docker-in-docker
      environment:
        DOCKER_HOST: http://docker-in-docker:2375
        DOCKER_TLS_CERTDIR: ""
    

    another note for anyone using dockerode, when using async & await just on my local was fine but I would always get docker is undefined once moving it to a docker environment which can be fixed by using .then instead.