[JAVA] [Docker] How to create a virtual environment for Rails and Nuxt.js apps

How can I use the Docker environment with an application that uses Nuxt and Rails? For reference, I will explain the files and commands up to the construction step by step!

This time, I would like to build an application development environment called feeder.

Files and directories prepared by yourself

#Files prepared by yourself

|-web/
|    |--app folder
|    |--Dockerfile
|--back/
|    |--app folder
|    |--Dockerfile
|    |--Gemfile
|    |--Gemfile.lock  #Empty file
|--docker-compose.yml
|--.git

Settings and Docker command files for container creation, startup (server startup), image creation, etc.

docker-compose.yml


version: "3"

services:
  db:
    container_name: database
    image: mysql:5.7
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: feeder
      MYSQL_DATABASE: feeder
      MYSQL_USER: feeder
      MYSQL_PASSWORD: feeder
      TZ: Asia/Tokyo
    ports:
      - 3308:3306
    volumes:
      - ./database/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./database/data:/var/lib/mysql
      - ./database/sql:/docker-entrypoint-initdb.d

  api:
    container_name: back
    tty: true
    depends_on:
      - db
    build:
      context: back/
      dockerfile: Dockerfile
    ports:
      - 3000:3000
    volumes:
      - ./back/app:/app
    command: rails server -b 0.0.0.0

  front:
    container_name: web
    tty: true
    build:
      context: web/
      dockerfile: Dockerfile
    ports:
      - 8080:3000
    volumes:
      - ./web/app:/app
    command: sh -c 'yarn install; yarn dev'

Description of files required for image creation and creation

Nuxt image blueprint

FROM node:12.5.0-alpine

ENV LANG=C.UTF-8 \
  TZ=Asia/Tokyo

WORKDIR /app

RUN apk update && \
  apk upgrade && \
  npm install -g npm && \
  npm install -g @vue/cli && \
  npm install -g create-nuxt-app

ENV HOST 0.0.0.0
EXPOSE 3000

Rails image blueprint

FROM ruby:2.5

ENV LANG=C.UTF-8 \
  TZ=Asia/Tokyo

WORKDIR /app

RUN apt-get update -qq && \
  apt-get install -y nodejs default-mysql-client

COPY app/Gemfile /app/Gemfile
COPY app/Gemfile.lock /app/Gemfile.lock
RUN bundle install

Gemfile


source 'https://rubygems.org'
gem 'rails', '~> 6.0.3', '>= 6.0.3.2'

Image creation

$ docker-compose build

Nuxt application generation

$ docker-compose run --rm web yarn create nuxt-app

Rails application generation

$ docker-compose run --rm back rails new . -f -d mysql --api

Setting up the connection between DB and rails

database.yml


# MySQL. Versions 5.5.8 and up are supported.
#
# Install the MySQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
  adapter: mysql2
  charset: utf8mb4
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: feeder
  password: feeder
  host: database

development:
  <<: *default
  database: feeder

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: feeder

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: feeder
  username: feeder
  password: <%= ENV['FEEDER_DATABASE_PASSWORD'] %>

Creating a database

$ docker-compose build
$ docker-compose run --rm back rails db:create

Start the server (start container)

$ docker-compose up -d

Enter the command in the service (in the container)

$ docker-compose exec service name command script to execute

I want to type a Rails application command

$ docker-compose exec back sh

I want to type a command for a Nuxt application

$ docker-compose exec web sh

Recommended Posts

[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to create a lightweight container image for Java apps
How to create and launch a Dockerfile for Payara Micro
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Migrate existing Rails 6 apps to Docker environment
How to create a Maven repository for 2020
[rails] How to create a partial template
How to build Rails 6 environment with Docker
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
Build a development environment for Docker + Rails6 + Postgresql
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
How to create pagination for a "kaminari" array
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to easily create a pull-down in Rails
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to create a Twitter share button
[Rails] How to create a table, add a column, and change the column type
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Rails] How to build an environment with Docker
Create a docker environment for Oracle 11g XE
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Build a development environment to create Ruby on Jets + React apps with Docker
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook
How to create a Java environment in just 3 seconds
How to build docker environment with Gradle for intelliJ
(Ruby on Rails6) How to create models and tables
How to create a method
Docker command to create Rails project with a single blow in environment without Ruby
Tutorial to create a blog with Rails for beginners Part 1
How to set and describe environment variables using Rails zsh
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
Tutorial to create a blog with Rails for beginners Part 2
Create Rails5 and postgresql environment with Docker and make pgadmin available
I tried to create a padrino development environment with Docker
Tutorial to create a blog with Rails for beginners Part 0
Building an environment for creating apps with Rails and Vue
Create a Vue3 environment with Docker!
Preparing to create a Rails application
How to create an environment that uses Docker (WSL2) and Vagrant (VirtualBox) together on Windows
[Rails] How to execute "rails db: create" etc. in production environment EC2
[Rails 6] How to create a dynamic form input screen using cocoon
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to install Pry after building Rails development environment with Docker
Install Rails in the development environment and create a new application
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
Use Jenkins to build inside Docker and then create a Docker image.
Create a Docker Image for redoc-cli and register it on Docker Hub
[For those who create portfolios] How to use binding.pry with Docker
How to build an environment with Docker, which is the minimum required to start a Rails application
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Create a MySQL environment with Docker from 0-> 1
[Swift5] How to create a splash screen
Note: [Docker] How to start and stop