Skip to content

Commit 8a9299a

Browse files
authored
Merge pull request #1 from sourcelair/feature-better-link-dind
Add better linking between DinD and operator namespaces
2 parents c8d6495 + c93ed6e commit 8a9299a

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

docker-compose.override.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: '3.7'
2+
3+
services:
4+
dind:
5+
build: ./

docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '3.7'
2+
3+
services:
4+
dind:
5+
image: sourcelair/swarm-dind-operator:${TAG:-local}
6+
command:
7+
- /usr/bin/swarm-operator-dind
8+
- --image=${DIND_IMAGE:-docker:18.09-dind}
9+
- --args=${DIND_ARGS}
10+
- --stop-timeout=${DIND_STOP_TIMEOUT:-10}
11+
- --binds=${DIND_BINDS}
12+
volumes:
13+
- /var/run/docker.sock:/var/run/docker.sock
14+
init: true
15+
16+
networks:
17+
default:
18+
name: ${NETWORK:-dind}
19+
attachable: true
20+
external: true

main.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io/ioutil"
67
"os"
78
"os/signal"
9+
"strings"
810
"syscall"
911

1012
docker "github.com/fsouza/go-dockerclient"
@@ -31,12 +33,27 @@ func runOperator() int {
3133
pflag.StringArrayVar(&containerBinds, "binds", []string{}, "the directories to bind in the container")
3234
pflag.Parse()
3335

36+
filteredBinds := make([]string, 0, len(containerBinds))
37+
for _, bind := range containerBinds {
38+
if bind != "" {
39+
filteredBinds = append(filteredBinds, bind)
40+
}
41+
}
42+
3443
args, err := shellquote.Split(containerArgs)
3544
if err != nil {
3645
fmt.Println(errors.Wrap(err, "invalid arguments given"))
3746
return 42
3847
}
3948

49+
data, err := ioutil.ReadFile("/proc/self/cpuset")
50+
if err != nil {
51+
fmt.Println(errors.Wrap(err, "cannot read cgroup file"))
52+
return 42
53+
}
54+
parts := strings.Split(string(data), "/")
55+
selfID := strings.TrimSpace(parts[len(parts)-1])
56+
4057
client, err := docker.NewClientFromEnv()
4158
if err != nil {
4259
fmt.Println(errors.Wrap(err, "cannot create client"))
@@ -78,18 +95,19 @@ func runOperator() int {
7895
container, err := client.CreateContainer(docker.CreateContainerOptions{
7996
Name: containerName,
8097
Config: &docker.Config{
81-
Cmd: args,
82-
Hostname: containerName,
83-
Image: imageName,
98+
Cmd: args,
99+
Image: imageName,
84100
Labels: map[string]string{
85101
"com.sourcelair.swarm-dind-operator": "true",
86102
"com.sourcelair.swarm-dind-operator.name": containerName,
87103
},
88104
StopTimeout: containerStopTimeout,
89105
},
90106
HostConfig: &docker.HostConfig{
91-
Privileged: true,
92-
Binds: containerBinds,
107+
Privileged: true,
108+
Binds: filteredBinds,
109+
NetworkMode: fmt.Sprintf("container:%s", selfID),
110+
PidMode: fmt.Sprintf("container:%s", selfID),
93111
},
94112
})
95113
if err != nil {

0 commit comments

Comments
 (0)