Skip to content

Commit 6d3cdb3

Browse files
committed
Forward MySQL port when Docker host is remote
1 parent 43d3426 commit 6d3cdb3

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

internal/services/mysql/docker.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"time"
1212
)
1313

14+
const PORT = "3306"
15+
1416
type dockerCreator struct {
1517
*rootConnection
1618
logger *zap.Logger
@@ -38,13 +40,14 @@ func NewDockerCreator(logger *zap.Logger, dockerClient *client.Client, container
3840
panic(err)
3941
}
4042

43+
port := utils.NewPortDecision(dockerClient, PORT)
44+
4145
rootPassword := utils.RandomString(16)
4246
cont, err := dockerClient.ContainerCreate(context.Background(), &container.Config{
43-
ExposedPorts: nil,
44-
Env: []string{"MYSQL_ROOT_PASSWORD=" + rootPassword},
45-
Cmd: nil,
46-
Image: dockerImage,
47-
}, nil, &network.NetworkingConfig{
47+
Env: []string{"MYSQL_ROOT_PASSWORD=" + rootPassword},
48+
Cmd: nil,
49+
Image: dockerImage,
50+
}, &container.HostConfig{PortBindings: port.Map()}, &network.NetworkingConfig{
4851
EndpointsConfig: map[string]*network.EndpointSettings{
4952
networkName: {
5053
Aliases: []string{"mysql"},
@@ -73,14 +76,14 @@ func NewDockerCreator(logger *zap.Logger, dockerClient *client.Client, container
7376
}
7477
logger.Debug("started mysql container")
7578

76-
containerAddress := utils.MustString(utils.DockerContainerAddress(context.Background(), dockerClient, cont.ID))
77-
7879
d := &dockerCreator{
79-
rootConnection: newRootConnection(containerAddress, "3306", "root", rootPassword),
80-
logger: logger,
81-
client: dockerClient,
82-
containerId: cont.ID,
83-
containerName: containerName,
80+
rootConnection: newRootConnection(
81+
port.Address(context.Background(), dockerClient, cont.ID), port.Port(), "root", rootPassword,
82+
),
83+
logger: logger,
84+
client: dockerClient,
85+
containerId: cont.ID,
86+
containerName: containerName,
8487
}
8588

8689
for attempt := 1; ; attempt++ {

0 commit comments

Comments
 (0)