Reputation: 352
I have multiple Ruby applications that are run in crons using IronWorker. I am new to Docker and I want to test the application locally before pushing the code to Iron. How do I do that?
Upvotes: 1
Views: 277
Reputation: 352
Your code can be stored privately on Iron.io and the image on Docker can include only the programming language and libraries and be made public. I've put together a "hello world" example showing how it can be done. I am using Alpine linux and the Ruby programming language along with Iron's dev packages. I also included the "pg" gem:
hello.rb
require 'pg'
puts "hello world"
Gemfile
source 'https://rubygems.org'
gem 'pg'
Dockerfile
FROM iron/ruby-2.3:dev
RUN apk update && apk upgrade
RUN gem install pg --no-ri --no-rdoc
COPY hello.rb /
RUN apk add bash
Here are the steps to get this running:
In this example, the name of the Docker username is testuser and the name of the Docker repository is testrepo.
Run the following command in a Docker Terminal. I have added a tag "0.0.1". This should be incremented with each change to the image that is pushed to Docker.
docker build -t testuser/testrepo:0.0.1 .
Since the Dockerfile did not include an ENTRYPOINT ["ruby", "hello.rb"] line, any terminal command can be included in a "docker run" command. To get into an image with a bash prompt, you would run:
docker run -it testuser/testrepo:0.0.1 /bin/bash
Once inside of bash, you can then see if the code can be executed:
ruby hello.rb
In this example, I received the following error:
`require': cannot load such file -- json (LoadError)
To fix that, update the Dockerfile to install json and then re-test the image. Here is the updated Dockerfile:
FROM iron/ruby-2.3:dev
RUN apk update && apk upgrade
RUN gem install pg --no-ri --no-rdoc
RUN gem install json --no-ri --no-rdoc
COPY hello.rb /
RUN apk add bash
Now that we know the code will run correctly with the image, we can update the Dockerfile and push the image to Docker and the code to Iron.
FROM iron/ruby-2.3:dev
RUN apk update && apk upgrade
RUN gem install pg --no-ri --no-rdoc
RUN gem install json --no-ri --no-rdoc
RUN apk add bash
docker build -t testuser/testrepo:0.0.1 .
docker push testuser/testrepo:0.0.1
iron register testuser/testrepo:0.0.1
zip -r hello.zip hello.rb
iron worker upload --zip hello.zip --name hello testuser/testrepo:0.0.1 ruby hello.rb
Done! You can now schedule an IronWorker through the HUD or through their API.
Upvotes: 0