skip to Main Content

So, I’m trying to create a bash script to clear the cache on a containerized docker instance on my system, as a single fire-and-forget command on a legacy codebase.

My current attempt has led me to the following script, which doesn’t work:

Clear_container_cache.sh

docker exec -it <my_image_name> bundle exec rake Rails.cache.clear

What would be a reliable way to do this? The containers are hosted on a mac, the containers are Linux, and of course ruby is running rails on the container.

Local Machine: Mac Mojave

Base Image: CentOS Linux 7 (customized base)

Rails Version: 4.1.16

Update:
Per ZedTuX’s suggestion, tried the following:

docker exec -it <container_name> bundle exec rails runner 'Rails.cache.clear'

which gives

OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: "bundle": executable file not found in $PATH": unknown

And running

docker-compose exec containership_hspweb_1 bundle exec rails runner 'Rails.cache.clear'

Errors out due to lack of a yml configuration file (which doesn’t exist, due to being dynamically created and destroyed during runtime.)

As a note,

bundle exec rails runner 'Rails.cache.clear'

does work when run directly in the container.

3

Answers


  1. Definitively your question lacks of information like the Dockerfile you’re using to build your image, the Rails version your are using, but with some assumptions, here is how I can help you:

    Assuming:

    • my_image_name is a container including the Rails framework and bundler
    • you are running Rails 5/6

    Rails.cache.clear wouldn’t work with Rake

    You are trying to run rake but you give it a Ruby/Rails code to execute as a parameter. After solving the “bundle: no such file or directory” error, you would anyway end with the following error:

    Don't know how to build task 'Rails.cache.clear' (See the list of available tasks with `rake --tasks`)
    

    Rake takes rake tasks as arguments like db:migrate, not Ruby/Rails code.

    In order to execute the Ruby/Rails code Rails.cache.clear from the console, you will have to use rails runner (or rails r) like so:

    bundle exec rails runner 'Rails.cache.clear'
    

    With docker-compose

    It looks really strange in 2019 to use docker exec in a project, so I guess you are actually using Docker compose, so you should go with docker-compose exec instead:

    docker-compose exec <container_name> bundle exec rails runner 'Rails.cache.clear'
    

    container_name is the service name defined in the docker-compose.yml file, like app or web.

    With docker

    Anyways, I can imagine you have a really good use case for docker exec so here is the same command for Docker:

    docker exec -it <container_name> bundle exec rails runner 'Rails.cache.clear' 
    

    But I still have “exec: “bin/bundle”: stat bin/bundle: no such file or directory”

    That means you have an issue in the way you’ve built your Dockerfile.
    Without its content we can’t help you, but there are plenty of blog articles explaining how to build a Rails Dockerfile which works, like:

    That should help you enough to get rid of this error.

    BTW my_image_name should be my_container_name as you’re running containers based on images, and not running images.

    Login or Signup to reply.
  2. Depending on what you have sh or bash you can try:

    docker exec -it <my_image_name> sh -c 'bundle exec rake Rails.cache.clear'
    

    or

    docker exec -it <my_image_name> bash -c 'bundle exec rake Rails.cache.clear'
    
    Login or Signup to reply.
  3. Posting my comment as an answer.

    Use the below command

    docker exec -it <container_name> bash -c "bundle exec rails runner 'Rails.cache.clear'".

    It asks the bash to run the bundle command, you can use bash/sh whichever is available in the container.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search