Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infra implementation #169

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 40 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ The dip is a CLI dev–tool that provides native-like interaction with a Dockeri
## Presentations and examples

- [Local development with Docker containers](https://slides.com/bibendi/dip)
- Dockerized Ruby on Rails applications: [one](https://github.com/lewagon/rails-k8s-demo), [two](https://github.com/bibendi/dip-example-rails), [three](https://github.com/evilmartians/evil_chat)
- [Dockerized Ruby on Rails application](https://SberMarket-Tech/outbox-example-apps)
- Dockerized Node.js application: [one](https://github.com/bibendi/twinkle.js), [two](https://github.com/bibendi/yt-graphql-react-event-booking-api)
- [Dockerized Ruby gem](https://github.com/bibendi/schked)
- [Dockerizing Ruby and Rails development](https://evilmartians.com/chronicles/ruby-on-whales-docker-for-ruby-rails-development)
- [Reusable development containers with Docker Compose and Dip](https://evilmartians.com/chronicles/reusable-development-containers-with-docker-compose-and-dip)

[![asciicast](https://asciinema.org/a/210236.svg)](https://asciinema.org/a/210236)

## Integration with shell
## Installation

```sh
gem install dip
```

### Integration with shell

Dip can be injected into the current shell (ZSH or Bash).

Expand Down Expand Up @@ -51,14 +57,6 @@ VERSION=20180515103400 rails db:migrate:down
You could add this `eval` at the end of your `~/.zshrc`, or `~/.bashrc`, or `~/.bash_profile`.
After that, it will be automatically applied when you open your preferred terminal.

## Installation

```sh
gem install dip
```

The compiled binary is no more provided since version 7, because of new version of [Ruby Packer](https://github.com/pmq20/ruby-packer) not released for a long time with recent Ruby version. Also there was a lot of work to prepare each release of Dip for MacOS version.

## Usage

```sh
Expand Down Expand Up @@ -240,19 +238,16 @@ services:

The container will run using the same user ID as your host machine.


### dip run

Run commands defined within the `interaction` section of dip.yml

A command will be executed by specified runner. Dip has three types of them:

- `docker-compose` runner — used when the `service` option is defined.
- `docker compose` runner — used when the `service` option is defined.
- `kubectl` runner — used when the `pod` option is defined.
- `local` runner — used when the previous ones are not defined.

If you are still using `docker-compose` binary (i.e., prior to Compose V2 changes), a command would be run through it. You can disable using of Compose V2 by passing an environment variable `DIP_COMPOSE_V2=false dip run`.

```sh
dip run rails c
dip run rake db:migrate
Expand Down Expand Up @@ -304,14 +299,43 @@ Run commands each by each from `provision` section of dip.yml

### dip compose

Run docker-compose commands that are configured according to the application's dip.yml:
Run Docker Compose commands that are configured according to the application's dip.yml:

```sh
dip compose COMMAND [OPTIONS]

dip compose up -d redis
```

### dip infra

Runs shared Docker Compose services that are used by the current application. Useful for microservices.

There are several official infrastructure services available:
- [dip-postgres](https://github.com/bibendi/dip-postgres)
- [dip-kafka](https://github.com/bibendi/dip-kafka)
- [dip-nginx](https://github.com/bibendi/dip-nginx)

```yaml
# dip.yml
infra:
foo:
git: https://github.com/owner/foo.git
ref: latest # default, optional
bar:
path: ~/path/to/bar
```

Repositories will be pulled to a `~/.dip/infra` folder. For example, for the `foo` service it would be like this: `~/.dip/infra/foo/latest` and clonned with the following command: `git clone -b <ref> --single-branch <git> --depth 1`.

Available CLI commands:

- `dip infra update` pulls updates from sources
- `dip infra up` starts all infra services
- `dip infra up -n kafka` starts a specific infra service
- `dip infra down` stops all infra services
- `dip infra down -n kafka` stops a specific infra service

### dip ktl

Run kubectl commands that are configured according to the application's dip.yml:
Expand Down Expand Up @@ -356,88 +380,10 @@ dip ssh up -u 1000

This especially helpful if you have something like this in your docker-compose.yml:

```
```yml
services:
web:
user: "1000:1000"

```

### dip nginx

Runs Nginx server container based on [nginxproxy/nginx-proxy](https://github.com/nginx-proxy/nginx-proxy) image. An application's docker-compose.yml should contain environment variable `VIRTUAL_HOST` and `VIRTUAL_PATH` and connects to external network `frontend`.

foo-project/docker-compose.yml

```yml
services:
foo-web:
image: company/foo_image
environment:
- VIRTUAL_HOST=*.bar-app.docker
- VIRTUAL_PATH=/
networks:
- default
- frontend
dns: $DIP_DNS

networks:
frontend:
external:
name: frontend
```

baz-project/docker-compose.yml

```yml
services:
baz-web:
image: company/baz_image
environment:
- VIRTUAL_HOST=*.bar-app.docker
- VIRTUAL_PATH=/api/v1/baz_service,/api/v2/baz_service
networks:
- default
- frontend
dns: $DIP_DNS

networks:
frontend:
external:
name: frontend
```

```sh
dip nginx up
cd foo-project && dip compose up
cd baz-project && dip compose up
curl www.bar-app.docker/api/v1/quz
curl www.bar-app.docker/api/v1/baz_service/qzz
```

#### Pass SSL certificates

```sh
dip nginx up -c $HOME/ssl_certificates
```

#### Publish more than one port to localhost

Just pass a list, separated by a space:

```sh
dip nginx up -p 80:80 443:443
```

### dip dns

Runs a DNS server container based on https://github.com/aacebedo/dnsdock. It is 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
```

## Changelog
Expand Down
5 changes: 3 additions & 2 deletions dip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Gem::Specification.new do |spec|
spec.authors = ["bibendi"]
spec.email = ["[email protected]"]

spec.summary = "Ruby gem CLI tool for better interacting docker-compose files."
spec.summary = "Ruby gem CLI tool for better interacting Docker Compose files."
spec.description = "DIP - Docker Interaction Process." \
"CLI tool for better development experience when interacting with docker and docker-compose."
"CLI tool for better development experience when interacting with docker and Docker Compose."
spec.homepage = "https://github.com/bibendi/dip"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
Expand Down Expand Up @@ -45,4 +45,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rubocop-rspec", "~> 2.2"
spec.add_development_dependency "simplecov", "~> 0.16"
spec.add_development_dependency "test-unit", "~> 3"
spec.add_development_dependency "fakefs"
end
28 changes: 0 additions & 28 deletions docs/docker-for-mac-install.md

This file was deleted.

17 changes: 0 additions & 17 deletions docs/docker-ubuntu-install.md

This file was deleted.

4 changes: 4 additions & 0 deletions lib/dip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def bin_path
$PROGRAM_NAME.start_with?("./") ? File.expand_path($PROGRAM_NAME) : "dip"
end

def home_path
@home_path ||= File.expand_path(ENV.fetch("DIP_HOME", "~/.dip"))
end

%w[test debug].each do |key|
define_method("#{key}?") do
ENV["DIP_ENV"] == key
Expand Down
24 changes: 10 additions & 14 deletions lib/dip/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Dip
class CLI < Thor
TOP_LEVEL_COMMANDS = %w[help version ls compose up stop down run provision ssh dns nginx console].freeze
TOP_LEVEL_COMMANDS = %w[help version ls compose up stop down run provision ssh infra console].freeze

class << self
# Hackery. Take the run method away from Thor so that we can redefine it.
Expand Down Expand Up @@ -47,30 +47,30 @@ def ls
Dip::Commands::List.new.execute
end

desc "compose CMD [OPTIONS]", "Run docker-compose commands"
desc "compose CMD [OPTIONS]", "Run Docker Compose commands"
def compose(*argv)
require_relative "commands/compose"
Dip::Commands::Compose.new(*argv).execute
end

desc "build [OPTIONS] SERVICE", "Run `docker-compose build` command"
desc "build [OPTIONS] SERVICE", "Run `docker compose build` command"
def build(*argv)
compose("build", *argv)
end

desc "up [OPTIONS] SERVICE", "Run `docker-compose up` command"
desc "up [OPTIONS] SERVICE", "Run `docker compose up` command"
def up(*argv)
compose("up", *argv)
end

desc "stop [OPTIONS] SERVICE", "Run `docker-compose stop` command"
desc "stop [OPTIONS] SERVICE", "Run `docker compose stop` command"
def stop(*argv)
compose("stop", *argv)
end

desc "down [OPTIONS]", "Run `docker-compose down` command"
desc "down [OPTIONS]", "Run `docker compose down` command"
method_option :help, aliases: "-h", type: :boolean, desc: "Display usage information"
method_option :all, aliases: "-A", type: :boolean, desc: "Shutdown all running docker-compose projects"
method_option :all, aliases: "-A", type: :boolean, desc: "Shutdown all running Docker Compose projects"
def down(*argv)
if options[:help]
invoke :help, ["down"]
Expand Down Expand Up @@ -121,13 +121,9 @@ def provision
desc "ssh", "ssh-agent container commands"
subcommand :ssh, Dip::CLI::SSH

require_relative "cli/dns"
desc "dns", "DNS server for automatic docker container discovery"
subcommand :dns, Dip::CLI::DNS

require_relative "cli/nginx"
desc "nginx", "Nginx reverse proxy server"
subcommand :nginx, Dip::CLI::Nginx
require_relative "cli/infra"
desc "infra", "Infrastructure services"
subcommand :infra, Dip::CLI::Infra

require_relative "cli/console"
desc "console", "Integrate Dip commands into shell (only ZSH and Bash are supported)"
Expand Down
Loading
Loading