├── .gitignore ├── Dockerfile ├── Gemfile ├── Gemfile.lock ├── README.md └── docker-compose.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore all logfiles and tempfiles. 11 | /log/* 12 | !/log/.keep 13 | /tmp 14 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ruby:2.3.0 2 | ENV LANG C.UTF-8 3 | 4 | RUN apt-get update && \ 5 | apt-get install -y nodejs \ 6 | vim \ 7 | mysql-client \ 8 | --no-install-recommends && \ 9 | rm -rf /var/lib/apt/lists/* 10 | 11 | #Cache bundle install 12 | WORKDIR /tmp 13 | ADD ./Gemfile Gemfile 14 | ADD ./Gemfile.lock Gemfile.lock 15 | RUN bundle install 16 | 17 | ENV APP_ROOT /workspace 18 | RUN mkdir -p $APP_ROOT 19 | WORKDIR $APP_ROOT 20 | COPY . $APP_ROOT 21 | 22 | EXPOSE 3000 23 | CMD rm -f tmp/pids/server.pid && rails s -b '0.0.0.0' 24 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'rails', '4.2.6' 3 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nooptr/docker-rails-mysql/092e801592745804fda09afbbb30dc70e4adaba1/Gemfile.lock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # About this Repo 2 | This is the Git repo of the Docker [official image](https://docs.docker.com/docker-hub/official_repos/) for [rails] (https://registry.hub.docker.com/_/rails/) and [mysql](https://hub.docker.com/_/mysql/). See these page for the full readme on how to use this Docker image and for information regarding contributing and issues. 3 | 4 | ## Pre-Requirements 5 | First install `docker`, `docker compose` for your machine and start it. How this is done is very well documented all over the internet. 6 | 7 | ### Build the project 8 | You need generate the Rails skeleton app using docker-compose run: 9 | ``` 10 | $ docker-compose run app rails new . --force --database=mysql --skip-bundle 11 | ``` 12 | 13 | Compose will build the image for the web service using the Dockerfile. Then it’ll run rails new inside a new container, using that image. Once it’s done, you should have generated a fresh app: 14 | ``` 15 | $ ls -l 16 | total 56 17 | -rw-r--r-- 1 user staff 488 Nov 22 22:02 Dockerfile 18 | -rw-r--r-- 1 user staff 1521 Nov 22 01:36 Gemfile 19 | -rw-r--r-- 1 user staff 4006 Nov 22 01:36 Gemfile.lock 20 | -rw-r--r-- 1 user staff 478 Nov 22 01:17 README.rdoc 21 | -rw-r--r-- 1 user staff 249 Nov 22 01:17 Rakefile 22 | drwxr-xr-x 8 user staff 272 Nov 22 01:36 app 23 | drwxr-xr-x 7 user staff 238 Nov 22 01:36 bin 24 | drwxr-xr-x 11 user staff 374 Nov 22 10:38 config 25 | -rw-r--r-- 1 user staff 153 Nov 22 01:17 config.ru 26 | drwxr-xr-x 5 user staff 170 Nov 22 10:46 db 27 | -rw-r--r-- 1 user staff 420 Nov 22 22:02 docker-compose.yml 28 | drwxr-xr-x 4 user staff 136 Nov 22 01:36 lib 29 | drwxr-xr-x 4 user staff 136 Nov 22 01:39 log 30 | drwxr-xr-x 7 user staff 238 Nov 22 01:36 public 31 | drwxr-xr-x 9 user staff 306 Nov 22 01:27 test 32 | drwxr-xr-x 6 user staff 204 Nov 22 01:39 tmp 33 | drwxr-xr-x 3 user staff 102 Nov 22 01:36 vendor 34 | ``` 35 | 36 | Now that you’ve got rails app, you need to build the image again. 37 | ``` 38 | docker-compose build 39 | ``` 40 | 41 | ### Connect the database 42 | The app is now bootable, but you’re not quite there yet. By default, Rails expects a database to be running on `localhost` - so you need to point it at the `mysql container` instead. You also need to change the database and username to align with the defaults set by the mysql image. 43 | 44 | Replace the contents of config/database.yml with the following: 45 | ``` 46 | default: &default 47 | adapter: mysql2 48 | encoding: utf8 49 | pool: 5 50 | username: root 51 | password: "root" 52 | host: mysql 53 | 54 | development: 55 | <<: *default 56 | database: dev 57 | 58 | test: 59 | <<: *default 60 | database: dev 61 | ``` 62 | 63 | You can now boot the app with: 64 | ``` 65 | docker-compose up -d 66 | ``` 67 | 68 | Finally, you need to create the database. In another terminal, run: 69 | ``` 70 | docker-compose run app rake db:create 71 | ``` 72 | 73 | That’s it. Your app should now be running on port 3000 on your Docker daemon. If you’re using Docker Machine, then `docker-machine ip MACHINE_VM` returns the Docker host IP address. 74 | ``` 75 | $ docker-machine ip default 76 | 192.168.99.100 77 | ``` 78 | 79 | http://192.168.99.100:3000/ 80 | 81 | ![rails](https://cloud.githubusercontent.com/assets/5398914/20524938/578c3872-b100-11e6-8a7f-d359f982bd24.png) 82 | 83 | To stop container then run following command: 84 | ``` 85 | $ docker-compose stop 86 | Stopping ror_app_1 ... done 87 | Stopping ror_mysql_1 ... done 88 | ``` 89 | 90 | To start container again then run following command: 91 | ``` 92 | $ docker-compose start 93 | Starting dbdata ... done 94 | Starting appdata ... done 95 | Starting mysql ... done 96 | Starting app ... done 97 | ``` 98 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | mysql: 2 | image: mysql:5.6.34 3 | ports: 4 | - "3306:3306" 5 | volumes_from: 6 | - dbdata 7 | environment: 8 | - MYSQL_ROOT_PASSWORD=root 9 | - MYSQL_DATABASE=dev 10 | 11 | dbdata: 12 | image: tianon/true 13 | volumes: 14 | - /var/lib/mysql 15 | 16 | app: 17 | build: . 18 | environment: 19 | RAILS_ENV: development 20 | ports: 21 | - '3000:3000' 22 | volumes_from: 23 | - appdata 24 | links: 25 | - "mysql" 26 | 27 | appdata: 28 | image: tianon/true 29 | volumes: 30 | - ".:/workspace" 31 | --------------------------------------------------------------------------------