-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
437 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.git | ||
README.md | ||
LICENSE | ||
Makefile | ||
GLOCKFILE | ||
*.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
docker-mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
language: go | ||
go: | ||
- 1.4 | ||
install: | ||
- make deps | ||
script: | ||
- make test | ||
before_deploy: | ||
- make tool | ||
- make release | ||
deploy: | ||
provider: releases | ||
api_key: | ||
secure: i4LFQ1kyX97abC925LrvK4rVO7zzaaqsZzWBCHSjxUBunKVtTe7jc8XHHG0Mh2qJfdog+sCh57+AmZsDy3B6fw3ZSrpFJh2miHPRWWdpfjCEVqQFHzQrfVm7hfqSYg+p7n4yxyY0FCNZ7T6/fNef/W8TUdvhc/RyZvgZ9eqePJw= | ||
skip_cleanup: true | ||
file: | ||
- docker-mysql-linux-386-0.1.0.tar.gz | ||
- docker-mysql-linux-amd64-0.1.0.tar.gz | ||
- docker-mysql-linux-arm-0.1.0.tar.gz | ||
- docker-mysql-darwin-386-0.1.0.tar.gz | ||
- docker-mysql-darwin-amd64-0.1.0.tar.gz | ||
on: | ||
tags: true | ||
all_branches: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,27 @@ FROM mysql:5.7 | |
|
||
MAINTAINER nanofi <[email protected]> | ||
|
||
RUN \ | ||
apt-get update && \ | ||
apt-get install -y --no-install-recommends ca-certificates wget && \ | ||
apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* | ||
|
||
RUN \ | ||
wget -P /usr/local/bin https://godist.herokuapp.com/projects/ddollar/forego/releases/current/linux-amd64/forego && \ | ||
chmod u+x /usr/local/bin/forego | ||
|
||
ENV CONTAINER_VERSION 0.1.0 | ||
RUN \ | ||
wget https://github.com/nanofi/docker-mysql/releases/download/$CONTAINER_VERSION/docker-mysql-linux-amd64-$CONTAINER_VERSION.tar.gz && \ | ||
tar -xvzf docker-mysql-linux-amd64-$CONTAINER_VERSION.tar.gz && \ | ||
mv dist/docker-mysql-linux-amd64 /usr/local/bin/docker-mysql && \ | ||
rm -rf dist | ||
|
||
RUN usermod -u 1000 mysql | ||
COPY docker-entrypoint.sh /entrypoint.sh | ||
|
||
COPY . /app/ | ||
WORKDIR /app/ | ||
|
||
ENTRYPOINT [] | ||
CMD ["mysqld_safe", "--datadir=/var/lib/mysql", "--user=mysql"] | ||
CMD ["forego", "start", "-r"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.SILENT : | ||
.PHONY : docker-mysql clean fmt | ||
|
||
TAG:=`git describe --abbrev=0 --tags` | ||
LDFLAGS:=-X main.buildVersion $(TAG) | ||
|
||
all: docker-mysql | ||
|
||
deps: | ||
go get github.com/mitchellh/gox | ||
go get github.com/fsouza/go-dockerclient | ||
|
||
tool: | ||
gox -build-toolchain -os "darwin linux" | ||
|
||
test: | ||
go test -v | ||
|
||
docker-mysql: | ||
echo "Building docker-mysql" | ||
go build -ldflags "$(LDFLAGS)" | ||
|
||
dist-clean: | ||
rm -rf dist | ||
|
||
dist: dist-clean | ||
gox -os "darwin linux" -output "dist/{{.Dir}}-{{.OS}}-{{.Arch}}" | ||
|
||
release: dist | ||
ls dist | xargs -I {} tar -cvzf {}-$(TAG).tar.gz dist/{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mysql: /app/start.sh mysqld_safe --datadir=/var/lib/mysql --user=mysql | ||
config: docker-mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
docker-mysql | ||
============ | ||
|
||
A Dockerfile that installs a mysql server with multiple databases. | ||
[![Build Status](https://travis-ci.org/nanofi/docker-mysql.svg?branch=master)](https://travis-ci.org/nanofi/docker-mysql) | ||
|
||
A Dockerfile that installs a mysql server with multiple databases. databases and users will generate dynamically with starting or stopping or killing a container. | ||
|
||
## Formatss of Parameters | ||
|
||
- `MYSQL_DATABASES=database1,database2,...,databaseN` | ||
- `MYSQL_USERS=user1:pass1:database1/database3,user2:pass2:database2/database6,...` | ||
## Usage | ||
To run it: | ||
``` | ||
$ docker run -d -v /var/run/docker.sock:/var/run/docker.sock nanofi/mysql | ||
``` | ||
Start any containers with env vars: | ||
- `DB_NAME`; database name | ||
- `DB_USER`; username | ||
- `DB_PASS`; password | ||
|
||
If `DB_NAME` presences, the container creates a database. If `DB_NAME`, `DB_USER` and `DB_PASS` presence, the container creates an user which has all privilege to the database. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
docker "github.com/fsouza/go-dockerclient" | ||
) | ||
|
||
type DockerContainer struct { | ||
} | ||
|
||
func exists(path string) (bool, error) { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
|
||
func getEndpoint() (string, error) { | ||
defaultEndpoint := "unix:///var/run/docker.sock" | ||
if os.Getenv("DOCKER_HOST") != "" { | ||
defaultEndpoint = os.Getenv("DOCKER_HOST") | ||
} | ||
if endpoint != "" { | ||
defaultEndpoint = endpoint | ||
} | ||
proto, host, err := parseHost(defaultEndpoint) | ||
if err != nil { | ||
return "", err | ||
} | ||
if proto == "unix" { | ||
exist, err := exists(host) | ||
if err != nil { | ||
return "", err | ||
} | ||
if !exist { | ||
return "", errors.New(host + " does not exist") | ||
} | ||
} | ||
return defaultEndpoint, nil | ||
} | ||
|
||
// based off of https://github.com/dotcloud/docker/blob/2a711d16e05b69328f2636f88f8eac035477f7e4/utils/utils.go | ||
func parseHost(addr string) (string, string, error) { | ||
var ( | ||
proto string | ||
host string | ||
port int | ||
) | ||
addr = strings.TrimSpace(addr) | ||
switch { | ||
case addr == "tcp://": | ||
return "", "", fmt.Errorf("Invalid bind address format: %s", addr) | ||
case strings.HasPrefix(addr, "unix://"): | ||
proto = "unix" | ||
addr = strings.TrimPrefix(addr, "unix://") | ||
if addr == "" { | ||
addr = "/var/run/docker.sock" | ||
} | ||
case strings.HasPrefix(addr, "tcp://"): | ||
proto = "tcp" | ||
addr = strings.TrimPrefix(addr, "tcp://") | ||
case strings.HasPrefix(addr, "fd://"): | ||
return "fd", addr, nil | ||
case addr == "": | ||
proto = "unix" | ||
addr = "/var/run/docker.sock" | ||
default: | ||
if strings.Contains(addr, "://") { | ||
return "", "", fmt.Errorf("Invalid bind address protocol: %s", addr) | ||
} | ||
proto = "tcp" | ||
} | ||
|
||
if proto != "unix" && strings.Contains(addr, ":") { | ||
hostParts := strings.Split(addr, ":") | ||
if len(hostParts) != 2 { | ||
return "", "", fmt.Errorf("Invalid bind address format: %s", addr) | ||
} | ||
if hostParts[0] != "" { | ||
host = hostParts[0] | ||
} else { | ||
host = "127.0.0.1" | ||
} | ||
|
||
if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 { | ||
port = p | ||
} else { | ||
return "", "", fmt.Errorf("Invalid bind address format: %s", addr) | ||
} | ||
|
||
} else if proto == "tcp" && !strings.Contains(addr, ":") { | ||
return "", "", fmt.Errorf("Invalid bind address format: %s", addr) | ||
} else { | ||
host = addr | ||
} | ||
if proto == "unix" { | ||
return proto, host, nil | ||
|
||
} | ||
return proto, fmt.Sprintf("%s:%d", host, port), nil | ||
} | ||
|
||
func getContainers(client *docker.Client) ([]*RuntimeContainer, error) { | ||
apiContainers, err := client.ListContainers(docker.ListContainersOptions{ | ||
All: false, | ||
Size: false, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
containers := []*RuntimeContainer{} | ||
for _, apiContainer := range apiContainers { | ||
container, err := client.InspectContainer(apiContainer.ID) | ||
if err != nil { | ||
log.Printf("error inspecting container: %s: %s\n", apiContainer.ID, err) | ||
continue | ||
} | ||
runtimeContainer := &RuntimeContainer{ | ||
ID: container.ID, | ||
Name: strings.TrimLeft(container.Name, "/"), | ||
Address: container.NetworkSettings.IPAddress, | ||
Env: make(map[string]string), | ||
} | ||
for _, entry := range container.Config.Env { | ||
parts := strings.Split(entry, "=") | ||
runtimeContainer.Env[parts[0]] = parts[1] | ||
} | ||
containers = append(containers, runtimeContainer) | ||
} | ||
return containers, nil | ||
} |
Oops, something went wrong.