From 935ebcf8091ce04a8727bbd691fcc55804e25794 Mon Sep 17 00:00:00 2001 From: Lincoln Anders Date: Mon, 1 Feb 2021 14:37:36 -0500 Subject: [PATCH] Add Dockerfile (#28) * First Dockerfile * Fixed dependencies * Dependency build order sped up * rm .env * Update README.md * Add Windows quirks to ReadMe * Add rails webpacker:install to the readme * Gemfile bump Co-authored-by: tumbleshack <43155606+tumbleshack@users.noreply.github.com> --- .dockerignore | 3 +++ .gitignore | 3 +++ Dockerfile | 21 +++++++++++++++++++ Gemfile.lock | 4 ++++ README.md | 39 ++++++++++++++++++++++++++++++++++-- config/database.yml | 2 +- docker-compose.yml | 49 +++++++++++++++++++++++++++++++++++++++++++++ example.env | 8 ++++++++ package.json | 1 + yarn.lock | 29 ++------------------------- 10 files changed, 129 insertions(+), 30 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 example.env diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e0ab448 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +/node_modules +/tmp +/db/data \ No newline at end of file diff --git a/.gitignore b/.gitignore index f22dd34..82df97d 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ /yarn-error.log yarn-debug.log* .yarn-integrity + +.env +/db/data diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9d4e693 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM ruby:2.7.0 +RUN apt-get update -qq + +RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - +RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list +RUN apt-get update && apt-get install -y yarn + +WORKDIR /cc_server + +COPY Gemfile /cc_server/Gemfile +COPY Gemfile.lock /cc_server/Gemfile.lock +RUN bundle install && gem install bundler:2.2.1 + +COPY package.json /cc_server/package.json +COPY yarn.lock /cc_server/yarn.lock +RUN yarn install --check-files + +COPY . /cc_server + +# Start the main process. +CMD rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 61b737e..8b6281d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -112,6 +112,8 @@ GEM nio4r (2.5.4) nokogiri (1.11.1-x86_64-darwin) racc (~> 1.4) + nokogiri (1.11.1-x86_64-linux) + racc (~> 1.4) orm_adapter (0.5.0) public_suffix (4.0.6) puma (5.1.1) @@ -212,7 +214,9 @@ GEM zeitwerk (2.4.2) PLATFORMS + ruby x86_64-darwin-19 + x86_64-linux DEPENDENCIES bootsnap (>= 1.4.4) diff --git a/README.md b/README.md index 629b2b3..c198160 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,20 @@ # Carrie's Closet ## Getting Started +On windows, make sure you set git [to use unix line endings](https://docs.github.com/en/github/using-git/configuring-git-to-handle-line-endings) +when checking out and committing. + +If you've already checked out and have windows line endings, you may encounter +an error stating: `env: ruby\r: No such file or directory`. To resolve, run +`find ./ -type f -exec dos2unix {} \;`. + After a clean install of this repo, you'll need to run `bundle install` inside - the `CarriesCloset` root folder. This will install all needed requirements. +the `CarriesCloset` root folder. This will install all needed requirements. + +Additionally, **duplicate** the file `example.env` in the root level directory. +Once duplicated, rename the duplicate, e.g. `example.env (1)`, to just `.env`. +This may cause the `.env` to disapear from your file brower, but don't worry +if you've named the file correctly. ## Database Creation You'll need to have MySQL installed and running locally. @@ -25,4 +37,27 @@ bin/rails db:create && bin/rails db:migrate && bin/rails db:seed create a `Tweet` model that belongs to a `User` model. Each `Tweet` will be stored in the database with a `body` text field and a `likes` counter. - `bin/rails destroy Tweet` will revert all generated files for the previous - `Tweet` model. \ No newline at end of file + `Tweet` model. + +## Running with Docker +To run the full app stack, run the following commands: +```shell +docker-compose build --parallel && docker-compose up -d +docker exec cc_server sh -c "bin/rails db:create && bin/rails db:migrate && bin/rails db:seed" +docker exec cc_server sh -c "rails webpacker:install" +``` + +Note that on Windows, you may need to run these command seprately. Eg. +``` +docker-compose build --parallel +docker-compose up -d +docker exec cc_server sh -c "bin/rails db:create" +docker exec cc_server sh -c "bin/rails db:migrate" +docker exec cc_server sh -c "bin/rails db:seed" +docker exec cc_server sh -c "rails webpacker:install" +``` + +This will create the `cc_mysql` MySQL Docker container, the `cc_redis` Redis +caching store, and the `cc_server` Rails server. Both commands should complete +without error to let you know everything is running properly. The Rails server +will run on http://127.0.0.1:3000 in most environments. diff --git a/config/database.yml b/config/database.yml index 7b93621..d236e3b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -15,7 +15,7 @@ default: &default pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: - host: 127.0.0.1 + host: <%= ENV['DATABASE_HOST'] %> development: <<: *default diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..595d714 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,49 @@ +version: "3.7" +services: + cc_server: + build: . + container_name: cc_server + # restart: unless-stopped + env_file: .env + volumes: + - .:/cc_server:cached + ports: + - 3000:3000 + expose: + - 3000 + networks: + - container_network + depends_on: + - cc_mysql + - cc_redis + cc_mysql: + container_name: cc_mysql + image: mysql:8.0 + command: --default-authentication-plugin=mysql_native_password +# restart: always + env_file: .env + ports: + - "127.0.0.1:3306:3306" + expose: + - 3306 + volumes: + - ./db/scripts/:/docker-entrypoint-initdb.d/ + - ./db/data/MySQL/:/var/lib/mysql/ + networks: + - container_network + cc_redis: + container_name: cc_redis + image: redis:latest + command: redis-server --requirepass $REDIS_PASSWORD --bind cc_redis --port 6379 + env_file: .env + ports: + - "127.0.0.1:6379:6379" + expose: + - 6379 + volumes: + - ./db/data/Redis:/data + networks: + - container_network +networks: + container_network: + driver: bridge \ No newline at end of file diff --git a/example.env b/example.env new file mode 100644 index 0000000..2896085 --- /dev/null +++ b/example.env @@ -0,0 +1,8 @@ +RAILS_ENV=development + +DATABASE_NAME=CarriesCloset_development +DATABASE_USER=CarriesCloset_development +MYSQL_ALLOW_EMPTY_PASSWORD=true +DATABASE_HOST=cc_mysql + +REDIS_PASSWORD=password diff --git a/package.json b/package.json index f5d911f..3741baa 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "version": "0.1.0", "devDependencies": { + "webpack": "^4.43.0", "webpack-dev-server": "^3.11.2" } } diff --git a/yarn.lock b/yarn.lock index b49d020..1001ef9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1427,13 +1427,6 @@ binary-extensions@^2.0.0: resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d" integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA== -bindings@^1.5.0: - version "1.5.0" - resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df" - integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ== - dependencies: - file-uri-to-path "1.0.0" - block-stream@*: version "0.0.9" resolved "https://registry.yarnpkg.com/block-stream/-/block-stream-0.0.9.tgz#13ebfe778a03205cfe03751481ebb4b3300c126a" @@ -2939,11 +2932,6 @@ file-loader@^6.0.0: loader-utils "^2.0.0" schema-utils "^3.0.0" -file-uri-to-path@1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd" - integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw== - fill-range@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-4.0.0.tgz#d544811d428f98eb06a63dc402d2403c328c38f7" @@ -3114,19 +3102,6 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= -fsevents@^1.2.7: - version "1.2.13" - resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.2.13.tgz#f325cb0455592428bcf11b383370ef70e3bfcc38" - integrity sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw== - dependencies: - bindings "^1.5.0" - nan "^2.12.1" - -fsevents@~2.3.1: - version "2.3.1" - resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.1.tgz#b209ab14c61012636c8863507edf7fb68cc54e9f" - integrity sha512-YR47Eg4hChJGAB1O3yEAOkGO+rlzutoICGqGo9EZ4lKWokzZRSyIW1QmTzqjtw8MJdj9srP869CuWw/hyzSiBw== - fstream@^1.0.0, fstream@^1.0.12: version "1.0.12" resolved "https://registry.yarnpkg.com/fstream/-/fstream-1.0.12.tgz#4e8ba8ee2d48be4f7d0de505455548eae5932045" @@ -4553,7 +4528,7 @@ multicast-dns@^6.0.1: dns-packet "^1.3.1" thunky "^1.0.2" -nan@^2.12.1, nan@^2.13.2: +nan@^2.13.2: version "2.14.2" resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19" integrity sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ== @@ -7457,7 +7432,7 @@ webpack-sources@^1.0.0, webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack- source-list-map "^2.0.0" source-map "~0.6.1" -webpack@^4.44.1: +webpack@^4.43.0, webpack@^4.44.1: version "4.46.0" resolved "https://registry.yarnpkg.com/webpack/-/webpack-4.46.0.tgz#bf9b4404ea20a073605e0a011d188d77cb6ad542" integrity sha512-6jJuJjg8znb/xRItk7bkT0+Q7AHCYjjFnvKIWQPkNIOyRqoCGvkOs0ipeQzrqz4l5FtN5ZI/ukEHroeX/o1/5Q==