Docker初心者です。
現在twitterAPIを利用してrubyで投稿するサンプルを RubyでTwitter Botを作ってHerokuで動かす - あかんわ を参考にDockerを利用して作成、動作確認をしていますが、
docker-compose run app bundle exec ruby tweer.rb
を実行してもtweetされない- 必要ファイル作成後に
docker-compose build
実行、docker-compose up
を実行すると下記が表示される
bash
1Recreating xxx_app_1 ... done 2Attaching to xxx_app_1 3app_1 | The Gemfile's dependencies are satisfied 4xxx_app_1 exited with code 0
- 上記URLのアプリをDockerを使用せずにコピペして作成、
bundle exec ruby tweet.rb
を実行するとtweetがされている
といった状況で原因が全くわかりません(m_ m)
もしわかる方がいらっしゃればご教授いただきますようよろしくお願いいたします(m _m)
動作環境
OS: mac
Ruby: 2.6.5
gem | ver |
---|---|
7.0.0 | |
Sinatra | 2.0.8.1 |
エラー内容
- terminalで
docker-compose run app bundle exec ruby tweet.rb
を実行してもtwitterに投稿されない
期待する動作
- terminalで
bash
1docker-compose run app bundle exec ruby tweet.rb 2# or 3docker-compose up
を実行するとtwitterに投稿されるようにしたい
該当しそうなコード(docker-compose run app bundle exec ruby tweet.rb
を実行するためだけのファイル)
- ディレクトリ構造
. ├── Dockerfile ├── Gemfile ├── Gemfile.lock ├── app.rb ├── config.ru ├── docker-compose.yml ├── docker-entrypoint.sh ├── readme.md ├── tweet.rb └── twitter_api.env
- Dockerfile
docker
1FROM ruby:2.6.5-stretch 2 3WORKDIR /app 4 5COPY Gemfile Gemfile.lock ./ 6RUN bundle install 7 8COPY docker-entrypoint.sh / 9RUN chmod +x /docker-entrypoint.sh 10ENTRYPOINT ["/docker-entrypoint.sh"] 11 12COPY . ./ 13 14CMD ["bundle", "exec", "ruby", "tweet.rb"] 15EXPOSE 4567
- docker-compose.yml
docker
1version: '3' 2 3services: 4 app: 5 build: . 6 7 env_file: twitter_api.env 8 ports: 9 - "10080:4567" 10 11 tty: true 12 stdin_open: true 13 14 volumes: 15 - .:/app 16 - app-bundle:/usr/local/bundle 17 18volumes: 19 app-bundle:
- docker-entrypoint.sh
bash
1#!/bin/bash 2 3set -eu 4 5# 必要であればBundlerでgemをインストールする 6bundle check || bundle install 7 8exec "$@" 9
- Gemfile
ruby
1# frozen_string_literal: true 2 3source "https://rubygems.org" 4 5git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } 6 7gem "sinatra", "~> 2.0.5" 8gem 'twitter' 9gem 'dotenv'
- Gemfile.lcok
ruby
1GEM 2 remote: https://rubygems.org/ 3 specs: 4 addressable (2.7.0) 5 public_suffix (>= 2.0.2, < 5.0) 6 buftok (0.2.0) 7 byebug (11.1.1) 8 coderay (1.1.2) 9 domain_name (0.5.20190701) 10 unf (>= 0.0.5, < 1.0.0) 11 dotenv (2.7.5) 12 equalizer (0.0.11) 13 ffi (1.12.2) 14 ffi-compiler (1.0.1) 15 ffi (>= 1.0.0) 16 rake 17 http (4.3.0) 18 addressable (~> 2.3) 19 http-cookie (~> 1.0) 20 http-form_data (~> 2.2) 21 http-parser (~> 1.2.0) 22 http-cookie (1.0.3) 23 domain_name (~> 0.5) 24 http-form_data (2.2.0) 25 http-parser (1.2.1) 26 ffi-compiler (>= 1.0, < 2.0) 27 http_parser.rb (0.6.0) 28 memoizable (0.4.2) 29 thread_safe (~> 0.3, >= 0.3.1) 30 method_source (0.9.2) 31 multipart-post (2.1.1) 32 mustermann (1.1.1) 33 ruby2_keywords (~> 0.0.1) 34 naught (1.1.0) 35 pry (0.12.2) 36 coderay (~> 1.1.0) 37 method_source (~> 0.9.0) 38 pry-byebug (3.8.0) 39 byebug (~> 11.0) 40 pry (~> 0.10) 41 public_suffix (4.0.3) 42 rack (2.2.2) 43 rack-protection (2.0.8.1) 44 rack 45 rake (13.0.1) 46 ruby2_keywords (0.0.2) 47 simple_oauth (0.3.1) 48 sinatra (2.0.8.1) 49 mustermann (~> 1.0) 50 rack (~> 2.0) 51 rack-protection (= 2.0.8.1) 52 tilt (~> 2.0) 53 thread_safe (0.3.6) 54 tilt (2.0.10) 55 twitter (7.0.0) 56 addressable (~> 2.3) 57 buftok (~> 0.2.0) 58 equalizer (~> 0.0.11) 59 http (~> 4.0) 60 http-form_data (~> 2.0) 61 http_parser.rb (~> 0.6.0) 62 memoizable (~> 0.4.0) 63 multipart-post (~> 2.0) 64 naught (~> 1.0) 65 simple_oauth (~> 0.3.0) 66 unf (0.1.4) 67 unf_ext 68 unf_ext (0.0.7.6) 69 70PLATFORMS 71 ruby 72 73DEPENDENCIES 74 dotenv 75 pry-byebug 76 sinatra (~> 2.0.5) 77 twitter 78 79BUNDLED WITH 80 1.17.2 81
- tweet.rb
ruby
1require 'dotenv' 2require 'twitter' 3 4class Tweet 5 6 def initialize 7 @text = ["ちゃうねん", 8 "せやないねん", 9 "どないやねん", 10 "ええねん", 11 "これや!!", 12 "いけるで!!", 13 "こっからや!!"] 14 15 @client = Twitter::REST::Client.new do |config| 16 config.consumer_key = ENV['TWITTER_CONSUMER_API_KEY'] 17 config.consumer_secret = ENV['YOUR_CONSUMER_API_SECRET_KEY'] 18 config.access_token = ENV['TWITTER_ACCESS_TOKEN'] 19 config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET'] 20 end 21 end 22 23 def random_tweet 24 tweet = @text[rand(@text.length)] 25 update(tweet) 26 end 27 28 private 29 30 def update(tweet) 31 begin 32 @client.update(tweet) 33 rescue => e 34 nil #TODO 35 end 36 end 37 38end 39 40# random_tweetを実行する 41if __FILE__ == $0 42 Tweet.new.random_tweet 43end 44