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.
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.
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
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
app name% docker-compose build
If there are no errors, it is successful.
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.
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.
You can stop it by clicking it again.
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.
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