diff --git a/.env.production.sample b/.env.production.sample
index faefa24829e43934a0dcf69c4de181fb273e4ade..eb1c5a48f50c7d74e8d1837f9a021a88d9da509d 100644
--- a/.env.production.sample
+++ b/.env.production.sample
@@ -36,7 +36,7 @@ OTP_SECRET=
 # You should only generate this once per instance. If you later decide to change it, all push subscription will
 # be invalidated, requiring the users to access the website again to resubscribe.
 #
-# ruby -e "require 'webpush'; vapid_key = Webpush.generate_key; puts vapid_key.private_key; puts vapid_key.public_key;"
+# Generate with `rake mastodon:webpush:generate_vapid_key` task (`docker-compose run --rm web rake mastodon:webpush:generate_vapid_key` if you use docker compose)
 #
 # For more information visit https://rossta.net/blog/using-the-web-push-api-with-vapid.html
 VAPID_PRIVATE_KEY=
diff --git a/config/initializers/vapid.rb b/config/initializers/vapid.rb
index 74e07377c69b2f91432391d2e6b032f0b3b48aaa..618f5a3fbbd1f056d510af1933baf051bc4faf35 100644
--- a/config/initializers/vapid.rb
+++ b/config/initializers/vapid.rb
@@ -6,7 +6,7 @@ Rails.application.configure do
   # You should only generate this once per instance. If you later decide to change it, all push subscription will
   # be invalidated, requiring the users to access the website again to resubscribe.
   #
-  # ruby -e "require 'webpush'; vapid_key = Webpush.generate_key; puts vapid_key.private_key; puts vapid_key.public_key;"
+  # Generate with `rake mastodon:webpush:generate_vapid_key` task (`docker-compose run --rm web rake mastodon:webpush:generate_vapid_key` if you use docker compose)
   #
   # For more information visit https://rossta.net/blog/using-the-web-push-api-with-vapid.html
 
diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake
index 1b05ece33f87c3e7aa69a273738f05c29321b7b1..010139e91d327344aac2c2dfe60e8d5dd691421f 100644
--- a/lib/tasks/mastodon.rake
+++ b/lib/tasks/mastodon.rake
@@ -184,6 +184,15 @@ namespace :mastodon do
     end
   end
 
+  namespace :webpush do
+    desc 'Generate VAPID key'
+    task generate_vapid_key: :environment do
+      vapid_key = Webpush.generate_key
+      puts "VAPID_PRIVATE_KEY=#{vapid_key.private_key}"
+      puts "VAPID_PUBLIC_KEY=#{vapid_key.public_key}"
+    end
+  end
+
   namespace :maintenance do
     desc 'Update counter caches'
     task update_counter_caches: :environment do