Comment puis-je utiliser l'environnement Docker avec une application qui utilise Nuxt et Rails? Pour référence, je vais vous expliquer les fichiers et les commandes jusqu'à la construction étape par étape!
Cette fois, je voudrais créer un environnement de développement d'applications appelé feeder
.
#Dossier préparé par vous-même
|-web/
| |--dossier d'application
| |--Dockerfile
|--back/
| |--dossier d'application
| |--Dockerfile
| |--Gemfile
| |--Gemfile.lock #Fichier vide
|--docker-compose.yml
|--.git
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'
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
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'
Création d'image
$ docker-compose build
Génération d'applications Nuxt
$ docker-compose run --rm web yarn create nuxt-app
Génération d'applications Rails
$ docker-compose run --rm back rails new . -f -d mysql --api
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'] %>
Créer une base de données
$ docker-compose build
$ docker-compose run --rm back rails db:create
Démarrez le serveur (démarrez le conteneur)
$ docker-compose up -d
$ docker-compose exec service name Script de commande à exécuter
$ docker-compose exec back sh
$ docker-compose exec web sh