DEV Community

Cover image for How to Deploy a Rails Application to AWS with Docker - Part 3

How to Deploy a Rails Application to AWS with Docker - Part 3

Farley Knight on February 29, 2020

Creating a load balancer Load balancers are like the waiters & waitresses of the web server world. Whenever a user wants to look at ...
Collapse
 
igavrysh profile image
Ievgen • Edited

Hi @farleyknight, thx for the guide! I followed your steps but my instance is not starting. When I check the log I see pretty strange error that keeps showing up again and again:

Waiting for MySQL...
DNS fwd/rev mismatch: ec2-54-86-250-45.compute-1.amazonaws.com != ip-172-31-36-241.ec2.internal
no port[s] to connect to
Enter fullscreen mode Exit fullscreen mode

Do you have any ideas why this can happen? If yes, can you please share how did you solve it? If not, maybe you can give me a hint how I can troubleshoot this issue?

Thx a lot for this guide and your help!

Collapse
 
igavrysh profile image
Ievgen • Edited

@farleyknight , the mysql connected and created/migrated db, however the web service is not starting, docker log from aws:

2021-01-30T13:32:00.100-08:00   yarn install v1.22.5
2021-01-30T13:32:00.201-08:00   [1/4] Resolving packages...
2021-01-30T13:32:00.204-08:00   success Already up-to-date.
2021-01-30T13:32:00.207-08:00   Done in 0.12s.
2021-01-30T13:32:01.318-08:00   DNS fwd/rev mismatch: ec2-54-86-250-45.compute-1.amazonaws.com != ip-172-31-36-241.ec2.internal
2021-01-30T13:32:01.319-08:00   ec2-54-86-250-45.compute-1.amazonaws.com [172.31.36.241] 3306 (mysql) open
2021-01-30T13:32:01.320-08:00   MySQL is up and running!
2021-01-30T13:32:05.197-08:00   Database 'the-greatest-rails-app-ever_test' already exists
2021-01-30T13:32:05.410-08:00   Created database 'the-greatest-rails-app-ever_development'
2021-01-30T13:32:05.410-08:00   == 20210129091928 CreateBlogPosts: migrating ==================================
2021-01-30T13:32:05.410-08:00   -- create_table(:blog_posts)
2021-01-30T13:32:05.410-08:00   -> 0.0259s
2021-01-30T13:32:05.410-08:00   == 20210129091928 CreateBlogPosts: migrated (0.0259s) =========================
2021-01-30T13:32:05.415-08:00   MySQL database has been created & migrated!
2021-01-30T13:32:05.415-08:00   test: 1
2021-01-30T13:32:05.420-08:00   test: 2
Enter fullscreen mode Exit fullscreen mode

It seems like aws container for unknown reason is stuck executing this command:

bundle exec rails server -b 0.0.0.0 -p 8080
Enter fullscreen mode Exit fullscreen mode

Do you have any idea why this can happen?

P.S. I added echo before bundle exec to make sure that container finished running previous lines:

# If the database exists, migrate. Otherwise setup (create and migrate)
bundle exec rake db:migrate 2>/dev/null || bundle exec rake db:create db:migrate
echo "MySQL database has been created & migrated!"

echo 'test: 1'
# Remove a potentially pre-existing server.pid for Rails.
rm -f tmp/pids/server.pid
echo 'test: 2'
# Run the Rails server
bundle exec rails server -b 0.0.0.0 -p 8080
Enter fullscreen mode Exit fullscreen mode
Collapse
 
igavrysh profile image
Ievgen

To anybody who is having the same problem as I had, it turns out that ruby on rails on docker container application in my case was running on random port (e.g. 32768, check screens below). This happened because in container settings the port was set to be 0, that means just to use random Host Port. Because of this, simple ping to 80 or 8080 port was not passing thru instance running container.

I figured that out by ssh-ing to instance that runs container (mentioned in this article above under Test the SSH connection section) and running docker ps command (see the screen below)

screen1

Simple check I used to see if the request is forwarded to container or not is to execute curl localhost:32768 from instance running container (see the screen below)

screen2

So to sum up, the app was working, but I was checking a wrong port (80 or 8080). What I should have done is just to open instance url:Host Port, where Host Port was randomly selected and it was not selected to be 80 or 8080.

To fix this issue you should modify container routing to explicitly set Host Port to be equal to 80 or 8080 (or any other port you choose to run web application)

Collapse
 
manisharma7575 profile image
Manish Sharma

How did you solved database connection issue?

Collapse
 
manisharma7575 profile image
Manish Sharma

@farleyknight I am also facing the similiar problem. Can you please help me with this?

Collapse
 
lucasocon profile image
Lucas Ocon

Hi @farleyknight I ran my cluster and my instance is working fine, the only issue that I have is that I can't connect to the DB on RDS, I tried with PgAdmin and I got a timeout error, maybe is something related to the security group?

Collapse
 
igavrysh profile image
Ievgen • Edited

@lucasocon , did you manage to start application on cluster? e.g. can you open blog site and create a separate post with web UI?
I managed to connect to DB with DataGrip only when I choose Standard create and Public Access: Yes when creating db. Still I am not able to launch application due to I guess MySQL connection issue, see my post above.
Any ideas how this can be resolved?
https://dev-to-uploads.s3.amazonaws.com/i/wvwv65squ68fklwgzo82.png
https://dev-to-uploads.s3.amazonaws.com/i/4zkmkjdb1dtfulm4da5r.png

Collapse
 
igavrysh profile image
Ievgen

This hypothesis was wrong, I added console log statement before and after mysql app initialization and saw that the app was not frozen and passed mysql initialization phase.

The real problem for the issue I described here was Host Port = 0 which meant that container is going to use random port as a Host Port in traffic routing. Check my replies above if you want to know more details why this happened and how to fix it.

Collapse
 
siddhantmohan profile image
Siddhant

I followed tthis but somehow my task is getting automatically shutdown and restarted again and again, cloudfront logs are:

2020-07-13T01:54:35.600+05:30
yarn install v1.22.4

2020-07-13T01:54:35.917+05:30
[1/4] Resolving packages...

2020-07-13T01:54:37.333+05:30
success Already up-to-date.

2020-07-13T01:54:37.337+05:30
Done in 1.75s.

2020-07-13T01:54:38.325+05:30
yarn install v1.22.4

2020-07-13T01:54:38.628+05:30
[1/4] Resolving packages...

2020-07-13T01:54:40.119+05:30
[2/4] Fetching packages...

2020-07-13T01:54:42.023+05:30
info fsevents@2.1.3: The platform "linux" is incompatible with this module.

2020-07-13T01:54:42.023+05:30
info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.

2020-07-13T01:54:42.025+05:30
info fsevents@1.2.13: The platform "linux" is incompatible with this module.

2020-07-13T01:54:42.025+05:30
info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.

2020-07-13T01:54:42.085+05:30
[3/4] Linking dependencies...

2020-07-13T01:54:42.093+05:30
warning " > webpack-dev-server@3.11.0" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0".

2020-07-13T01:54:42.093+05:30
warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has unmet peer dependency "webpack@^4.0.0".

2020-07-13T01:54:47.701+05:30
[4/4] Building fresh packages...

2020-07-13T01:54:47.730+05:30
Done in 9.41s.

2020-07-13T01:55:02.519+05:30
Everything's up-to-date. Nothing to do

2020-07-13T01:55:06.280+05:30
D, [2020-07-12T20:25:06.280705 #220] DEBUG -- : [1m[35m (1.0ms)[0m [1m[34mSELECT pg_try_advisory_lock(2903025111434932890)[0m

2020-07-13T01:55:06.300+05:30
D, [2020-07-12T20:25:06.300771 #220] DEBUG -- : [1m[35m (1.2ms)[0m [1m[34mSELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC[0m

2020-07-13T01:55:06.311+05:30
D, [2020-07-12T20:25:06.311509 #220] DEBUG -- : [1m[36mActiveRecord::InternalMetadata Load (1.1ms)[0m [1m[34mSELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2[0m [["key", "environment"], ["LIMIT", 1]]

2020-07-13T01:55:06.318+05:30
D, [2020-07-12T20:25:06.318450 #220] DEBUG -- : [1m[35m (1.1ms)[0m [1m[34mSELECT pg_advisory_unlock(2903025111434932890)[0m

2020-07-13T01:55:06.336+05:30
database has been migrated!

2020-07-13T01:55:09.226+05:30
=> Booting Puma

2020-07-13T01:55:09.226+05:30
=> Rails 6.0.3.2 application starting in staging

2020-07-13T01:55:09.226+05:30
=> Run rails server --help for more startup options

2020-07-13T01:55:10.939+05:30
Puma starting in single mode...

2020-07-13T01:55:10.939+05:30

  • Version 4.3.5 (ruby 2.7.1-p83), codename: Mysterious Traveller

2020-07-13T01:55:10.939+05:30

  • Min threads: 5, max threads: 5

2020-07-13T01:55:10.939+05:30

  • Environment: staging

2020-07-13T01:55:10.939+05:30

  • Listening on tcp://0.0.0.0:8080

2020-07-13T01:55:10.939+05:30
Use Ctrl-C to stop

2020-07-13T01:55:17.332+05:30
I, [2020-07-12T20:25:17.329063 #224] INFO -- : [f033624b-8770-4112-8b05-bb7f3566e0b8] Started GET "/" for 172.31.12.192 at 2020-07-12 20:25:17 +0000

2020-07-13T01:55:17.332+05:30
I, [2020-07-12T20:25:17.330961 #224] INFO -- : [f033624b-8770-4112-8b05-bb7f3566e0b8] Processing by StaticsController#home as HTML

2020-07-13T01:55:17.337+05:30
I, [2020-07-12T20:25:17.337764 #224] INFO -- : [f033624b-8770-4112-8b05-bb7f3566e0b8] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:55:17.338+05:30
I, [2020-07-12T20:25:17.338120 #224] INFO -- : [f033624b-8770-4112-8b05-bb7f3566e0b8] Completed 302 Found in 7ms (Allocations: 536)

2020-07-13T01:55:17.356+05:30
I, [2020-07-12T20:25:17.356581 #224] INFO -- : [4bce0e2c-2eb9-4067-82ec-c8d46d2f6acb] Started GET "/" for 172.31.37.118 at 2020-07-12 20:25:17 +0000

2020-07-13T01:55:17.357+05:30
I, [2020-07-12T20:25:17.357318 #224] INFO -- : [4bce0e2c-2eb9-4067-82ec-c8d46d2f6acb] Processing by StaticsController#home as HTML

2020-07-13T01:55:17.358+05:30
I, [2020-07-12T20:25:17.358182 #224] INFO -- : [4bce0e2c-2eb9-4067-82ec-c8d46d2f6acb] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:55:17.358+05:30
I, [2020-07-12T20:25:17.358390 #224] INFO -- : [4bce0e2c-2eb9-4067-82ec-c8d46d2f6acb] Completed 302 Found in 1ms (Allocations: 371)

2020-07-13T01:55:47.358+05:30
I, [2020-07-12T20:25:47.358200 #224] INFO -- : [7d482232-6a1d-4d7f-b279-4a508d6c8798] Started GET "/" for 172.31.12.192 at 2020-07-12 20:25:47 +0000

2020-07-13T01:55:47.359+05:30
I, [2020-07-12T20:25:47.359128 #224] INFO -- : [7d482232-6a1d-4d7f-b279-4a508d6c8798] Processing by StaticsController#home as HTML

2020-07-13T01:55:47.360+05:30
I, [2020-07-12T20:25:47.360016 #224] INFO -- : [7d482232-6a1d-4d7f-b279-4a508d6c8798] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:55:47.360+05:30
I, [2020-07-12T20:25:47.360237 #224] INFO -- : [7d482232-6a1d-4d7f-b279-4a508d6c8798] Completed 302 Found in 1ms (Allocations: 371)

2020-07-13T01:55:47.386+05:30
I, [2020-07-12T20:25:47.386789 #224] INFO -- : [ad51aec8-ca24-4df3-9c4a-fe0b270f4a9c] Started GET "/" for 172.31.37.118 at 2020-07-12 20:25:47 +0000

2020-07-13T01:55:47.387+05:30
I, [2020-07-12T20:25:47.387553 #224] INFO -- : [ad51aec8-ca24-4df3-9c4a-fe0b270f4a9c] Processing by StaticsController#home as HTML

2020-07-13T01:55:47.388+05:30
I, [2020-07-12T20:25:47.388546 #224] INFO -- : [ad51aec8-ca24-4df3-9c4a-fe0b270f4a9c] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:55:47.388+05:30
I, [2020-07-12T20:25:47.388751 #224] INFO -- : [ad51aec8-ca24-4df3-9c4a-fe0b270f4a9c] Completed 302 Found in 1ms (Allocations: 371)

2020-07-13T01:55:47.557+05:30
D, [2020-07-12T20:25:47.557215 #224] DEBUG -- [Bugsnag]: Request to sessions.bugsnag.com completed, status: 202

2020-07-13T01:56:17.391+05:30
I, [2020-07-12T20:26:17.389284 #224] INFO -- : [fa10091b-ba10-40a7-bca8-c354367d6224] Started GET "/" for 172.31.12.192 at 2020-07-12 20:26:17 +0000

2020-07-13T01:56:17.391+05:30
I, [2020-07-12T20:26:17.389995 #224] INFO -- : [fa10091b-ba10-40a7-bca8-c354367d6224] Processing by StaticsController#home as HTML

2020-07-13T01:56:17.391+05:30
I, [2020-07-12T20:26:17.390748 #224] INFO -- : [fa10091b-ba10-40a7-bca8-c354367d6224] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:56:17.391+05:30
I, [2020-07-12T20:26:17.390890 #224] INFO -- : [fa10091b-ba10-40a7-bca8-c354367d6224] Completed 302 Found in 1ms (Allocations: 371)

2020-07-13T01:56:17.417+05:30
I, [2020-07-12T20:26:17.417779 #224] INFO -- : [77bd4b5c-21ad-4896-b1ff-3a50c4f1351d] Started GET "/" for 172.31.37.118 at 2020-07-12 20:26:17 +0000

2020-07-13T01:56:17.418+05:30
I, [2020-07-12T20:26:17.418630 #224] INFO -- : [77bd4b5c-21ad-4896-b1ff-3a50c4f1351d] Processing by StaticsController#home as HTML

2020-07-13T01:56:17.419+05:30
I, [2020-07-12T20:26:17.419514 #224] INFO -- : [77bd4b5c-21ad-4896-b1ff-3a50c4f1351d] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:56:17.419+05:30
I, [2020-07-12T20:26:17.419734 #224] INFO -- : [77bd4b5c-21ad-4896-b1ff-3a50c4f1351d] Completed 302 Found in 1ms (Allocations: 370)

2020-07-13T01:56:17.551+05:30
D, [2020-07-12T20:26:17.550908 #224] DEBUG -- [Bugsnag]: Request to sessions.bugsnag.com completed, status: 202

2020-07-13T01:56:47.396+05:30
I, [2020-07-12T20:26:47.396093 #224] INFO -- : [9668a89d-5422-4a72-a15a-9c4ef62f9b21] Started GET "/" for 172.31.12.192 at 2020-07-12 20:26:47 +0000

2020-07-13T01:56:47.397+05:30
I, [2020-07-12T20:26:47.397029 #224] INFO -- : [9668a89d-5422-4a72-a15a-9c4ef62f9b21] Processing by StaticsController#home as HTML

2020-07-13T01:56:47.397+05:30
I, [2020-07-12T20:26:47.397894 #224] INFO -- : [9668a89d-5422-4a72-a15a-9c4ef62f9b21] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:56:47.398+05:30
I, [2020-07-12T20:26:47.398179 #224] INFO -- : [9668a89d-5422-4a72-a15a-9c4ef62f9b21] Completed 302 Found in 1ms (Allocations: 370)

2020-07-13T01:56:47.445+05:30
I, [2020-07-12T20:26:47.445049 #224] INFO -- : [66f07b83-37e8-4d51-b7b7-bd5bc0ffb581] Started GET "/" for 172.31.37.118 at 2020-07-12 20:26:47 +0000

2020-07-13T01:56:47.446+05:30
I, [2020-07-12T20:26:47.445939 #224] INFO -- : [66f07b83-37e8-4d51-b7b7-bd5bc0ffb581] Processing by StaticsController#home as HTML

2020-07-13T01:56:47.446+05:30
I, [2020-07-12T20:26:47.446826 #224] INFO -- : [66f07b83-37e8-4d51-b7b7-bd5bc0ffb581] Redirected to 172.31.35.107:32811/users/sign_in

2020-07-13T01:56:47.447+05:30
I, [2020-07-12T20:26:47.447048 #224] INFO -- : [66f07b83-37e8-4d51-b7b7-bd5bc0ffb581] Completed 302 Found in 1ms (Allocations: 370)

2020-07-13T01:56:47.570+05:30
D, [2020-07-12T20:26:47.570039 #224] DEBUG -- [Bugsnag]: Request to sessions.bugsnag.com completed, status: 202

Collapse
 
farleyknight profile image
Farley Knight

Apologies Siddhant for the late reply. I can't quite tell what the problem is from these logs. Do you want to DM me on dev.to and maybe I can help you one-on-one? Let me know. Thanks!