Skip to content

Commit

Permalink
Merge pull request #35 from bibendi/edge
Browse files Browse the repository at this point in the history
 Run dnsdock on random ip and assign DIP_DNS env variable
  • Loading branch information
bibendi authored Apr 11, 2018
2 parents 36a4682 + 81a7a6a commit 567679a
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 11 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ services:
networks:
- default
- frontend
dns: $DIP_DNS
networks:
frontend:
Expand All @@ -190,6 +191,7 @@ services:
networks:
- default
- frontend
dns: $DIP_DNS
networks:
frontend:
Expand All @@ -204,3 +206,14 @@ networks:
curl www.bar-app.docker/api/v1/quz
curl www.bar-app.docker/api/v1/baz_service/qzz
```

### dip dns

Runs DNS server container based on https://github.com/aacebedo/dnsdock It used for container to container requests through nginx. An application's docker-compose.yml should define `dns` configuration with environment variable `$DIP_DNS` and connect to external network `frontend`. `$DIP_DNS` will be automatically assigned by dip.

```sh
dip dns up
cd foo-project
dip compose exec foo-web curl http://www.bar-app.docker/api/v1/baz_service
```
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dip
version: 1.1.0
version: 2.0.0

crystal: 0.23.1

Expand Down
18 changes: 16 additions & 2 deletions spec/dip/commands/dns_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ describe Dip::Cli::Commands::Dns do
--detach \
--volume /var/run/docker.sock:/var/run/docker.sock:ro \
--restart always \
--publish 172.17.0.1:53:53/udp \
--publish 53/udp \
--net frontend \
--name=dnsdock aacebedo/dnsdock:latest-amd64")
end
end

it "starts dns container with custom ip and socket" do
Dip::Cli::Commands::Dns.run(%w(up --ip 8.8.8.8 -s /var/tmp/tmp.sock)) do |cmd|
Dip::Cli::Commands::Dns.run(%w(up --publish 8.8.8.8:53:53/udp -s /var/tmp/tmp.sock)) do |cmd|
cmd.out.gets_to_end.should contain("docker run \
--detach \
--volume /var/tmp/tmp.sock:/var/run/docker.sock:ro \
--restart always \
--publish 8.8.8.8:53:53/udp \
--net frontend \
--name=dnsdock aacebedo/dnsdock:latest-amd64")
end
end
Expand All @@ -36,5 +38,17 @@ describe Dip::Cli::Commands::Dns do
end
end
end

describe "#ip" do
it "get running ip of dns container" do
Dip::Cli::Commands::Dns.run(%w(ip)) do |cmd|
output = cmd.out.gets_to_end

output.should contain("docker inspect" \
" --format '{{ .NetworkSettings.Networks.frontend.IPAddress }}'" \
" dnsdock 2>/dev/null")
end
end
end
end
end
6 changes: 4 additions & 2 deletions spec/dip/commands/nginx_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ describe Dip::Cli::Commands::Nginx do
--detach \
--volume /var/run/docker.sock:/tmp/docker.sock:ro \
--restart always \
--publish 0.0.0.0:80:80 \
--publish 80:80 \
--net frontend \
--name=nginx abakpress/nginx-proxy:latest")
--name=nginx \
--label com.dnsdock.alias=docker \
abakpress/nginx-proxy:latest")
end
end
end
Expand Down
21 changes: 19 additions & 2 deletions src/dip/commands/dns.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Dip::Cli::Commands
class Dns < ::Cli::Supercommand
command "up"
command "down"
command "ip"

class Options
help
Expand All @@ -17,7 +18,8 @@ module Dip::Cli::Commands
class Up < ::Dip::Command
class Options
string %w(-s --socket), var: "PATH", default: "/var/run/docker.sock"
string %w(--ip), var: "IP", default: "172.17.0.1"
string %w(--net), var: "NET", default: "frontend"
string %w(--publish), var: "PUBLISH", default: "53/udp"
string %w(--name), var: "NAME", default: "dnsdock"
string %w(--image), var: "IMAGE", default: "aacebedo/dnsdock:latest-amd64"
string %w(--domain), var: "DOMAIN", default: "docker"
Expand All @@ -33,7 +35,8 @@ module Dip::Cli::Commands
result << "--detach"
result << "--volume #{options.socket}:/var/run/docker.sock:ro"
result << "--restart always"
result << "--publish #{options.ip}:53:53/udp"
result << "--publish #{options.publish}"
result << "--net #{options.net}"
result << "--name=#{options.name}"
result.join(' ')
end
Expand All @@ -50,6 +53,20 @@ module Dip::Cli::Commands
exec_cmd("docker rm -v #{options.name}")
end
end

class Ip < ::Dip::Command
class Options
string %w(--name), var: "NAME", default: "dnsdock"
string %w(--net), var: "NET", default: "frontend"
help
end

def run
exec_cmd("docker inspect" \
" --format '{{ .NetworkSettings.Networks.#{options.net}.IPAddress }}'" \
" #{options.name} 2>/dev/null")
end
end
end
end
end
7 changes: 4 additions & 3 deletions src/dip/commands/nginx.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ module Dip::Cli::Commands
class Options
string %w(-s --socket), var: "PATH", default: "/var/run/docker.sock"
string %w(--net), var: "NET", default: "frontend"
string %w(--ip), var: "IP", default: "0.0.0.0"
string %w(-p --port), var: "PORT", default: "80"
string %w(--publish), var: "PUBLISH", default: "80:80"
string %w(--name), var: "NAME", default: "nginx"
string %w(--image), var: "IMAGE", default: "abakpress/nginx-proxy:latest"
string %w(--domain), var: "DOMAIN", default: "docker"
help
end

Expand All @@ -35,9 +35,10 @@ module Dip::Cli::Commands
result << "--detach"
result << "--volume #{options.socket}:/tmp/docker.sock:ro"
result << "--restart always"
result << "--publish #{options.ip}:#{options.port}:80"
result << "--publish #{options.publish}"
result << "--net #{options.net}"
result << "--name=#{options.name}"
result << "--label com.dnsdock.alias=#{options.domain}"
result.join(' ')
end
end
Expand Down
19 changes: 19 additions & 0 deletions src/dip/environment.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module Dip
default_vars.each do |key, value|
@vars[key] = ENV.fetch(key) { replace(value) }
end

unless Dip.test?
@vars["DIP_DNS"] = ENV.fetch("DIP_DNS") { find_dns }
end
end

def merge!(new_vars : Hash(String, String))
Expand All @@ -26,5 +30,20 @@ module Dip

result
end

private def find_dns
dns_net = ENV.fetch("FRONTEND_NETWORK") { "frontend" }
dns_name = ENV.fetch("DNSDOCK_CONTAINER") { "dnsdock" }
cmd = "docker inspect" \
" --format '{{ .NetworkSettings.Networks.#{dns_net}.IPAddress }}'" \
" #{dns_name} 2>/dev/null"
ip = `#{cmd}`.strip

if ip.empty?
"8.8.8.8"
else
ip
end
end
end
end
2 changes: 1 addition & 1 deletion src/dip/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Dip
VERSION = "1.1.0"
VERSION = "2.0.0"
end

0 comments on commit 567679a

Please sign in to comment.