EC2 ✕ Manual deployment with Docker / docker-compose / Asset compilation

Introduction

Previous article

Building a Docker environment on EC2 (ArgumentError: Missing'secret_key_base', Mysql2 :: Error :: ConnectionError: Access denied for user'root'@ '172.18.0.4' (using password: NO) solution)

I explained the introduction of Docker on EC2.

This time, I will continue to explain ** how to start Docker container on EC2 using Docker and docker-compose on EC2, how to manually deploy Rails application and start the server **, etc. I will.

Premise

(1) Docker and docker-compose must be installed in the development environment

(2) Docker and docker-compose must be installed in the initial settings (environment settings) of EC2.

Development environment

Ruby 2.6.5 Bundler 2.1.4 Rails 6.0.0 MySQL 5.6.50 AWS EC2

Launch a Rails app on EC2

Up to the last time, you have been able to create a DB in Docker on EC2, so all you have to do is start it and deploy it.

However, I was frustrated because I couldn't start it for about 8 hours because I had a hard time starting it here (laughs).

Is #unicorn required?

In my case, I thought that unicorn was indispensable for EC2 deployment, and I saw some articles that could start the server with Docker and unicorn, so I thought that I could go with it.

However, I fell in love with the swamp there. Since the DB was created normally, execute the following in the terminal.

Terminal(EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D

I started the server with this command.

Then I got the following error.

Terminal(EC2)


ArgumentError: directory for
pid=/var/www/app/tmp/pids/unicorn.pid not writable

At first, I thought it was a permission problem, and I thought that the app/tmp/pid directory did not exist, so I tried and errored by referring to the following article. I worked by referring to the following article.

Unicorn error (unicorn.pid not writable (ArgumentError))[Capistrano, Unicorn] ArgumentError: directory for pid =/var/www/myapp/current/shared/tmp/pids/unicorn.pid not writable was not a write permission issueUnicorn cannot be started with unicorn.pid not writable (ArgumentError)

However, none of them went well and I started out ...

If so, I was thinking that there was another cause ... "** I started the container in the first place, so I don't have to bother to start the server with unicorn? **" and started the container.

Terminal(EC2)


#Container creation
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose build
#Create DB in container
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails db:create RAILS_ENV=production
#Perform migration in container
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails db:migrate RAILS_ENV=production
#Start container
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose up -d
# -Can be started in the background by adding the d option
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker ps -a
#Check if status is up

Now I should be deploying ... and when I access http: // Elastic IP: 3000 /, I get angry that there is no database_development.

Creating database_development

I don't know if it's special for me, but I thought it was OK because I created a DB for the production environment with RAILS_ENV = production, but I got an error when I included the one in the development environment, so enter the following did.

Terminal(EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails db:create 
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails db:migrate

The editor also changed the description a little.

docker-compose.yml(Change before)


version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 'password' 
    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'"
#Change the volume part
    volumes:
      - .:/app name
    ports:
      - "3000:3000"
    depends_on:
      - db

docker-compose.yml(After change)


version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 'password' 
    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'"
#Change volumes from app name
    volumes:
      - ./mysql:/var/lib/mysql
    ports:
      - "3000:3000"
    depends_on:
      - db

Now when I save, update, and view it in my browser, I get another error.

1ffcbcf57f6384e2a905697ee4066865.png

Webpacker::Manifest::MissingEntryError

I thought, "Oh, the error has changed!" And tried to solve it, but this was difficult.

The error is "** Browser cannot read JavaScript etc. **".

For that purpose, what to do is to do ** asset compilation ** if the browser can read it, but I needed a little confirmation, and I was addicted to it.

If you come up with rails s etc., please refer to the following article. This article introduces ** How to resolve errors around Webpacker **.

[Rails6] 4 ways to deal with Webpacker :: Manifest :: MissingEntryError

So, ** execute asset compilation **

Perform asset compilation in Docker container

Terminal(EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails assets:precompile RAILS_ENV=production

Compile the assets inside the container and start the container.

Terminal(EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose up

** If you can access http: // Elastic IP: 3000/** It's a success.

References

Automatic deployment with Docker + Rails5.2 + Unicorn + Nginx + capistrano

Webpacker, yarn related errors and solutions that may stumble during Rails6 development

[Rails 6] What to do when Webpacker :: Manifest :: MissingEntryError occurs

Recommended Posts

EC2 ✕ Manual deployment with Docker / docker-compose / Asset compilation
Docker Compact Manual (3: docker-compose)
Deploy with EC2 / Docker / Laravel
Launch Rails on EC2 (manual deployment)
Create Laravel environment with Docker (docker-compose)
Easy deployment with Capistrano + AWS (EC2) + Rails
Deploy your application with VPC + EC2 + Docker.
EC2 on Docker-compose
[Introduction to Docker x ECS] ECS deployment with docker compose up
[Note] Build a Python3 environment with Docker in EC2