🤖

[やってみた] AWS上で作成したDockerコンテナをHerokuで表示する

4 min read

0. はじめに

  1. 勉強のためにお試しで作業してみました。
  2. データベースは生成していますが、使用はしていません。

1. AWS Cloud9

Item Value
Name Docker
Description <none>
Environment typeInfo Create a new EC2 instance for environment (direct access)
Instance type t2.micro (1 GiB RAM + 1 vCPU)
Platform Ubuntu Server 18.04 LTS
Cost-saving setting After 30 minitu (default)
sudo apt-get update && sudo apt-get upgrade -y

2. Resize an Amazon EBS volume

https://docs.aws.amazon.com/cloud9/latest/user-guide/move-environment.html#move-environment-resize
SIZE=30

INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id)

VOLUMEID=$(aws ec2 describe-instances \
  --instance-id $INSTANCEID \
  --query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \
  --output text)  
  
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE

while [ "$(aws ec2 describe-volumes-modifications \
    --volume-id $VOLUMEID \
    --filters Name=modification-state,Values="optimizing","completed" \
    --query "length(VolumesModifications)"\
    --output text)" != "1" ]; do
  sleep 1
done

sudo growpart /dev/xvda 1
sudo resize2fs /dev/xvda1

df -h

3. Docker & Rails

docker version

sudo apt-get install -y docker-compose
docker-compose -v
mkdir ~/environment/hello_app
cd ~/environment/hello_app

tee Dockerfile << EOF
FROM ruby:2.5
RUN apt-get update && apt-get install -y  \\
    nodejs \\
    postgresql-client
    
WORKDIR /hello_app
COPY Gemfile Gemfile.lock /hello_app/
RUN bundle install

COPY . .
CMD ["rails", "s", "-b", "0.0.0.0"]
EOF

tee Gemfile << EOF
source 'https://rubygems.org'
gem 'rails', '~>5.2'
EOF

touch Gemfile.lock

tee docker-compose.yml << EOF
version: '3'

services:
  web:
    build: .
    ports:
      - "8080:3000"
    volumes:
      - .:/hello_app
    environment:
      - 'POSTGRES_PASSWORD=postgres'
    depends_on:
      - db
      
  db:
    image: postgres
    ports:
      - '5432:5432'
    volumes:
      - postgresql-data:/var/lib/postgresql/data
    environment:
      - 'POSTGRES_PASSWORD=postgres'
      
volumes:
  postgresql-data:
    driver: local
EOF

tee .dockerignore << EOF
tmp/pids/server.pid
EOF

docker-compose run web rails new . --force --no-deps --database=postgresql

sudo chown -R ubuntu:ubuntu .

tee config/database.yml << EOF
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  port: 5432
  password: <%= ENV.fetch("POSTGRES_PASSWORD") { 5 } %> 
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: hello_app_development

test:
  <<: *default
  database: hello_app_test

production:
  url: <%= ENV['DATABASE_URL'] %>
EOF

docker-compose up --build -d
docker-compose ps

docker-compose run web rake db:create
docker-compose run web rails db:migrate


4. Heroku

https://jp.heroku.com/home
sudo snap install --classic heroku

heroku login --interactive

sudo apt install -y gnupg2 pass

heroku container:login
heroku create

heroku config:set POSTGRES_PASSWORD=postgres

heroku container:push web
heroku container:release web
heroku addons:create heroku-postgresql:hobby-dev
heroku run rails db:migrate

heroku apps:info


5. おわり

  1. データベースを利用したコードも試す。
  2. CIサービスを試してみたい。