nickjj / docker-rails-example

A production ready example Rails app that's using Docker and Docker Compose.
MIT License
941 stars 185 forks source link

rename .env file #74

Closed ZPVIP closed 5 months ago

ZPVIP commented 5 months ago

First of all, thank you for your outstanding contribution to rails + docker.

I'm trying to use dotenv to add some environment variables, and kamal to deploy to production, which will use yaml-formatted .env. Given that the .env used by docker-rails-example is just a bash file, would it be possible to rename it to something like .env_bash, to avoid conflicts with dotenv?

Thank you.

nickjj commented 5 months ago

Hi,

I haven't used Kamal but if your app is running in Docker, you don't need the dotenv gem since Docker will make your env variables available to your app.

With that said, if you still want to rename the file you can but Docker Compose will specifically look for an .env file by default to perform variable interpolation in the docker-compose.yml file. If you want to use a custom env file name then instead of running docker compose up you can run docker compose --env-file .env_bash up. Personally I would try to avoid that since it'll affect how anyone will run your project but it technically works.

I requested a change to Docker Compose to make it easier to use a custom env file name but Docker shot down the idea https://github.com/docker/compose/issues/11122. In my heart of hearts I know that feature would be really welcome but there's nothing I can do unfortunately (even as a Docker Captain with direct access to Docker's engineers!). If you feel strongly about it please comment in that issue, perhaps Docker will reconsider if they see enough folks asking for it.

ZPVIP commented 5 months ago

Thank you so much for such a quick reply .

After reading your reply, I realized that I don't need dotenv for rails in docker anymore, and also, I realized that dotenv for kamal supports bash formats(starting with export), such as export S3_BUCKET=YOURS3BUCKET, and all I need to do is to put the kamal related variables into the same .env.

By the way, do you have any plans to make a video on deploying rails programs to production with docker? I'm sure the tools you use and your perspectives are much better than "official" kamal.

nickjj commented 5 months ago

I use this project as is and use Ansible to bootstrap the server.

With basically 15 lines of Ansible YAML configuration[0] and 1 command I can set up any server to run any Dockerized application that's securely served over any domain name of your choosing, complete with a mechanism to git push to the server and build or pull images from a Docker registry, database backups, log management, nginx with SSL, fully locked down security wise, automated updates and everything else you likely want to confidently run an application in production.

I have nothing against Kamal but it feels like it's trying to reinvent a combination of Ansible and Docker Compose. These are tried and true tools that have been production ready for about a decade and are tech stack agnostic. If you plan to keep 1 server around for a while (such as deploying to 1 server) in my opinion having a configuration management tool manage that server is important, otherwise you end up making manual changes to it that you can't track effectively.

[0]: This is the configuration part which uses a bunch of different Ansible playbooks and roles I've developed, maintain and keep updated for the last ~8 years.

ZPVIP commented 5 months ago

I appreciate your valuable suggestions.

I've been a long-time user of Capistrano, but recently, I've been exploring Kamal as I'm interested in trying out Docker deployment. It would be fantastic if you could share your Ansible sample code, and I could use your YAML as a starting point for modifications.

I believe it would be beneficial not only for me but also for others using docker-rails-example. Your video provides the most helpful instructions I've found online.

nickjj commented 5 months ago

In this case Ansible sets up the server with a git post receive hook but deployment is done through git pushing. This can be initiated from your dev box or a CI server.

Conceptually I see it like this:

As for open sourcing all of the Ansible stuff, maybe some day. There's been a deployment course in my mind for the last 4 years but it's hard to make such a big course while also working full time. But if it ever does ship, it would be included there.

ZPVIP commented 5 months ago

Thanks for the key tips .

I reckon I've got the hang of it now. Since I'm just deploying to one server, I figure I don't really need Ansible to set it up. The server already has a bunch of Python programs running in Docker. I've been using the post-receive hook to deploy my Python programs, so I'm thinking I can give Rails a shot the same way. Just gotta set up a reverse proxy, and I should be good to roll.

Thank you, still looking forward to your course.

nickjj commented 5 months ago

Sounds good, yep I deploy my Flask, Django, Rails and other apps all the same way. nginx reverse proxies them and you're golden.