{%hackmd BJqmTNgxD %}
## Rails on Docker
#### PastLeo @ 5xRuby
---
### What is docker?
![docker](http://i.imgur.com/afEjiqM.png)
---
## Virtualization
#### 分離環境,保持本機的乾淨
---
## Lightweight
#### Containers running on a single machine share the same operating system kernel
#### 磁碟上/啟動上都很輕快
---
## Portability
#### 外送到府,取貨免付款
---
![docker-in-vm](http://i.imgur.com/JpuQ4Qr.png)
### Installation
---
### Installation
#### [官方網站](https://www.docker.com/)
* For Mac or Windows
* [docker native](https://www.docker.com/products/docker)
* [docker toolbox](https://www.docker.com/products/docker-toolbox)
* Linux
* [https://get.docker.com](https://get.docker.com)
---
![hello-world](http://i.imgur.com/7YISj2P.jpg)
### Hello Docker!
```
docker run hello-world
```
---
![cloud2image2container](http://i.imgur.com/EzKrUy9.png)
### What the hell just happened?
---
![ruby](http://i.imgur.com/XjopV7U.png)
### 外帶 ruby
```
docker pull ruby
```
---
## 驗貨一下
```
docker run -it ruby
```
---
## 我要不同版本的
### [Dockerhub](https://hub.docker.com/)
### [Ruby on dockerhub](https://hub.docker.com/_/ruby/)
---
### docker run 參數
```
docker run [options] image[:tag] [command]
```
* `-it` : interactive mode
* `-v host_path:container_path` : sharing volume
* `-p host_port:container_port` : exposing ports
* `docker run --help` to know more...
---
## Let's Bootstrap a rails project
#### 練習看看
---
## Useful docker commands
#### `ps [-l|-a]`, `kill`, `rm`
#### There are a lot more commands, just ask docker: `docker`
---
![image](http://i.imgur.com/zzTf9e7.png)
### How does a image being created?
---
![image](http://i.imgur.com/knYQxav.png)
### Commit! (Snapshot)
#### `docker commit container_id [image_name]`
---
## image related commands
#### `images`, `rmi`
---
![docker-build](http://i.imgur.com/1HvxbbG.png)
### Automation?
#### `Dockerfile` => `docker build`
---
### Let's create a Dockerfile for the rails project just created
* `FROM image` : base image
* `RUN command` : command to run
* `WORKDIR /path` : absolute path to `cd`
* `COPY|ADD src des` : add file from context to image, [difference between COPY & ADD](http://stackoverflow.com/questions/24958140/docker-copy-vs-add)
* `CMD` : set default `docker run` command
[More about Dockerfile](https://docs.docker.com/engine/reference/builder/)
---
## Build it!
```
docker build -t {image_name} {path_to_context}
```
---
## Filesystem that stores changes
#### Shared Layers between images
#### Cached when build
---
![docker-build](http://i.imgur.com/wMBZrst.png)
### Push! (Upload)
```
docker push {image}
```
---
![docker-compose](http://i.imgur.com/Ot2kx3C.png)
### Multiple Containers and More automation
---
## What if I need mysql/postgresql?
```
RUN apt-get install postgresql...?
```
---
### `docker-compose`
* Features
* No more annoying parameters, more automations
* Multiple containers at the same time
* Link containers together so they can 'compose'
* [How to install](https://docs.docker.com/compose/install/)
* docker toolbox or native have already installed for you
---
### The `docker-compose.yml`
```
version: '2'
services:
services_1:
...services_settings...
services_2:
...services_settings...
```
---
### Service settings
* What image to run
* `image: {image_name}`
* `build: {path/to/context}`
* `ports`, `volumes`
* `command`
* `depends_on`, `environment`
#### [More settings](https://docs.docker.com/compose/compose-file/)
---
## Let's use docker-compose to compose rails and postgres!
#### [Postgresql on dockerhub](https://hub.docker.com/_/postgres/)
---
### Up!
* `config` to check if is correct
* `build`
* `up [service]` / `run {service}`
* `logs`
* `ps`, `kill`, `rm`
---
## One more thing
#### One-off command, eg. `rails c`, `rake ...`
---
## `entrypoint`
#### Use this to make rails allow other commands
---
### Thank you for listening
https://github.com/pastleo/rails_on_docker
{"metaMigratedAt":"2023-06-15T12:20:16.092Z","metaMigratedFrom":"YAML","title":"Rails on Docker","breaks":"true","slideOptions":"{\"spotlight\":{\"enabled\":true,\"size\":80,\"presentingCursor\":\"default\",\"toggleSpotlightOnMouseDown\":false,\"spotlightOnKeyPressAndHold\":90,\"initialPresentationMode\":true,\"disablingUserSelect\":false,\"fadeInAndOut\":500}}","contributors":"[{\"id\":\"0eb274f7-a3a4-4c8e-b0d4-e5c08eaf9e72\",\"add\":4328,\"del\":0}]"}