Skip to content
This repository has been archived by the owner on Nov 10, 2021. It is now read-only.

DEVELOPER-3012 RHSCL 2.2 Beta Node.js v4 TTHWs #1097

Merged
merged 1 commit into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
378 changes: 378 additions & 0 deletions products/rhel/get-started-dcr7-nodejsbeta.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
:awestruct-layout: product-get-started-multipath
:awestruct-interpolate: true

## Path Name
Containerized Node.js

## Path Intro section
[.large-6.columns]
image:#{cdn(site.base_url + '/images/products/multipath/containerized-nodejs-logo.png')}[Node.js Logo]

[.large-18.columns#PathIntroSection]
Get started building Node.js v4 applications in docker containers on Red Hat Enterprise Linux in under 15 minutes.

## Prerequisites section title
Introduction and Prerequisites

## Prerequisites section
In this tutorial, you will learn how to start building Node.js v4 applications in docker containers on Red Hat Enterprise Linux. In order to build and run containers you will first install `docker` on your Red Hat Enterprise Linux 7 system. You will use the Node.js v4 container image from Red Hat Software Collections (RHSCL) 2.2 Beta as the basis for your containerized application.

You will need a system running Red Hat Enterprise Linux 7 Server with a current Red Hat subscription that allows you to download software and updates from Red Hat. Developers can now get a no-cost Red Hat Enterprise Linux® Developer Suite subscription for development purposes by link:#{site.download_manager_base_url}/download-manager/link/1350474[registering and downloading] through link:#{site.base_url}/[developers.redhat.com].

If you encounter difficulties at any point, see <<troubleshooting,Troubleshooting and FAQ>>.

## Step1 Duration
5 minutes

## Step1 Title
Prepare your system

## Step2 Duration
5 minutes

## Step2 Title
Run your first container

## Step3 Duration
5 minutes

## Step3 Title
Build Hello World in a container

## Step1 Content

In this step, you will configure your system to build and run docker containers. In the process, you will add the necessary software repositories, then verify that your system has a current Red Hat subscription and is able to receive updates from Red Hat. Your system needs to be already registered with Red Hat.

First, you will enable two Red Hat software repositories that are disabled by default. Instructions are provided for both the command line (CLI) and graphical user interface (GUI).

### Using the Red Hat Subscription Manager GUI

Red Hat Subscription Manager can be started from the _System Tools_ group of the _Applications_ menu. Alternatively, you can start it from the command prompt by typing `subscription-manager-gui`.

Select _Repositories_ from the _System_ menu of the subscription manager. In the list of repositories, check the _Enabled_ column for _rhel-7-server-optional-rpms_ and _rhel-7-server-extras-rpms_. After clicking, it might take several seconds for the check mark to appear in the enabled column.

### Using subscription-manager from the command line

You can add or remove software repositories from the command line using the `subscription-manager` tool. Start a _Terminal_ window if you don't already have one open. Use `su` to become the root user. Use `subscription-manager --list` option to to view the available software repositories.

[.code-block]
```
$ su -
# subscription-manager repos --list
```

Enable the two additional repositories:

[.code-block]
```
# subscription-manager repos --enable rhel-7-server-extras-rpms
# subscription-manager repos --enable rhel-7-server-optional-rpms
```

### Install docker and start the docker daemon

In the next step you will:

. Update your system with any available software updates
. Install `docker` and a few additional rpms using `yum`
. Configure the `docker` daemon to start at boot time
. Start the `docker` daemon

If you don't have a root _Terminal_ window open, start a _Terminal_ window and become the root user with `su`.

Now download and install any available updates by running `yum update`. If updates are available, `yum` will list them and ask if it is OK to proceed.

[.code-block]
```
$ su -
# yum update
```

Install `docker` and necessary additional rpms:

[.code-block]
```
# yum install docker device-mapper-libs device-mapper-event-libs
```

Enable the docker daemon to start at boot time and start it now:

[.code-block]
```
# systemctl enable docker.service
# systemctl start docker.service
```

Now verify that the docker daemon is running:

[.code-block]
```
# systemctl status docker.service
```

Your system is now ready to build and run docker-formatted containers. If you encounter difficulties at any point, see <<troubleshooting,Troubleshooting and FAQ>>.

## Step2 Content

This step will download and install Node.js v4 using a container image from the Red Hat container registry, a repository of container images. Installing the Node.js container image will make Node.js 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 must use `docker` commands to use or view the container's content.

The commands shown in this section can be used to download and install other containers, 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 Node.js. Then, when someone installs your container, their system will automatically download the required Node.js container directly from the Red Hat container registry.

The Node.js v4 container image is part of RHSCL, which provides the latest development technologies for Red Hat Enterprise Linux. Access to the RHSCL is included with many Red Hat Enterprise Linux (RHEL) subscriptions. For more information about which subscriptions include RHSCL, see link:https://access.redhat.com/solutions/472793[How to use Red Hat Software Collections (RHSCL) or Red Hat Developer Toolset (DTS)].

If you don't have a root _Terminal_ window open, start a _Terminal_ window and become the root user with `su`.

To download and install the Node.js container image, use the following command:

[.code-block]
```
# docker pull registry.access.redhat.com/rhscl_beta/nodejs-4-rhel7
```

The `docker images` command lists the container images that are present on your system:

[.code-block]
```
# docker images
```

The list will include those images you've downloaded and any containers previously installed on your system.

Now start a `bash` shell to have a look around inside a container that uses the Node.js 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.

[.code-block]
```
# docker run -it rhscl_beta/nodejs-4-rhel7 /bin/bash
bash-4.2$ which node
/opt/rh/rh-nodejs4/root/usr/bin/node
bash-4.2$ node --version
v4.4.2
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 14:42 ? 00:00:00 /bin/bash
default 14 1 0 14:42 ? 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`.

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:

[.code-block]
```
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84458ca538fb rhscl_beta/nodejs-4-rhel7 "container-entrypoin About a minute ago Exited (0) About a minute 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.

[.code-block]
```

bash-4.2$ ps -ef
UID PID PPID C STIME TTY TIME CMD
default 1 0 0 14:44 ? 00:00:00 /bin/bash
default 11 1 0 14:45 ? 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>_`.

[.code-block]
```
# docker rm determined_mayer
```

To see what other containers are available in the Red Hat container registry, use one or more of the following searches:

[.code-block]
```
# docker search registry.access.redhat.com/rhscl
# docker search registry.access.redhat.com/rhscl_beta
# 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,Troubleshooting and FAQ>>.


## Step3 Content

In this step, you will create a tiny Hello World container that uses Node.js 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 the container. You don't need to be running under the root user to create the files, but you will need root privileges to run the `docker` commands.

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:

.Dockerfile
----
FROM rhscl_beta/nodejs-4-rhel7

MAINTAINER Your Name "your-email@example.com"

EXPOSE 8000

COPY . /opt/app-root/src

CMD /bin/bash -c 'node hello-http.js'
----

Create the file `hello-http.js` with the following contents:

.hello-http.js
----
var http = require('http');
var port = 8000;
var laddr = '0.0.0.0';
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello, Red Hat Developers World from Node ' +
process.version + '!\n');
console.log('Processed request for '+ req.url);
}).listen(port, laddr);
console.log('Server running at http://' + laddr + ':' + port + '/');

----


Now build the container image using `docker build`. You will need to be root using `su` or `sudo` in the directory you created that contains `Dockerfile` and `hello-http.js`.

[.code-block]
```
# docker build -t myname/nodeweb .
```

You can see the container image that was created using the following command:

[.code-block]
```
# docker images
```

Now run the container using `docker run`. The Node.js http.server module 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.

[.code-block]
```
# docker run -d -p 8000:8000 --name helloweb myname/nodeweb`
```

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_/nodeweb` container image you created.

[.code-block]
```
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7885aa23773 myname/nodeweb "container-entrypoint" 6 seconds ago Up 4 seconds 0.0.0.0:8000->8000/tcp, 8080/tcp helloweb
```

Use `curl` to access the Node.js web server:

[.code-block]
```
# curl http://localhost:8000/
Hello, Red Hat Developers World from Node v4.4.2!
```

To view the logs from the running container use `docker logs _<container-name>_`:

[.code-block]
```
# docker logs helloweb
```

When you are done, stop the running container:

[.code-block]
```
# 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`:

[.code-block]
```
# docker inspect myname/nodeweb
```

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 Node.js 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`.


## Where to go next?

* link:https://access.redhat.com/documentation/en/red-hat-enterprise-linux-atomic-host/version-7/getting-started-with-containers/[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_. See <<About Red Hat Enterprise Linux Atomic Host>>.

* link:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/7.2_Release_Notes/[Red Hat Enterprise Linux 7.2 Release Notes] -- includes information on recent updates to the link:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/7.2_Release_Notes/atomic_host_and_containers.html[container tools included in Red Hat Enterprise Linux and Atomic Host].

### About Red Hat Enterprise Linux Atomic Host

Atomic Host is specifically optimized for deploying Linux containers in environments like Infrastructure as a Service (IaaS) clouds. Atomic Host's minimal footprint contains only the software needed to efficiently host containers. Atomic Host isn't intended for software development activities as it doesn't include development tools or a graphical user interface.

During software development it is suggested that you use Red Hat Enterprise Linux, which is suitable for many purposes including desktop and server installations. You can build and run containers on Red Hat Enterprise Linux, see link:https://access.redhat.com/articles/881893[Get Started with Docker Formatted Container Images on Red Hat Systems]. The steps to build a container image that include your application can be automated with a Dockerfile.

After your application is packaged in a container you should test it on Atomic Host to ensure that it is ready for deployment. In addition to minimized footprint, production environments built for running containers benefit from Atomic Host's enhanced security and atomic update and rollback capability.

Developers who are creating continuous integration/continuous delivery (CI/CD) environments will want to consider containers deployed on Atomic Host. This allows test environments to be quickly created while minimizing system resource requirements.


## More Resources

### Become a Red Hat developer: developers.redhat.com

Red Hat delivers the resources and ecosystem of experts to help you be more productive and build great solutions. Register for free at link:http://developers.redhat.com/[developers.redhat.com].

*Follow the Red Hat Developer Blog* +
link:http://developerblog.redhat.com/[]



## Faq section title
[[troubleshooting]]Troubleshooting and FAQ

## Faq section
. *My system is unable to download updates from Red Hat.*
+
Your system must be registered with Red Hat using `subscription-manager register`. You need to have a current Red Hat subscription.

. *As a developer, how can I get a no-cost Red Hat Enterprise Linux subscription?*
+
When you register and download Red Hat Enterprise Linux Server through link:#{site.base_url}/[developers.redhat.com], a no-cost Red Hat Enterprise Linux Developer Suite subscription will be automatically added to your account. We recommend you follow our link:#{site.base_url}/products/rhel/get-started/[Getting Started Guide] which covers downloading and installing Red Hat Enterprise Linux on a physical system or virtual machine (VM) using your choice of VirtualBox, VMware, Microsoft Hyper-V, or Linux KVM/Libvirt.

. *How do I tell if there is a container image available that has a newer version of Node.js?*
+
*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:
+
[.code-block]
```
# docker search registry.access.redhat.com/rhscl
# docker search registry.access.redhat.com/rhscl_beta
# docker search registry.access.redhat.com/openshift3
# docker search registry.access.redhat.com/rhel
# docker search registry.access.redhat.com/jboss
```
+
. *I can't find the `docker` rpm.*
+
*`yum` is unable to find the `docker` rpm.*
+
*When I try to install `docker`, `yum` gives the error _No package docker available_.*
+
The `docker` rpm is in the _rhel-7-server-extras-rpms_ software repository. It is only available for the server version of Red Hat Enterprise Linux. The _rhel-7-server-extras-rpms_ repository is disabled by default. See the first step in this tutorial for information on enabling additional software repositories.

. *Where can I learn more about delivering applications with Linux containers?*
+
If you haven't already joined the link:http://developers.redhat.com/[Red Hat Developers program], sign up at link:http://developers.redhat.com/[developers.redhat.com]. Membership is free. +
link:https://access.redhat.com/articles/1483053[Recommended Practices for Container Development] and many other container articles are available from the link:https://access.redhat.com/[Red Hat Customer Portal]. +
If you are a Red Hat Technology Partner, visit the link:https://access.redhat.com/articles/1483053[Container Zone] at the link:http://connect.redhat.com/[Red Hat Connect for Technology Partners] web site.
Loading