I have a rake task in my application, that should start a TCP server.
I have moved rake command to an entrypoint.sh
file and in docker-compose added new service, which is called tcp
.
That tcp service return incorrect path, when I run docker-compose up -d
. I tried different ways to setup, but no luck. How do I build docker image for a rake task?
unable to prepare context: path "/Users/mac_user/Projects/fmt100/lib/tasks/socketing.rake" not found
docker-compose.yml
version: '3.9'
services:
tcp:
build: ./lib/tasks/socketing.rake
depends_on:
- app
- database
- redis
- sidekiq
env_file: .env
volumes:
- .:/app
- tcp_server:/app/lib/tasks
entrypoint: ./entrypoints/tcp-entrypoint.sh
volumes:
tcp_server:
You should have it
build:
the same image that contains your Rails application. Override thecommand:
to run the Rake task. In the Compose file it should look roughly like:The
sidekiq
container probably looks very similar.Since you have two containers labeled
build: .
, Compose will try to build the image twice. However, these images have identical Dockerfiles and run on an identical source tree. This means that Docker's layer caching will take effect, and the second build will run extremely quickly and will not actually produce a new image.To make this work correctly, you also need to correctly structure your Dockerfile. It's important here that you put the actual command to run as the Dockerfile
CMD
and not theENTRYPOINT
(or at the very least it must match the override you have in thedocker-compose.yml
). As always, make sure the image is self-contained and includes all of its library dependencies and application code (note the absence ofvolumes:
in the Compose file above).You should not put the command in the
ENTRYPOINT
or an entrypoint script. Instead, use the entrypoint to do some first-time setup, and maybe inject related wrappers like Bundler.The
command:
is very straightforward to override, and whatever you provide appears in the"$@"
in the final statement of the script. So if you want a Pry console, for example, you canwhich will override the
command:
at the command line, leaving the image'sENTRYPOINT
unchanged. The sample Compose file does the same thing for your TCP server launched as a Rake task (and, again, I'd expect your Sidekiq worker to work the same way).