www-viget-com-1hyo6b.txt (16146B)
1 [1] Skip to Main Content 2 [2] Viget 3 4 • [3] Work 5 • [4] Services 6 • [5] Articles 7 • [6] Careers 8 • [7] Contact 9 • Open Menu 10 11 Navigation 12 13 [9] Viget Close 14 15 • Practice 16 • [11] Work 17 • [12] Services 18 • [13] Articles 19 20 We’re a full-service digital agency that’s been helping clients make lasting 21 change since 1999. 22 23 [14] Contact Us 24 25 People 26 27 • [15]Company 28 • [16]Careers 29 • [17]Code of Ethics 30 • [18]Diversity & Inclusion 31 32 More 33 34 • [19]Pointless Corp. 35 • [20]Explorations 36 • [21]Code at Viget 37 38 Featured 39 40 [22] 41 Read the Article: Building the Future, One Intern at a Time 42 43 Newsletter 44 45 Building the Future, One Intern at a Time 46 47 [23] 48 Read the Article: Some Thoughts after a Major Ruby on Rails Upgrade 49 50 Article 51 52 Some Thoughts after a Major Ruby on Rails Upgrade 53 54 Docker + Rails: Solutions to Common Hurdles 55 56 [eyJidWNrZXQiOiJ2Z3QtdmlnZXRjb20tYW] 57 58 • [24]Home 59 • [25]Articles 60 • [26]Docker + Rails: Solutions to Common Hurdles 61 62 [27] Subscribe (opens in a new window) 63 Share 64 65 • [29] Share this page 66 • [30] Share this page 67 • [31] Post this page 68 69 Eli Fatsi, Former Development Director 70 71 Article Categories: [32] #Code, [33] #Back-end Engineering 72 73 Posted on March 30, 2021 74 75 • [34] 76 Share 77 • [35] 78 Share 79 • [36] 80 Post 81 82 Tips n Tricks for working with Rails applications in Docker 83 84 T i p s n T r i c k s f o r w o r k i n g w i t h R a i l s a p p l i c a t i o 85 n s i n D o c k e r 86 87 This is the follow on post to an earlier post: [37]Docker: Right for Us. Right 88 for You? 89 90 Docker has made a number of large problems go away, and replaced them with 91 (usually) (hopefully) a smaller set of its own problems. Sometimes it's not 92 worth it to make that sacrifice, but it often is for us, and here are the 93 tricks we've picked up for working with Rails sites and Docker Compose: 94 95 Rails Tips & Tricks [38]# 96 97 db host 98 99 Trying to docker-ify a Rails app and running into this? 100 101 could not connect to server: No such file or directory 102 Is the server running locally and accepting 103 connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? 104 105 Make sure you have a db service defined in docker-compose.yml, and add host: db 106 to your database.yml file. All of the services you define in the compose file 107 become network-addressable names to each other. So adding host: db tells Rails 108 that the hostname of the database server is db, which should point to your 109 database service. 110 111 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 112 113 binding.pry 114 115 AKA the one tool I use the most by far I gotta have it. TLDR on the solution: 116 [39]https://stackoverflow.com/questions/35211638/ 117 how-to-debug-a-rails-app-in-docker-with-pry/37264588#37264588 118 119 The gist is that you need to add some Docker Compose flags to the service you 120 want to run a pry inside, and then docker attach [running_container_name] after 121 you've started the service container. 122 123 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 124 125 save_and_open_page 126 127 If you're testing w/ Capybara and want to get a glimpse of your site while a 128 feature test is running, then Docker is going to make that difficult for you. 129 TLDR - [40]https://github.com/copiousfreetime/launchy/issues/108# 130 issuecomment-319644326 131 132 And the gist? You need to configure a volume so screenshots saved on the Docker 133 container are accessible to the host machine. You then need to rig up guard 134 locally to watch the volume directory and automatically open any HTML files 135 that land there. 136 137 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 138 139 cacheing bundler and npm builds 140 141 If you don't configure this properly, you could find yourself re-installing 142 dependencies every time you spin up your containers. We lean on persisted 143 volumes for this, and pick up the log and tmp directories while we're at it. 144 like so: 145 146 - services 147 - app: 148 entrypoint: ./.docker-config/entrypoint.sh 149 volumes: 150 - ./node_modules:/usr/src/app/node_modules 151 - gems:/usr/local/bundle 152 - log:/usr/src/app/log 153 - tmp:/usr/src/app/tmp 154 155 I'll also point out that we prefer to move the dependency installation steps 156 into an entrypoint script, instead of within the Dockerfile build definition. 157 This means that we're rarely rebuilding our baseline image (unless something 158 deeper level changes, like needing an additional runtime available), and our 159 dependencies are kept up to date everytime we cycle the containers. Here's an 160 example of an entrypoint script for an app with Ruby gems and NPM packages: 161 162 #!/bin/bash 163 164 set -e 165 166 # Ensure PID is reset. This can happen if docker isn't cleanly shut down. 167 rm -rf /usr/src/app/tmp/pids 168 169 # Verify node_modules are up to date 170 yarn install --silent 171 172 # Verify gems are up to date 173 if ! bundle check > /dev/null; then 174 echo "Gems dependencies are out of date. Installing..." 175 bundle install 176 fi 177 178 exec "$@" 179 180 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 181 182 asset performance 183 184 Out of the box, sites that run perfectly quickly locally would take a lonnnnng 185 time to respond to local asset requests. One solution we settled on was to add 186 :delegated to the volume dedicated to syncing the codebase. For example: 187 188 app: 189 image: ruby:3.0 190 command: rails server -p 3000 -b '0.0.0.0' 191 ports: 192 - 3000:3000 193 volumes: 194 # Ensure changes to the codebase are picked up by the docker container 195 # The `:delegated` here is a critical config for speedy development 196 - .:/usr/src/app:delegated 197 198 • The docs have a lot to say about this if you want to learn more: [41]http:/ 199 /docs.docker.oeynet.com/docker-for-mac/osxfs-caching/ 200 • And this StackOverflow answer has a good recap in case that docs link goes 201 down: [42]https://stackoverflow.com/a/63437557/1655658 202 203 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 204 205 Running bash 206 207 This one took me a while to figure out as I was learning the difference between 208 a running Docker container and a service and an image and a volume. Once I had 209 a clearer picture of how Docker (and Docker Compose) worked, opening up a shell 210 into a running thing stopped being tricky. Here's what I've picked up though, 211 hopefully these words mean something to you: 212 213 If you have a running container based off of a docker-compose service, you can 214 bash into it like so: 215 216 docker-compose exec [name-of-service] bash 217 218 If you have a non-running container based off of a docker-compose service, you 219 can bash into it like so: 220 221 docker-compose run --rm [name-of-service] bash 222 223 The difference here is exec vs run --rm. You can probably guess what each does. 224 The --rm flag is important so that the newly spun up containers don't hang 225 around forever after you close out of the session. 226 227 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 228 229 importing database dumps 230 231 If you're running something like PostgreSQL within Docker, it becomes a bit 232 trickier to psql your way in from a local command line. As is often the case 233 with Docker, there are a few ways to get the job done here, and the one that's 234 right for you is dependent on what you're trying to do. 235 236 One path is to requiring you to expose a port and run commands through that 237 port. This can be done in a docker-compose.yml: 238 239 services: 240 db: 241 image: mdillon/postgis:11-alpine 242 ports: 243 # Expose Postgres to host networking (your computer) at port 5433. 244 # This prevents conflicts with Postgres installed on your machine 245 # while still allowing the database to be browsed at port 5432 246 - 5433:5432 247 volumes: 248 - db-data:/var/lib/postgresql/data 249 250 Another option is to define a new volume within your database service which 251 makes a database dump on your local machine accessible to the running Docker 252 container. If you had a database dump stored within your project directory at . 253 /local/db-backups, you could make it accessible within Docker like so: 254 255 services: 256 db: 257 image: mdillon/postgis:11-alpine 258 volumes: 259 - db-data:/var/lib/postgresql/data 260 - ./local/db-backups:/app/db-backups 261 262 Then, bash into the running container and have at it with all your usual psql 263 moves. 264 265 Note: I'm including the db-data volume definition to point out that we always 266 have a persistent volume configured for database services so the data persists 267 when the containers are wound down. 268 269 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 270 271 Using local SSH credentials on Docker container 272 273 We have a lot of deploy processes that lean on SSH private keys for 274 authorization. If you attempt to run one of those commands in a running Docker 275 container, it'll fail due to it's sandboxed environment not being able to reach 276 out into your local machine for the credentials. This is our Docker Compose 277 solution for that: 278 279 services: 280 app: 281 ... 282 volumes: 283 - type: bind 284 source: /run/host-services/ssh-auth.sock 285 target: /run/host-services/ssh-auth.sock 286 287 The End [43]# 288 289 That's about all I could recall now that it's been ~1.5 years since digging in 290 on my own Docker journey. Hopefully something in there was useful to you. If 291 you have another tip or trick that solved an early challenge, drop a line in 292 the comments below! 293 294 Related Articles 295 296 • [44] 297 Some Thoughts after a Major Ruby on Rails Upgrade 298 299 Article 300 301 Some Thoughts after a Major Ruby on Rails Upgrade 302 303 Noah Over 304 305 • [45] 306 Setting up a Python Project Using asdf, PDM, and Ruff 307 308 Article 309 310 Setting up a Python Project Using asdf, PDM, and Ruff 311 312 Danny Brown 313 314 • [46] 315 Craft 5: What It Means For Super Table Page Builders 316 317 Article 318 319 Craft 5: What It Means For Super Table Page Builders 320 321 Max Myers 322 323 The Viget Newsletter 324 325 Nobody likes popups, so we waited until now to recommend our newsletter, 326 featuring thoughts, opinions, and tools for building a better digital world. 327 [47]Read the current issue. 328 329 [48]Subscribe Here (opens in new window) 330 331 Site Footer 332 333 Have an unsolvable problem or audacious idea? 334 335 Let’s get to work 336 337 [49] Contact Us [50] [email protected] [51] 703.891.0670 338 339 • Practice 340 • [52]Work 341 • [53]Services 342 • [54]Articles 343 344 • People 345 • [55]Company 346 • [56]Careers 347 • [57]Code of Ethics 348 • [58]Diversity & Inclusion 349 350 • More 351 • [59]Pointless Corp. 352 • [60]Explorations 353 • [61]Code at Viget 354 355 Sign Up For Our Newsletter 356 357 A curated periodical featuring thoughts, opinions, and tools for building a 358 better digital world. 359 360 [62] Check it out 361 362 Social Links 363 364 [63] Viget 365 366 • [64] 367 • [65] 368 • [66] 369 • [67] 370 • [68] 371 • [69] 372 373 Viget rhymes with 'dig it'. Click here to hear how we say it. 374 375 Office Locations 376 377 • [71]Washington, DC Metro 378 • [72]Durham, NC 379 • [73]Boulder, CO 380 • [74]Chattanooga, TN 381 382 © 1999 – 2024 Viget Labs, LLC. [75]Terms [76]Privacy [77]MRF 383 384 385 References: 386 387 [1] https://www.viget.com/articles/docker-rails-solutions-to-common-hurdles/#content 388 [2] https://www.viget.com/ 389 [3] https://www.viget.com/work/ 390 [4] https://www.viget.com/services/ 391 [5] https://www.viget.com/articles/ 392 [6] https://www.viget.com/careers/ 393 [7] https://www.viget.com/contact/ 394 [9] https://www.viget.com/ 395 [11] https://www.viget.com/work/ 396 [12] https://www.viget.com/services/ 397 [13] https://www.viget.com/articles/ 398 [14] https://www.viget.com/contact/ 399 [15] https://www.viget.com/about/ 400 [16] https://www.viget.com/careers/ 401 [17] https://www.viget.com/code-of-ethics/ 402 [18] https://www.viget.com/diversity-equity-and-inclusion/ 403 [19] https://pointlesscorp.com/ 404 [20] https://explorations.viget.com/ 405 [21] https://code.viget.com/ 406 [22] https://www.viget.com/newsletter/building-the-future-one-intern-at-a-time/ 407 [23] https://www.viget.com/articles/some-thoughts-after-a-major-ruby-on-rails-upgrade/ 408 [24] https://www.viget.com/ 409 [25] https://www.viget.com/articles 410 [26] https://www.viget.com/articles/docker-rails-solutions-to-common-hurdles/#hero 411 [27] http://eepurl.com/gtHqsj 412 [29] https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.viget.com%2Farticles%2Fdocker-rails-solutions-to-common-hurdles%2F 413 [30] http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.viget.com%2Farticles%2Fdocker-rails-solutions-to-common-hurdles%2F 414 [31] https://x.com/intent/tweet?text=Tips%20n%20Tricks%20for%20working%20with%20Rails%20applications%20in%20Docker%20https%3A%2F%2Fwww.viget.com%2Farticles%2Fdocker-rails-solutions-to-common-hurdles%2F 415 [32] https://www.viget.com/articles/?category=code#results 416 [33] https://www.viget.com/articles/?category=back-end-engineering#results 417 [34] https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.viget.com%2Farticles%2Fdocker-rails-solutions-to-common-hurdles%2F 418 [35] http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.viget.com%2Farticles%2Fdocker-rails-solutions-to-common-hurdles%2F 419 [36] https://x.com/intent/tweet?text=Tips%20n%20Tricks%20for%20working%20with%20Rails%20applications%20in%20Docker%20https%3A%2F%2Fwww.viget.com%2Farticles%2Fdocker-rails-solutions-to-common-hurdles%2F 420 [37] https://www.viget.com/articles/docker-right-for-us-right-for-you-1 421 [38] https://www.viget.com/articles/docker-rails-solutions-to-common-hurdles/#rails-tips-tricks 422 [39] https://stackoverflow.com/questions/35211638/how-to-debug-a-rails-app-in-docker-with-pry/37264588#37264588 423 [40] https://github.com/copiousfreetime/launchy/issues/108#issuecomment-319644326 424 [41] http://docs.docker.oeynet.com/docker-for-mac/osxfs-caching/ 425 [42] https://stackoverflow.com/a/63437557/1655658 426 [43] https://www.viget.com/articles/docker-rails-solutions-to-common-hurdles/#the-end 427 [44] https://www.viget.com/articles/some-thoughts-after-a-major-ruby-on-rails-upgrade/ 428 [45] https://www.viget.com/articles/setting-up-a-python-project-using-asdf-pdm-and-ruff/ 429 [46] https://www.viget.com/articles/craft-5-what-it-means-for-super-table-page-builders/ 430 [47] https://www.viget.com/newsletter 431 [48] http://eepurl.com/gtHqsj 432 [49] https://www.viget.com/contact/ 433 [50] mailto:[email protected]?subject=Hello%2C%20Viget%21 434 [51] tel:7038910670 435 [52] https://www.viget.com/work/ 436 [53] https://www.viget.com/services/ 437 [54] https://www.viget.com/articles/ 438 [55] https://www.viget.com/about/ 439 [56] https://www.viget.com/careers/ 440 [57] https://www.viget.com/code-of-ethics/ 441 [58] https://www.viget.com/diversity-equity-and-inclusion/ 442 [59] https://pointlesscorp.com/ 443 [60] https://explorations.viget.com/ 444 [61] https://code.viget.com/ 445 [62] https://www.viget.com/newsletter/ 446 [63] https://www.viget.com/ 447 [64] http://x.com/viget 448 [65] https://github.com/vigetlabs 449 [66] https://dribbble.com/viget 450 [67] https://www.instagram.com/viget/ 451 [68] https://www.linkedin.com/company/viget-labs 452 [69] https://vimeo.com/viget/collections 453 [71] https://www.viget.com/dc-metro-hq/ 454 [72] https://www.viget.com/durham/ 455 [73] https://www.viget.com/boulder/ 456 [74] https://www.viget.com/chattanooga/ 457 [75] https://www.viget.com/terms-conditions/ 458 [76] https://www.viget.com/privacy-policy/ 459 [77] https://individual.carefirst.com/individuals-families/mandates-policies/machine-readable-file.page