# Executing rails commands from host Bash

**URL:** https://meta.discourse.org/t/executing-rails-commands-from-host-bash/189126
**Category:** Self-hosting
**Created:** [May 4, 2021, 11:04am UTC](https://meta.discourse.org/t/executing-rails-commands-from-host-bash/189126 "2021-05-04T11:04:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sven2](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sven2](https://meta.discourse.org/u/sven2)
#### Post date: [May 4, 2021, 11:04am UTC](https://meta.discourse.org/t/executing-rails-commands-from-host-bash/189126/1 "2021-05-04T11:04:42Z")

</div>

Hi all,

I want to execute automated user clean up tasks from the host Bash. Manually, I run

```plaintext
/var/discourse/launcher enter app
rails c
UserDestroyer.new(Discourse.system_user).destroy(User.find_by_username_or_email("user@example.com"), delete_posts: false) 

```

I have a long list of user names from a text file that need to be removed. Manually executing this is not reasonable. I tried to wrap the deletion command in a bash script. When executing rails c with the `launcher app` subcommand, the Redis connection fails:

```plaintext
/var/discourse/launcher run app "echo \"User.find_by_username_or_email('user@example.com')\" | rails c"
Failed to report error: Error connecting to Redis on localhost:6379 (Errno::EADDRNOTAVAIL) 2 Error connecting to Redis on localhost:6379 (Errno::EADDRNOTAVAIL) subscribe failed, reconnecting in 1 second.

```

However, when I compare the environments with `export` between `launcher enter app` and `launcher run app`, they look pretty much identical. What am I missing? `launcher run` starts in `/` while `launcher enter` directly enters `/var/www/discourse`. Using a cd before executing rails does not help.

Any hints? Thanks!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 4, 2021, 11:11am UTC](https://meta.discourse.org/t/executing-rails-commands-from-host-bash/189126/2 "2021-05-04T11:11:36Z")

</div>

I think docker run launches a new container.

For things like that I use `docker exec` rather than `launcher`,which does a bunch of stuff you don’t need.

---

<div class="post-metadata">

### Author: ![sven2](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sven2](https://meta.discourse.org/u/sven2)
#### Post date: [May 4, 2021, 11:41am UTC](https://meta.discourse.org/t/executing-rails-commands-from-host-bash/189126/3 "2021-05-04T11:41:14Z")

</div>

Thanks for the reply. I find that the behavior of rails with `docker exec` is also very odd. Manually, it works perfectly well:

```plaintext
docker exec -it de7f5f6f649c '/bin/bash'
root@discourse-app:/# rails c
[1] pry(main)> User.find_by_username_or_email('myname')

```

But when I try to execute `rails c` from outside the container, I get the following problems:

```plaintext
docker exec -it de7f5f6f649c "/usr/local/bin/rails console"
OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "/usr/local/bin/rails console": stat /usr/local/bin/rails console: no such file or directory: unknown

```

But more interesting:

```plaintext
docker exec -it de7f5f6f649c '/bin/bash -c "/usr/local/bin/rails"'
OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "/bin/bash -c \"/usr/local/bin/rails\"": stat /bin/bash -c "/usr/local/bin/rails": no such file or directory: unknown

```

Why is this different at all?

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [May 4, 2021, 2:34pm UTC](https://meta.discourse.org/t/executing-rails-commands-from-host-bash/189126/4 "2021-05-04T14:34:46Z")

</div>

This is expected; you are telling docker to execute the binary `/usr/local/bin/rails console` in the container. That is, a single file with an embedded space. This file doesn’t exist.

consider the following:

```plaintext
○ → docker run -i debian /bin/echo hello
hello

○ → docker run -i debian '/bin/echo hello'
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "/bin/echo hello": stat /bin/echo hello: no such file or directory: unknown.
ERRO[0000] error waiting for container: context canceled 

```

Quote the command the same way as if you were executing from inside the container:

```plaintext
○ → docker exec -i app rails runner 'puts "hello"'
hello

```

Using the example from the OP, this should work:

```plaintext
docker exec -i app rails runner 'UserDestroyer.new(Discourse.system_user).destroy(User.find_by_username_or_email("user@example.com"), delete_posts: false)'

```

---

<div class="post-metadata">

### Author: ![sven2](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sven2](https://meta.discourse.org/u/sven2)
#### Post date: [May 4, 2021, 3:40pm UTC](https://meta.discourse.org/t/executing-rails-commands-from-host-bash/189126/5 "2021-05-04T15:40:01Z")

</div>

Okay, it was unexpected for me because I did not RTFM. Thanks for the fast help.
