Get started building Ruby applications in docker formatted containers using the Red Hat Container Development CDK (CDK) 2.0
In this tutorial, you will learn how to start building Ruby applications in docker formatted containers using the Red Hat Container Development Kit (CDK) 2.0 on Red Hat Enterprise Linux. You need to have CDK 2.0 installed and should have downloaded the Red Hat Enterprise Linux vagrant box for your system. See the CDK 2.0 Installation Guide for more information.
If you encounter difficulties at any point, see Troubleshooting and FAQ.
The steps in this tutorial run on Red Hat Enterprise Linux (RHEL) inside the vagrant box. The vagrant box includes docker, OpenShift Enterprise, and kubernetes.. You will enter the commands in this tutorial after logging into the box with vagrant ssh
.
Open a Terminal or Command window to enter the commands in this tutorial. On Windows, using Cygwin Terminal instead of a cmd.exe window is suggested.
If you haven’t already installed the CDK, follow the CDK Installation Guide.
To start the vagrant box:
-
Change to the directory where you unpacked the CDK zip file.
-
Change to the sub-directory
components/rhel/rhel-ose
. Alternatively, copy theVagrantfile
in that directory to a working directory of your choosing. -
Start the box by entering
vagrant up
. Note: theVagrantfile
needs to be in the current directory when entering vagrant commands without specifying a box name or a path to theVagrantfile
. -
You will be prompted to register the Vagrant box with Red Hat Subscription Management during startup. This is required to allow the box to download software from Red Hat by attaching it to your Red Hat subscription. You will need to enter your Red Hat username and password.
The vagrant registration plugin will automatically attach the box to your Red Hat subscription when it starts up and release it when the box is shutdown using the
vagrant halt
command.
When starting the vagrant box, a number of log messages will be displayed. Most of these are informational, however you should review the output if the box fails to start. Below is the output from a typical vagrant up
:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default: Warning: Connection timeout. Retrying...
default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
==> default: Registering box with vagrant-registration...
default: Would you like to register the system now (default: yes)? [y|n]y
default: username: _your username_
default: password: _your password_
==> default: Rsyncing folder: /cygdrive/c/cdk2/components/rhel/rhel-ose/ => /vagrant
==> default: Running provisioner: shell...
default: Running: inline script
==> default: Created symlink from /etc/systemd/system/multi-user.target.wants/openshift.service to /etc/systemd/system/openshift.service.
You should now be able to log into the vagrant box using vagrant ssh
.
This step will download and install Ruby using a container image from the Red Hat Atomic Registry, a repository of container images. Installing the Ruby container will make Ruby available for use by other containers on your system. Because containers run in isolated environments, your host system will not be altered by the installation. You will use docker
commands to interact with and view the container’s contents.
The commands shown in this section can be used to download and install other container images, like application containers you build. Containers can specify that they require other containers to be installed, which can happen automatically. For example, you can specify in the Dockerfile
that is used to describe and build your container that your application requires Ruby. Then, when someone installs your container, their system will automatically download the required Ruby container directly from the Red Hat Atomic Registry.
The Ruby container image is part of Red Hat Software Collections, which provides the latest stable versions of dynamic languages, open source databases, and web development tools for Red Hat Enterprise Linux. Access to the Red Hat Software Collections (RHSCL) is included with many Red Hat Enterprise Linux (RHEL) subscriptions. For more information about which subscriptions include RHSCL, see How to use Red Hat Software Collections (RHSCL) or Red Hat Developer Toolset (DTS).
Run all of the following commands on Red Hat Enterprise Linux inside the vagrant box. If you haven’t logged into the vagrant box, open a Terminal or Command window and change to the directory cdk/components/rhel-ose/Vagrantfile
. Log in with vagrant ssh
To download and install the Ruby container image, use the following command:
$ docker pull registry.access.redhat.com/rhscl/ruby-22-rhel7
The docker images
command lists the container images that are present on your system:
$ docker images
The list will include those you’ve downloaded and any containers previously installed on your system. The CDK vagrant box includes software components that are distributed as container images.
Now start a bash
shell to have a look around inside a container that uses the Ruby container image. The shell prompt changes, which is an indication that you are typing at the shell inside the container. A ps -ef
shows the only thing running inside the container is bash
and ps
. Type exit
to leave the container’s bash shell.
bash-4.2$ which ruby
/opt/rh/rh-ruby22/root/usr/bin/ruby
bash-4.2$ ruby --version
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
bash-4.2$ pwd
/opt/app-root/src
bash-4.2$ ps -ef
UID PID PPID C STIME TTY TIME CMD
default 1 0 0 19:40 ? 00:00:00 /bin/bash
default 19 1 0 19:41 ? 00:00:00 ps -ef
bash-4.2$ exit
The prior docker run
command created a container to run your command, keep any state, and isolate it from the rest of the system. You can view the list of running containers with docker ps
. To see all of the containers that have been created, including those that have exited, use docker ps -a
. Depending on which Vagrantfile you used there may be a number of other containers running such as containers used to create an OpenShift environment.
You can restart the container that was created above with docker start
. Containers are referred to by name. Docker will automatically generate a name if you don’t provide one. Once the container has been restarted, docker attach
will let you interact with the shell running inside of it. See the following example:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b08a6244a4c rhscl/ruby-22-rhel7 "container-entrypoint" 3 minutes ago Exited (0) 2 minutes ago determined_mayer
$ docker start determined_mayer
determined_mayer
$ docker attach determined_mayer
At this point you are connected to the running shell inside the container. When you attach you won’t see the command prompt, so hit Enter to get it to print another one.
bash-4.2$ ps -ef
UID PID PPID C STIME TTY TIME CMD
default 1 0 0 19:44 ? 00:00:00 /bin/bash
default 17 1 0 19:44 ? 00:00:00 ps -ef
bash-4.2$ exit
Since the only process in the container, bash
, was told to exit
the container will no longer be running. This can be verified with docker ps -a
. Containers that are no longer needed can be cleaned up with docker rm <container-name>
.
$ docker rm determined_mayer
To see what other container images are available in the Red Hat container registry, use one or more of the following searches:
$ docker search registry.access.redhat.com/rhscl
$ docker search registry.access.redhat.com/openshift3
$ docker search registry.access.redhat.com/rhel
$ docker search registry.access.redhat.com/jboss
If you need help, see Troubleshooting and FAQ.
In this step, you will create a tiny Hello World container that uses Ruby as a web server. Once created, the container can be run on other systems that have docker
installed. You will need to create several files in an empty directory using your favorite editor, including a Dockerfile
that describes how to build the container image.
Note: you can edit files on your host system that can be synchronized to your vagrant box with vagrant rsync
. For more information see Vagrant synchronizad folders in the CDK Installation Guide.
First, create an empty directory, and then create a file named Dockerfile
with the following contents, but change the MAINTAINER
line to have your name and email address:
FROM rhscl/ruby-22-rhel7 MAINTAINER Your Name "[email protected]" EXPOSE 8000 COPY . /opt/app-root/src CMD /bin/bash -c 'ruby hello-http.rb'
Create the file hello-http.rb
in the same directory as the Dockerfile
:
require 'socket' port = 8000 STDERR.print "Listening on port #{port}\n" server = TCPServer.new('0.0.0.0', port) loop do socket = server.accept request = socket.gets STDERR.puts request response = "Hello, Red Hat Developers World!\n" socket.print "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + response.bytesize.to_s + "\r\n" + "Connection: close\r\n" socket.print "\r\n" socket.print response socket.close end
Now build the container image using docker build
.
$ docker build -t myname/rubyweb .
You can see the container image that was created using the following command:
$ docker images
Now run the container using docker run
. The Ruby socket server will create a tiny web server that listens on port 8000 inside the container. The run
command will map port 8000 on the host machine to port 8000 inside the container.
$ docker run -d -p 8000:8000 --name helloweb myname/rubyweb
The run command returns a unique ID for the container, which you can ignore. To check that the container is running, use docker ps
. The output should show a container named helloweb
that is running the myname/rubyweb
container image you created.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7885aa23773 myname/rubyweb "container-entrypoint" 6 seconds ago Up 4 seconds 0.0.0.0:8000->8000/tcp, 8080/tcp helloweb
Use curl
to access the Ruby web server:
# curl http://localhost:8000/
Hello, Red Hat Developers World!
Note: you should also be able to access the Ruby web server running inside your container from the browser on your host machine. The rhel-ose/Vagrantfile
sets the vagrant box’s IP address to 10.1.2.2. The url to use on your host system is http://10.1.2.2:8000/
.
To view the logs from the running container use docker logs <container-name>
:
$ docker logs helloweb
When you are done, stop the running container:
$ docker stop helloweb
The helloweb
container will be retained until you remove it with docker rm
. You can restart the container with docker start helloweb
. Note: A subsequent docker run
will generate an error if a container with the same name already exists.
You can view information about a container using docker inspect
:
$ docker inspect myname/rubyweb
The output is a JSON structure that is easily readable. The Config section has details of the container’s runtime environment such as environment variables and default command. Note that much of the information in the container’s configuration was inherited from the parent container, which in this case is the Ruby runtime container.
Finally, when the application container images you create are ready, you can distribute them by pushing them to a public or private container registry. Your containers will then be available to install on other systems using docker pull
.
-
Getting Started with Container Development Kit — contains detailed information on the components that make up the CDK along with instructions on how to interact with containers in the CDK environment.
-
Container Development Guide (under development) — Provides guidance for more advanced container development. It illustrates the different ways of creating containers to run with Docker, Kubernetes, Nulecule, Atomic and other container run-time environments.
-
Red Hat Enterprise Linux Atomic Host 7 Getting Started with Containers — This document covers working with and deploying containers on both Red Hat Enterprise Linux and Red Hat Enterprise Linux Atomic Host. It also provides information on orchestrating multi-container environments with kubernetes.
-
Red Hat Enterprise Linux 7.2 Release Notes — includes information on recent updates to the container tools included in Red Hat Enterprise Linux and Atomic Host.
Red Hat delivers the resources and ecosystem of experts to help you be more productive and build great solutions. Register for free at developers.redhat.com.
Follow the Red Hat Developer Blog
http://developerblog.redhat.com/
-
How do I tell if there is a container image available that has a newer version of Ruby?
How can I see what other container images are available?
I can’t find the container mentioned in this tutorial, how can I tell if the name changed?
To see what other containers are available in the Red Hat container registry, use one or more of the following searches:
$ docker search registry.access.redhat.com/rhscl $ docker search registry.access.redhat.com/openshift3 $ docker search registry.access.redhat.com/rhel $ docker search registry.access.redhat.com/jboss
-
Where can I learn more about delivering applications with Linux containers?
If you haven’t already joined the Red Hat Developers program, sign up at developers.redhat.com. Membership is free.
Recommended Practices for Container Development and many other container articles are available from the Red Hat Customer Portal.If you are a Red Hat Technology Partner, visit the Container Zone at the Red Hat Connect for Technology Partners web site.