I introduced Docker to Rails 6, so I summarized it (beginner)

Purpose

Since I implemented the portfolio and introduced docker, I will output it. I didn't know what docker was, so I refer to qiita from various people.

Docker installation

I saw this article and installed it! !! https://qiita.com/kurkuru/items/127fa99ef5b2f0288b81 After installation, execute the following command.

 % docker run -d -p 80:80 docker/getting-started

Confirmation of installation

% docker -v

Installation is complete when a command like this appears.

Docker version 20.10.2, build 2291f61



% docker-compose -v

 This is also complete when ``` docker-compose version 1.27.4, build 40524192``` is output.
# Over the introduction of Docker
 As a merit, you can execute rails and mysql built in the local environment with just one command. It was written as `` `, but it was difficult for me as a beginner to understand. After introducing docker, I understood this meaning, so it is better to move your hand first.


# Introducing docker in an app under development
 I will introduce it immediately.
 *** The framework uses Ruby on Rails 6. *** ***
### Creating a Dockerfile
 As a procedure, go to the application under development where you want to install Docker

~ %cd app name

app name% touch Dockerfile

 Since the Dockerfile has been created, please describe the following.

```dockerfile
FROM ruby:2.6.5 

RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       libpq-dev \
                       nodejs

RUN mkdir /app name
WORKDIR /app name

ADD ./Gemfile /app name/Gemfile
ADD ./Gemfile.lock /app name/Gemfile.lock

RUN gem install bundler
RUN bundle install
ADD . /app name

Change the app name to the name of the app you are developing.

Let's create Docker-compose.yml.

app name% touch docker-compose.yml

Let's write the following.

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 'password' #It works fine as a password as it is
    ports:
      - "4306:3306" #Settings required for connecting to Docker container and Sequelpro

  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app name
    ports:
      - "3000:3000"
    depends_on:
      - db
      

Edit config/database.yml

config/database.yml


default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password
  socket: /tmp/mysql.sock
  host: db
development:
  <<: *default
  database:app name_development


production.
test:
  <<: *default
  database:app name_test

Only password and `` `host: db``` have been added

Create a container with docker-compose.yml build

app name% docker-compose build

If there are no errors, it is successful.

Create DB and execute migrate on container

app name% docker-compose run web bundle exec rails db:create

If it is Rails6, an error will occur when this command is executed, so edit it.

config/webpacker.yml


  # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
  check_yarn_integrity: false

Approximately line 56 `check_yarn_integrity: true``` Since it is, change this to `false```.

When you're done editing, run the same command again. Next, let's migrate.

app name% docker-compose run web bundle exec rake db:migrate

Now you have a db design on the container. Finally, let's start the container.

app name% docker-compose up 

Installation is complete if you can access localhost: 3000 and start it.

Launch from docker hub

Since multiple apps are loaded in the container, you can start the app with just one click without typing a command. You can start it by clicking the red RAN. スクリーンショット 2021-01-14 13.27.21.png

You can stop it by clicking it again.

Wordbook

① What is a docker file

The material that creates the container. When I executed the command `` `docker-compose build```, I think that it was loaded in steps. If the reading is successful, it means that the container has been created.

② What is docker-compose?

A tool for moving multiple containers at the same time

It's like this! I will do my best to write in more detail.

Recommended Posts

I introduced Docker to Rails 6, so I summarized it (beginner)
After understanding Docker, I actually introduced it.
Rails6 I tried to introduce Docker to an existing application
firewalld went wrong, so I managed to fix it.
I tried to build an environment using Docker (beginner)
I tried Rails beginner [Chapter 1]
I tried Rails beginner [Chapter 2]
Introduced gRPC client to rails
Beginner Ruby on Rails What I learned is being summarized
I did Docker construction of rails + postgresql as official, but it didn't work, so I fixed some
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Deploy Rails on Docker to heroku
We introduced clang to docker today. (Solution)
I tried to introduce CircleCI 2.0 to Rails app
I found Lombok useful so I wrote it
[Rails] How to use rails console with docker
Migrate existing Rails 6 apps to Docker environment
Deploy to heroku with Docker (Rails 6, MySQL)
Introduced Vue.js to an existing Rails app
Deploy to Heroku [Ruby on Rails] Beginner
Introduced vscode with apt to docker: ubuntu
What is Docker? I tried to summarize
I found MyBatis useful, so I wrote it.
How to use credentials.yml.enc introduced in Rails 5.2
Introduced Vuetify to an existing Rails app
How to build Rails 6 environment with Docker
Introduced Docker to Windows 10 Home August 2020 version
Delegated type added in Rails 6.1 and Union types of GraphQL seem to be compatible, so I tried it
Beginner: I get an error on line 30. I don't know where to fix it.
I used it without knowing the O / R mapping of rails, so I checked it.
I didn't know what to write in Maven's scope so I looked it up
I can't remember the text file input / output in Java, so I summarized it.
Apple Silicon compatible version of Docker Desktop (Preview) has been released to the public, so I will try it
I used Docker for my portfolio as a beginner, so I hope that even 1mm will be helpful to someone.
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I installed Docker on EC2 and started it
The code I used to connect Rails 3 to PostgreSQL 10
I want to play with Firestore from Rails
I summarized the naming conventions for each Rails
[Rails] How to build an environment with Docker
Introduce dotenv to Docker + Rails to manage environment variables
[Rails] I want to load CSS with webpacker
Easy to display hello world with Rails + Docker
[Java] I studied polymorphism, so I will summarize how to use it and its merits.
I couldn't run Processing on jdk-9, so I reverted to jdk-1.8 and it worked on Eclipse OXYGEN
7 things I want you to keep so that it doesn't become a fucking code
kintone clone? I was quite addicted to launching OSS WebDB Extension with Lightsail + Docker, so make a note of it.