@@ -3,8 +3,10 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "github.com/docker/go-connections/nat"
6
7
"os"
7
8
"path/filepath"
9
+ "runtime"
8
10
"strings"
9
11
"time"
10
12
@@ -67,15 +69,24 @@ func (r *runner) runContainer(image string) error {
67
69
return err
68
70
}
69
71
72
+ containerAddr := "localhost"
73
+ containerPort := r .port
74
+ if runtime .GOOS == "darwin" {
75
+ // See justification below.
76
+ containerPort = "8443"
77
+ containerAddr = "0.0.0.0"
78
+ }
79
+
70
80
// We want the code-server logs to be available inside the container for easy
71
81
// access during development, but also going to stdout so `docker logs` can be used
72
82
// to debug a failed code-server startup.
73
- cmd := "cd " + projectDir +
74
- "; code-server --host 127.0.0.1" +
75
- " --port " + r .port +
76
- " --data-dir ~/.config/Code --extensions-dir ~/.vscode/extensions --allow-http --no-auth 2>&1 | tee " + containerLogPath
83
+ cmd := fmt .Sprintf (`set -euxo pipefail || exit 1
84
+ cd %v
85
+ code-server --host %v --port %v \
86
+ --data-dir ~/.config/Code --extensions-dir ~/.vscode/extensions --allow-http --no-auth 2>&1 | tee %v
87
+ ` , projectDir , containerAddr , containerPort , containerLogPath )
77
88
if r .testCmd != "" {
78
- cmd = r .testCmd + "; exit 1"
89
+ cmd = r .testCmd + "\n exit 1"
79
90
}
80
91
81
92
var envs []string
@@ -98,7 +109,10 @@ func (r *runner) runContainer(image string) error {
98
109
projectLocalDirLabel : r .projectLocalDir ,
99
110
projectNameLabel : r .projectName ,
100
111
},
101
- User : r .hostUser + ":user" ,
112
+ // The user inside has uid 1000. This works even on macOS where the default user has uid 501.
113
+ // See https://stackoverflow.com/questions/43097341/docker-on-macosx-does-not-translate-file-ownership-correctly-in-volumes
114
+ // The docker image runs it as uid 1000 so we don't need to set anything.
115
+ User : "" ,
102
116
}
103
117
104
118
err = r .addImageDefinedLabels (image , containerConfig .Labels )
@@ -123,6 +137,19 @@ func (r *runner) runContainer(image string) error {
123
137
},
124
138
}
125
139
140
+ // macOS does not support host networking.
141
+ // See https://github.com/docker/for-mac/issues/2716
142
+ if runtime .GOOS == "darwin" {
143
+ portSpec := fmt .Sprintf ("127.0.0.1:%v:%v/tcp" , r .port , "8443" )
144
+ hostConfig .NetworkMode = ""
145
+ exposed , bindings , err := nat .ParsePortSpecs ([]string {portSpec })
146
+ if err != nil {
147
+ return xerrors .Errorf ("failed to parse port spec: %w" , err )
148
+ }
149
+ containerConfig .ExposedPorts = exposed
150
+ hostConfig .PortBindings = bindings
151
+ }
152
+
126
153
_ , err = cli .ContainerCreate (ctx , containerConfig , hostConfig , nil , r .cntName )
127
154
if err != nil {
128
155
return xerrors .Errorf ("failed to create container: %w" , err )
0 commit comments