Skip to content

Commit

Permalink
Add configuration to expliictly bind to a static host db port, fixes d…
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay committed Mar 25, 2019
1 parent e8b32c5 commit 09812ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
18 changes: 18 additions & 0 deletions pkg/ddevapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/drud/ddev/pkg/globalconfig"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/drud/ddev/pkg/output"
"github.com/drud/ddev/pkg/util"
"github.com/drud/ddev/pkg/version"
"github.com/phayes/freeport"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -76,6 +78,12 @@ func NewApp(AppRoot string, provider string) (*DdevApp, error) {
app.RouterHTTPSPort = DdevDefaultRouterHTTPSPort
app.MariaDBVersion = version.MariaDBDefaultVersion

dbPort, err := freeport.GetFreePort()
if err != nil {
return app, fmt.Errorf("Failed to find a free port for ddev-dbserver container binding: %v", err)
}
app.HostDBPort = strconv.Itoa(dbPort)

// These should always default to the latest image/tag names from the Version package.
app.WebImage = version.GetWebImage()
app.DBImage = version.GetDBImage(app.MariaDBVersion)
Expand Down Expand Up @@ -142,6 +150,16 @@ func (app *DdevApp) WriteConfig() error {
appcopy.BgsyncImage = ""
}

// We now want to reserve the port we're writing for DBport so it doesn't
// accidentally get used for other projects.
if !util.ArrayContainsString(globalconfig.DdevGlobalConfig.UsedHostDBPorts, app.HostDBPort) {
globalconfig.DdevGlobalConfig.UsedHostDBPorts = append(globalconfig.DdevGlobalConfig.UsedHostDBPorts, app.HostDBPort)
err := globalconfig.WriteGlobalConfig(globalconfig.DdevGlobalConfig)
if err != nil {
return err
}
}

// Don't write default working dir values to config
defaults := appcopy.DefaultWorkingDirMap()
for service, defaultWorkingDir := range defaults {
Expand Down
2 changes: 2 additions & 0 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type DdevApp struct {
UploadDir string `yaml:"upload_dir,omitempty"`
WorkingDir map[string]string `yaml:"working_dir,omitempty"`
OmitContainers []string `yaml:"omit_containers,omitempty"`
HostDBPort string `yaml:"host_db_port"`
}

// GetType returns the application type as a (lowercase) string
Expand Down Expand Up @@ -917,6 +918,7 @@ func (app *DdevApp) DockerEnv() {
"DDEV_WEBIMAGE": app.WebImage,
"DDEV_BGSYNCIMAGE": app.BgsyncImage,
"DDEV_APPROOT": app.AppRoot,
"DDEV_HOST_DB_PORT": app.HostDBPort,
"DDEV_DOCROOT": app.Docroot,
"DDEV_URL": app.GetHTTPURL(),
"DDEV_HOSTNAME": app.HostName(),
Expand Down
7 changes: 6 additions & 1 deletion pkg/ddevapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
restart: "no"
user: "$DDEV_UID:$DDEV_GID"
ports:
- "3306"
- "127.0.0.1:$DDEV_HOST_DB_PORT:3306"
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.platform: {{ .Plugin }}
Expand Down Expand Up @@ -242,6 +242,11 @@ const ConfigInstructions = `
# Note that these containers can also be omitted globally in the
# ~/.ddev/global_config.yaml or with the "ddev config global" command.
# host_db_port: "6667"
# The host port binding for the ddev-dbserver can be explicitly specified. It's normally static
# after first configuration
# provider: default # Currently either "default" or "pantheon"
#
Expand Down
1 change: 1 addition & 0 deletions pkg/globalconfig/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type GlobalConfig struct {
OmitContainers []string `yaml:"omit_containers"`
InstrumentationOptIn bool `yaml:"instrumentation_opt_in"`
LastUsedVersion string `yaml:"last_used_version"`
UsedHostDBPorts []string `yaml:"used_host_db_ports"`
DeveloperMode bool `yaml:"developer_mode,omitempty"`
}

Expand Down

0 comments on commit 09812ca

Please sign in to comment.