From 91ea932ae61fad4dbf618f7b48cbdeef8601c2f1 Mon Sep 17 00:00:00 2001 From: Jose E Date: Tue, 17 Apr 2018 22:40:18 -0600 Subject: [PATCH 1/6] updating bash_profile consolidating some useful information. The following lines have to be added to the ~/.bash_profile for it to work. I pulled this information from https://docs.docker.com/compose/completion/. I thought it would be useful to add it here so that all this info could be in one place. if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi --- docker-for-mac/index.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docker-for-mac/index.md b/docker-for-mac/index.md index 7f506471a1..b41e23193e 100644 --- a/docker-for-mac/index.md +++ b/docker-for-mac/index.md @@ -460,6 +460,14 @@ ln -s $etc/docker-machine.bash-completion $(brew --prefix)/etc/bash_completion.d ln -s $etc/docker-compose.bash-completion $(brew --prefix)/etc/bash_completion.d/docker-compose ``` +Add the following to your `~/.bash_profile`: + +```shell +if [ -f $(brew --prefix)/etc/bash_completion ]; then +. $(brew --prefix)/etc/bash_completion +fi +``` + ### Zsh In Zsh, the [completion system](http://zsh.sourceforge.net/Doc/Release/Completion-System.html){:target="_blank" class="_"} From 7f4e5e3c81388592d26d30755f511cdb71f4c76e Mon Sep 17 00:00:00 2001 From: Jose E Date: Wed, 18 Apr 2018 11:59:52 -0600 Subject: [PATCH 2/6] added another ~/.bash_profile option Added another ~/.bash_profile options, listed below: ` [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion ` --- docker-for-mac/index.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docker-for-mac/index.md b/docker-for-mac/index.md index b41e23193e..90ee978bb6 100644 --- a/docker-for-mac/index.md +++ b/docker-for-mac/index.md @@ -462,6 +462,13 @@ ln -s $etc/docker-compose.bash-completion $(brew --prefix)/etc/bash_completion.d Add the following to your `~/.bash_profile`: + +```shell +[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion +``` + +OR + ```shell if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion From b4967e1bcc42988b72a1d4c98739afeed901915b Mon Sep 17 00:00:00 2001 From: Din Tort Date: Wed, 4 Jul 2018 15:11:51 +0200 Subject: [PATCH 3/6] Updated gelf.md Fixed an ivalid dash in --log-opt gelf-address --- config/containers/logging/gelf.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/containers/logging/gelf.md b/config/containers/logging/gelf.md index 55a7462830..9ca0adbae2 100644 --- a/config/containers/logging/gelf.md +++ b/config/containers/logging/gelf.md @@ -46,7 +46,7 @@ and `--log-opt` options to the Docker daemon: ```bash dockerd - --log-driver gelf –-log-opt gelf-address=udp://1.2.3.4:12201 \ + --log-driver gelf --log-opt gelf-address=udp://1.2.3.4:12201 \ ``` To make the configuration permanent, you can configure it in `/etc/docker/daemon.json`: From 3238740467e4b43358f7181295cbcd7b81949ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20K=C3=A4mper-Leymann?= Date: Sun, 18 Mar 2018 22:14:58 +0100 Subject: [PATCH 4/6] Update rails.md It occupied me quite a while figuring out why this sample didn't have a restart policy. And why we where supposed to always properly stop the containers wheres you normally would simply hit `Ctrl+C`. Turned out that this `pid` issue is a rails-specific issue. https://github.com/docker/compose/issues/1393#issuecomment-99988242 And of course there's already [a Gem for fixing that](https://github.com/lorenzosinisi/shutup) but having the one-liner in the `docker-compose.yml` keeps the logic where it belongs to (and now also is treated here in the sample). --- compose/rails.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compose/rails.md b/compose/rails.md index d3f071f78a..4ded542f5d 100644 --- a/compose/rails.md +++ b/compose/rails.md @@ -47,11 +47,13 @@ to link them together and expose the web app's port. services: db: image: postgres + restart: always volumes: - ./tmp/db:/var/lib/postgresql/data web: build: . - command: bundle exec rails s -p 3000 -b '0.0.0.0' + restart: always + command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/myapp ports: From 9c4ebb876cc8bce3041fe84986b0e9fbd900a3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20K=C3=A4mper-Leymann?= Date: Wed, 21 Mar 2018 08:36:29 +0100 Subject: [PATCH 5/6] Update rails.md Remove `restart: always` again. --- compose/rails.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/compose/rails.md b/compose/rails.md index 4ded542f5d..0ebe3bdc43 100644 --- a/compose/rails.md +++ b/compose/rails.md @@ -47,12 +47,10 @@ to link them together and expose the web app's port. services: db: image: postgres - restart: always volumes: - ./tmp/db:/var/lib/postgresql/data web: build: . - restart: always command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/myapp From 7d2f8cfc89b7fd35747c3dd400dd1571bf884c18 Mon Sep 17 00:00:00 2001 From: Maria Bermudez Date: Wed, 2 Jan 2019 17:15:34 -0800 Subject: [PATCH 6/6] Fix merge conflicts and incorporate content feedback --- compose/rails.md | 113 +++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 47 deletions(-) diff --git a/compose/rails.md b/compose/rails.md index 0ebe3bdc43..4e8a1af82a 100644 --- a/compose/rails.md +++ b/compose/rails.md @@ -9,11 +9,14 @@ a Rails/PostgreSQL app. Before starting, [install Compose](install.md). ### Define the project -Start by setting up the four files needed to build the app. App will run inside a Docker container containing its dependencies. Defining dependencies is done using a file called `Dockerfile`. To begin with, the +Start by setting up the five files needed to build the app. First, since +your app is going to run inside a Docker container containing all of its +dependencies, define exactly what needs to be included in the +container. This is done using a file called `Dockerfile`. To begin with, the Dockerfile consists of: FROM ruby:2.5 - RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs + RUN apt-get update -qq && apt-get install -y nodejs postgresql-client RUN mkdir /myapp WORKDIR /myapp COPY Gemfile /myapp/Gemfile @@ -21,6 +24,15 @@ Dockerfile consists of: RUN bundle install COPY . /myapp + # Add a script to be executed every time the container starts. + COPY entrypoint.sh /usr/bin/ + RUN chmod +x /usr/bin/entrypoint.sh + ENTRYPOINT ["entrypoint.sh"] + EXPOSE 3000 + + # Start the main process. + CMD ["rails", "server", "-b", "0.0.0.0"] + That'll put your application code inside an image that builds a container with Ruby, Bundler and all your dependencies inside it. For more information on how to write Dockerfiles, see the [Docker user @@ -31,12 +43,28 @@ Next, create a bootstrap `Gemfile` which just loads Rails. It'll be overwritten in a moment by `rails new`. source 'https://rubygems.org' - gem 'rails', '5.2.0' + gem 'rails', '5.1.5.0' Create an empty `Gemfile.lock` to build our `Dockerfile`. touch Gemfile.lock +Next, provide an entrypoint script to fix a Rails-specific issue that +prevents the server from restarting when a certain `server.pid` file pre-exists. +This script will be executed every time the container gets started. +`entrypoint.sh` consists of: + +```bash +#!/bin/bash +set -e + +# Remove a potentially pre-existing server.pid for Rails. +rm -f /myapp/tmp/pids/server.pid + +# Then exec the container's main process (what's set as CMD in the Dockerfile). +exec "$@" +``` + Finally, `docker-compose.yml` is where the magic happens. This file describes the services that comprise your app (a database and a web app), how to get each one's Docker image (the database just runs on a pre-made PostgreSQL image, and @@ -61,7 +89,6 @@ to link them together and expose the web app's port. >**Tip**: You can use either a `.yml` or `.yaml` extension for this file. - ### Build the project With those four files in place, you can now generate the Rails skeleton app @@ -75,36 +102,35 @@ image. Once it's done, you should have generated a fresh app. List the files. -```shell +```bash $ ls -l -total 72 --rw-r--r-- 1 vmb staff 223 5 26 14:20 Dockerfile --rw-r--r-- 1 vmb staff 2223 5 26 14:24 Gemfile --rw-r--r-- 1 vmb staff 5300 5 26 14:25 Gemfile.lock --rw-r--r-- 1 vmb staff 374 5 26 14:24 README.md --rw-r--r-- 1 vmb staff 227 5 26 14:24 Rakefile -drwxr-xr-x 10 vmb staff 320 5 26 14:24 app -drwxr-xr-x 9 vmb staff 288 5 26 14:25 bin -drwxr-xr-x 16 vmb staff 512 5 26 14:24 config --rw-r--r-- 1 vmb staff 130 5 26 14:24 config.ru -drwxr-xr-x 3 vmb staff 96 5 26 14:24 db --rw-r--r-- 1 vmb staff 266 5 26 14:22 docker-compose.yml -drwxr-xr-x 4 vmb staff 128 5 26 14:24 lib -drwxr-xr-x 3 vmb staff 96 5 26 14:24 log --rw-r--r-- 1 vmb staff 63 5 26 14:24 package.json -drwxr-xr-x 9 vmb staff 288 5 26 14:24 public -drwxr-xr-x 3 vmb staff 96 5 26 14:24 storage -drwxr-xr-x 11 vmb staff 352 5 26 14:24 test -drwxr-xr-x 6 vmb staff 192 5 26 14:24 tmp -drwxr-xr-x 3 vmb staff 96 5 26 14:24 vendor - +total 64 +-rw-r--r-- 1 vmb staff 222 Jun 7 12:05 Dockerfile +-rw-r--r-- 1 vmb staff 1738 Jun 7 12:09 Gemfile +-rw-r--r-- 1 vmb staff 4297 Jun 7 12:09 Gemfile.lock +-rw-r--r-- 1 vmb staff 374 Jun 7 12:09 README.md +-rw-r--r-- 1 vmb staff 227 Jun 7 12:09 Rakefile +drwxr-xr-x 10 vmb staff 340 Jun 7 12:09 app +drwxr-xr-x 8 vmb staff 272 Jun 7 12:09 bin +drwxr-xr-x 14 vmb staff 476 Jun 7 12:09 config +-rw-r--r-- 1 vmb staff 130 Jun 7 12:09 config.ru +drwxr-xr-x 3 vmb staff 102 Jun 7 12:09 db +-rw-r--r-- 1 vmb staff 211 Jun 7 12:06 docker-compose.yml +-rw-r--r-- 1 vmb staff 184 Jun 7 12:08 entrypoint.sh +drwxr-xr-x 4 vmb staff 136 Jun 7 12:09 lib +drwxr-xr-x 3 vmb staff 102 Jun 7 12:09 log +-rw-r--r-- 1 vmb staff 63 Jun 7 12:09 package.json +drwxr-xr-x 9 vmb staff 306 Jun 7 12:09 public +drwxr-xr-x 9 vmb staff 306 Jun 7 12:09 test +drwxr-xr-x 4 vmb staff 136 Jun 7 12:09 tmp +drwxr-xr-x 3 vmb staff 102 Jun 7 12:09 vendor ``` If you are running Docker on Linux, the files `rails new` created are owned by root. This happens because the container runs as the root user. If this is the case, change the ownership of the new files. -```shell +```bash sudo chown -R $USER:$USER . ``` @@ -150,27 +176,20 @@ You can now boot the app with [docker-compose up](/compose/reference/up/): docker-compose up -If all's well, you should see some PostgreSQL output, and then — after a few -seconds — the familiar refrain: +If all's well, you should see some PostgreSQL output. - Starting rails_db_1 ... - Starting rails_db_1 ... done - Recreating rails_web_1 ... - Recreating rails_web_1 ... done - Attaching to rails_db_1, rails_web_1 - db_1 | LOG: database system was shut down at 2017-06-07 19:12:02 UTC - db_1 | LOG: MultiXact member wraparound protections are now enabled - db_1 | LOG: database system is ready to accept connections - db_1 | LOG: autovacuum launcher started - web_1 | => Booting Puma - web_1 | => Rails 5.2.0 application starting in development - web_1 | => Run `rails server -h` for more startup options - web_1 | Puma starting in single mode... - web_1 | * Version 3.11.4 (ruby 2.5.1-p57), codename: Love Song - web_1 | * Min threads: 5, max threads: 5 - web_1 | * Environment: development - web_1 | * Listening on tcp://0.0.0.0:3000 - web_1 | Use Ctrl-C to stop +```bash +rails_db_1 is up-to-date +Creating rails_web_1 ... done +Attaching to rails_db_1, rails_web_1 +db_1 | PostgreSQL init process complete; ready for start up. +db_1 | +db_1 | 2018-03-21 20:18:37.437 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +db_1 | 2018-03-21 20:18:37.437 UTC [1] LOG: listening on IPv6 address "::", port 5432 +db_1 | 2018-03-21 20:18:37.443 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +db_1 | 2018-03-21 20:18:37.726 UTC [55] LOG: database system was shut down at 2018-03-21 20:18:37 UTC +db_1 | 2018-03-21 20:18:37.772 UTC [1] LOG: database system is ready to accept connections +``` Finally, you need to create the database. In another terminal, run: