Docker Gem Install



Gems are for ruby devs. The expertise you need is a ruby dev who also uses docker. When I was programming in Ruby around 2011, most ruby setups used rvm and bundler to handle dependencies. Finally, docker-compose.yml is where the magic happens. This file describes the services that comprise your app (a database and a web app), how to get each one’s Docker image (the database just runs on a pre-made PostgreSQL image, and the web app is built from the current directory), and the configuration needed to link them together and expose the web app’s port. As others have noted, in general use the -v flag for the gem install command. If you're developing a gem locally, after cutting a gem from your gemspec: $ gem install gemname-version.gem Assuming version 0.8, it would look like this: $ gem install gemname-0.8.gem. RUN go get -d -v && go install -v && go build EXPOSE 3000 CMD './api' Starting from the top, you’ll find that the FROM statement is now golang: 1.10. This means Docker will fetch a pre-built Go image from Docker Hub that has all the needed Go tools already installed. Now, once again, build the Docker image with.

Please note that, due to the upcoming Docker Rate Limit announcement, users will be required to add their own authentication information to their build settings or build config as documented below.

Travis CI builds can run and build Docker images, and can also push images toDocker repositories or other remote storage.

To use Docker add the following settings to your .travis.yml:

Then you can add - docker commands to your build as shown in the followingexamples.

We do not currently support use of Docker on macOS.

For information on how to use Docker on Travis CI Enterprise check out Enabling Docker Builds.

Using a Docker Image from a Repository in a Build #

This example repository runs twoDocker containers built from the same image:

  • a Sinatra application
  • the Sinatra application test suite

After specifying in the .travis.yml to use Docker (with services: - docker) and Ruby (with language: ruby), the before_install build step pulls a Docker image from carlad/sinatra then runs

in a container built from that image after mapping some ports and paths. Readthe Docker User Guide if you need arefresher on how to use Docker.

The full .travis.yml looks like this

Gem

and produces the following buildoutput:

Building a Docker Image from a Dockerfile #

Instead of downloading the Docker image fromcarlad/sinatra you canbuild it directly from the Dockerfile in the GitHubrepository.

To build the Dockerfile in the current directory, and give it the samecarlad/sinatra label, change the docker pull line to:

The full .travis.yml looks like this

Pushing a Docker Image to a Registry #

To push an image to a Docker registry, one must first authenticate via dockerlogin. The email, username, and password used for login should be stored inthe repository settings environment variables, which may be set up through therepository settings web page or locally via the Travis CLI, e.g.:

Be sure to encrypt environment variablesusing the travis gem.

Within your .travis.yml prior to attempting a docker push or perhaps beforedocker pull of a private image, e.g.:

Branch Based Registry Pushes #

To push a particular branch of your repository to a remote registry,use the custom deploy section of your .travis.yml:

Where docker_push is a script in your repository containing:

Private Registry Login #

When pushing to a private registry, be sure to specify the hostname in thedocker login command, e.g.:

Docker Gem InstallDocker

Using Docker Compose #

The Docker Compose tool is also installed in the Docker enabled environment.

If needed, you can easily replace this preinstalled version of docker-composeby adding the following before_install step to your .travis.yml:

Docker run gem install bundler

Installing a newer Docker version #

You can upgrade to the latest version and use any new Docker features by manuallyupdating it in the before_install step of your .travis.yml:

Updating from download.docker.com

Alternatively, you can use addons instead of before_install to update via apt as well:

Check what version of Docker you’re running with docker --version

Examples #

  • heroku/logplex (Heroku log router)
  • kartorza/docker-pg-backup (A cron job that will back up databases running in a docker PostgreSQL container)

Estimated reading time: 8 minutes

This Quickstart guide shows you how to use Docker Compose to set up and runa Rails/PostgreSQL app. Before starting, install Compose.

Define the project

Start by setting up the files needed to build the app. The app will run inside aDocker container containing its dependencies. Defining dependencies is done usinga file called Dockerfile. To begin with, the Dockerfile consists of:

That’ll put your application code inside an image that builds a containerwith Ruby, Bundler and all your dependencies inside it. For more information onhow to write Dockerfiles, see the Docker user guideand the Dockerfile reference.

Next, create a bootstrap Gemfile which just loads Rails. It’ll be overwrittenin a moment by rails new.

Create an empty Gemfile.lock to build our Dockerfile.

Next, provide an entrypoint script to fix a Rails-specific issue thatprevents the server from restarting when a certain server.pid file pre-exists.This script will be executed every time the container gets started.entrypoint.sh consists of:

Finally, docker-compose.yml is where the magic happens. This file describesthe services that comprise your app (a database and a web app), how to get eachone’s Docker image (the database just runs on a pre-made PostgreSQL image, andthe web app is built from the current directory), and the configuration neededto link them together and expose the web app’s port.

Docker run gem install bundler

Tip

Docker Ruby Gem Install

You can use either a .yml or .yaml extension for this file.

Build the project

With those files in place, you can now generate the Rails skeleton appusing docker-compose run:

First, Compose builds the image for the web service using the Dockerfile.The --no-deps tells Compose not to start linked services. Then it runsrails new inside a new container, using that image. Once it’s done, youshould have generated a fresh app.

List the files.

If you are running Docker on Linux, the files rails new created are owned byroot. This happens because the container runs as the root user. If this is thecase, change the ownership of the new files.

If you are running Docker on Mac or Windows, you should already have ownershipof all files, including those generated by rails new.

Now that you’ve got a new Gemfile, you need to build the image again. (This, andchanges to the Gemfile or the Dockerfile, should be the only times you’ll needto rebuild.)

Connect the database

The app is now bootable, but you’re not quite there yet. By default, Railsexpects a database to be running on localhost - so you need to point it at thedb container instead. You also need to change the database and username toalign with the defaults set by the postgres image.

Replace the contents of config/database.yml with the following:

You can now boot the app with docker-compose up:

If all’s well, you should see some PostgreSQL output.

Finally, you need to create the database. In another terminal, run:

Here is an example of the output from that command:

Docker Run Gem Install

View the Rails welcome page!

That’s it. Your app should now be running on port 3000 on your Docker daemon.

On Docker Desktop for Mac and Docker Desktop for Windows, go to http://localhost:3000 on a webbrowser to see the Rails Welcome.

Stop the application

To stop the application, run docker-compose down inyour project directory. You can use the same terminal window in which youstarted the database, or another one where you have access to a command prompt.This is a clean way to stop the application.

Restart the application

Docker Gem Install Bundler

To restart the application run docker-compose up in the project directory.

Rebuild the application

If you make changes to the Gemfile or the Compose file to try out some differentconfigurations, you need to rebuild. Some changes require onlydocker-compose up --build, but a full rebuild requires a re-run ofdocker-compose run web bundle install to sync changes in the Gemfile.lock tothe host, followed by docker-compose up --build.

Here is an example of the first case, where a full rebuild is not necessary.Suppose you simply want to change the exposed port on the local host from 3000in our first example to 3001. Make the change to the Compose file to exposeport 3000 on the container through a new port, 3001, on the host, and savethe changes:

Now, rebuild and restart the app with docker-compose up --build.

Inside the container, your app is running on the same port as before 3000, butthe Rails Welcome is now available on http://localhost:3001 on your localhost.

More Compose documentation

documentation, docs, docker, compose, orchestration, containers