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
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 bundlerRails.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: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 userails runner
(orrails r
) like so: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 withdocker-compose exec
instead:container_name
is the service name defined in thedocker-compose.yml
file, likeapp
orweb
.With docker
Anyways, I can imagine you have a really good use case for
docker exec
so here is the same command for Docker: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 bemy_container_name
as you’re running containers based on images, and not running images.Depending on what you have
sh
orbash
you can try:or
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.