Create and run a docker container on specific network

I’m trying to create an image and run a container from it as a step in the pipeline, but I want it on specific network.

# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
d9317920b1a6   apps      bridge    local    <---- I want it here
73318eaa1b75   bridge    bridge    local
1c68cdc44f29   host      host      local
4e63843bcd0a   none      null      local

In order to do that, I have a drone/drone-runner-docker:1 container added to this network. I see it when I run docker network inspect apps. I also see every all containers that are created when I run my pipeline.

Now, my pipeline is rather simple:

kind: pipeline
type: docker
name: default

steps:
- name: build
  image: docker:dind
  volumes:
  - name: dockersock
    path: /var/run
  commands:
  - sleep 20
  - docker network ls
  # - docker build *whatever*
  # - docker run -d --net=apps *whatever*

services:
- name: docker
  image: docker:dind
  privileged: true
  volumes:
  - name: dockersock
    path: /var/run

volumes:
- name: dockersock
  temp: {}

this build step produces the following:

+ sleep 20

+ docker network ls

NETWORK ID     NAME      DRIVER    SCOPE
7ac1733c9a7b   bridge    bridge    local
5152fdb46e9e   host      host      local
507a2585be0b   none      null      local

What I actually noticed right now is that these are all names except apps but they have different IDs which means they aren’t the same as in the beginning

As I understand this problem, I don’t actually need “docker in docker” solution, I need to connect to my top-level Docker. What I need to change? Do I need to adjust volumes: section somehow to mount my main /var/run/docker.sock ?

I will post this as answer for visibility if anyone else will search for it.


So it seems that I figured it out, I need to use volume from host machine:

kind: pipeline
type: docker
name: default

steps:
- name: build
  image: docker:dind
  volumes:
  - name: dockersock
    path: /var/run
  commands:
  - docker network ls

volumes:
- name: dockersock
  host:
    path: /var/run

This way I don’t need the second services: nor don’t I need sleep at all because the Docker service I am talking to is the one that’s running the whole show

In this case, docker network ls outputs the following:

NETWORK ID     NAME                         DRIVER    SCOPE
d9317920b1a6   apps                         bridge    local
baea1019d3f5   bridge                       bridge    local
1a724af4be43   drone-4UeGXrbVI7pBv798qAze   bridge    local
1c68cdc44f29   host                         host      local
4e63843bcd0a   none                         null      local

Just what I need, and drone- network is removed after pipeline completes