SaltyCrane Blog — Notes on JavaScript and web development

How to run Docker in Docker on Mac

Docker in Docker can be used in GitLab CI/CD to build Docker images. This is how to run Docker in Docker on Mac.

  • create directory

    mkdir /tmp/my-project
    cd /tmp/my-project
    
  • create docker-compose.yml file:

    version: "3"
    services:
      docker-daemon:
        container_name: "my-docker-daemon"
        environment:
          DOCKER_TLS_CERTDIR: ""
        image: "docker:dind"
        networks:
          "my-network":
            aliases:
              - "docker"
        privileged: true
      docker-client:
        command: sh -c 'while [ 1 ]; do sleep 1000; done'
        container_name: "my-docker-client"
        depends_on:
          - "docker-daemon"
        environment:
          DOCKER_HOST: "tcp://docker:2375"
        image: "docker:latest"
        networks:
          "my-network": {}
    
    networks:
      "my-network":
        name: "my-network"
    
  • run the docker daemon and client containers

    docker-compose up -d
    
  • run a shell in the client container

    docker exec -it my-docker-client sh
    
  • run a docker command in the docker client container

    / # docker ps
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    

References

Comments