Build Rails (API) x MySQL x Nuxt.js environment with Docker

Introduction

In one project, I decided to use Rails (API server) MySQL (DB server) x Nuxt (web server), so I decided to build the environment using Docker. At the same time as leaving a log, I will try to build an environment in a form that can be reused as universally as possible.

"Reusable form" means that when I used Laravel in another project before, I built the environment with laradock, but the environment variable is the application name and Just specify the port number, and it will build a container etc. This time I will make a Rails version of it. (By the way, laradock does not have a Dockerfile to launch a container for nuxt.)

All we have to do this time is read the values from the environment variables and set the Dockerfile and docker-compose.yml. The created environment can be found at here, so please check the details at any time.

Directory structure

.
|- mysql
|   |- Dockerfile
|   └ my.cnf
|- nuxt
|   |- src
|   |└ nuxt application
|   └ Dockerfile
|- rails
|   |- src
|   |   |- Gemfile
|   |   |- Gemfile.lock
|   |└ rails application
|   └ Dockerfile
|- docker-compose.yml
└ .env

MySQL server

First, we will build a DB server.

Dockerfile

mysql/Dockerfile


ARG MYSQL_VERSION
FROM mysql:${MYSQL_VERSION}

ENV TZ ${TIMEZONE}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && chown -R mysql:root /var/lib/mysql/

COPY my.cnf /etc/mysql/conf.d/my.cnf

RUN chmod 0444 /etc/mysql/conf.d/my.cnf

CMD ["mysqld"]

EXPOSE 3306

docker-compose.yml

docker-compose.yml


version: "3.7"

services:
  mysql:
    build:
      context: ./mysql
      args:
        - MYSQL_VERSION=${MYSQL_VERSION}
    image: ${PROJECT_NAME}-mysql-img
    container_name: "${PROJECT_NAME}_mysql"
    environment:
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - TZ=${TIMEZONE}
    volumes:
      - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
    ports:
      - "${MYSQL_PORT}:3306"
    user: "1000:50"
    command: --innodb-use-native-aio=0

Build & start container

$ docker-compose up -d mysql

By the way, the env file looks like this. Please set here individually as you like.

.env


###########################################################
###################### General Setup ######################
###########################################################
# PROJECT NAME
PROJECT_NAME=railsdock

# TIMEZONE
TIMEZONE=Asia/Tokyo

# Choose storage path on your machine. For all storage systems
DATA_PATH_HOST=./.storage

### MYSQL #################################################
MYSQL_VERSION=8.0.21
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d

### Rails #################################################
RAILS_PORT=23450

### NUXT #################################################
NUXT_PORT=8080

API server (Rails) construction

This time, we will build a Rails environment as an API server called from Nuxt (front).

Dockerfile

rails/Dockerfile


FROM ruby:2.7.1

RUN apt-get update -qq \
  && apt-get install -y build-essential default-libmysqlclient-dev \
  && rm -rf /var/lib/apt/lists/* \
  && gem update

WORKDIR /rails/src
COPY ./src/Gemfile /rails/src/
COPY ./src/Gemfile.lock /rails/src/

RUN bundle install

EXPOSE 23450

CMD ["bundle", "exec", "rails", "server", "-p", "23450", "-b", "0.0.0.0"]

By specifying CMD, the rails server can be started just by starting the container.

docker-compose.yml

docker-compose.yml


version: "3.7"

services:
  mysql:
    #abridgement

  rails:
    container_name: ${PROJECT_NAME}-rails
    build: rails/
    image: ${PROJECT_NAME}-rails-img
    volumes:
      - ./rails:/rails
    ports:
      - "${RAILS_PORT}:23450"
    depends_on: 
      - mysql
    stdin_open: true
    tty: true

By specifying mysql with depends_on, the rails server will be started after the mysql server container is started.

Launch Rails

rails/src/Gemfile


source 'https://rubygems.org'
gem 'rails', '6.0.3'

Also, make a Gemfile.lock.

touch rails/src/Gemfile.lock
$ docker-compose run rails rails new . --database=mysql --api

Set password and host in database.yml

rails/src/config/database.yml


default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: root # <= .MYSQL set in env_ROOT_PASSWORD
  host: mysql # <= docker-compose.The service name set in the yml service
$ docker-compose run rails rails db:create
$ docker-compose up -d rails

Your Rails application should now be up and running.

Please access 23450 (port number follows your own env file) and check the startup.



# Nuxt (front environment construction)
 Finally, we will build an application server with Nuxt.

 Dockerfile


#### **`nuxt/Dockerfile`**
```dockerfile

FROM node:14.5.0

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

WORKDIR /nuxt/src

RUN apt-get update && \
    yarn global add \
                create-nuxt-app \
                nuxt \
                @vue/cli \
                pug \
                pug-plain-loader \
                node-sass \
                sass-loader

ENV HOST 0.0.0.0

EXPOSE 8080

CMD [ "yarn", "dev" ]

docker-compose.yml

docker-compose.yml


version: "3.7"

services:
  mysql:
    #abridgement

  rails:
    #abridgement

  nuxt:
    build:
      context: ./nuxt
      dockerfile: Dockerfile
    image: ${PROJECT_NAME}-nuxt-img
    container_name: "${PROJECT_NAME}_nuxt_app"
    environment:
      - HOST=0.0.0.0
    volumes:
      - ./nuxt:/nuxt:cached
    ports:
      - ${NUXT_PORT}:8080
    stdin_open: true
    tty: true

Nuxt application creation

$ docker-compose run nuxt npx create-nuxt-app .


create-nuxt-app v3.1.0
✨  Generating Nuxt.js project in .
? Project name: src
? Programming language: JavaScript
? Package manager: Yarn
? UI framework: None
? Nuxt.js modules: Axios
? Linting tools: (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Testing framework: None
? Rendering mode: Single Page App
? Deployment target: Server (Node.js hosting)
? Development tools: jsconfig.json (Recommended for VS Code)

For Nuxt.js modules, we recommend choosing axios. Also, although I have not introduced it this time, if you install Vuetify in the UI framework, you can easily use nice components. So, I think it's a perfect framework for those who are not good at front desk. You can install it later, so please add it when you need it.

nuxt server settings

js:nxut/src/nuxt.config.js


server: {
  port: 8080, //Default: 3000
  host: '0.0.0.0' //Default: localhost
},

By default, nuxt is set to start the server on port 3000, localhost. If you change the port number arbitrarily, add the above server setting to nuxt.config.js at the end.

nuxt server start

$ docker-compose up -d nuxt

Building the first nuxt application will take some time. I think it will take about 1 minute at the longest, so please wait a moment and access localhost: 8080. If you can see the top page of nuxt, the development environment is complete.

By the way

Currently I'm implementing it with Docker on Vagrant. This made container builds and hot reloads faster. (I feel like it has become.) I will leave a log for this as well.

Recommended Posts

Build Rails (API) x MySQL x Nuxt.js environment with Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Rails + MySQL environment construction with Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Create Rails 6 + MySQL environment with Docker compose
How to build Rails 6 environment with Docker
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
[Rails] How to build an environment with Docker
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
Build docker environment with WSL
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Build Couchbase local environment with Docker
Build PlantUML environment with VSCode + Docker
Build docker + laravel environment with laradock
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
Rails environment construction with Docker (personal apocalypse)
Build a PureScript development environment with Docker
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
Deploy to heroku with Docker (Rails 6, MySQL)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Edit Mysql with commands in Docker environment
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
Create a MySQL environment with Docker from 0-> 1
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Laravel + MySQL + phpMyadmin environment construction with Docker
Build a Wordpress development environment with Docker
I tried to build an API server with Go (Echo) x MySQL x Docker x Clean Architecture
Build a development environment for Django + MySQL + nginx with Docker Compose
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Build TensorFlow operation check environment with Docker
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
I built a rails environment with docker and mysql, but I got stuck
Build an environment of "API development + API verification using Swagger UI" with Docker
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Build a development environment for Docker + Rails6 + Postgresql
Introducing Rspec with Ruby on Rails x Docker
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
How to build API with GraphQL and Rails
React + Django + Nginx + MySQL environment construction with Docker
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[First team development ②] Build an environment with Docker
Rails Docker environment construction
Docker x Rails 6 (memo)
Rails deploy with Docker
[Docker] Connection with MySQL
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
How to build docker environment with Gradle for intelliJ
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
Easily build a Vue.js environment with Docker + Vue CLI