Skip to content

Latest commit

 

History

History
312 lines (208 loc) · 19.8 KB

get-started-atomic-python.adoc

File metadata and controls

312 lines (208 loc) · 19.8 KB

Path Intro section

Get started building Python application containers on Red Hat Enterprise Linux Atomic Host in under 15 minutes.

Prerequisites section title

Introduction and Prerequisites

Prerequisites section

In this tutorial, you will learn how to deploy a Python 3 application in containers on Red Hat Enterprise Linux Atomic Host, which is a secure, minimal-footprint OS optimized to run Linux containers. You will need to have Red Hat Enterprise Linux Atomic Host installed on a virtual (or physical) machine. Alternatively, If you are already running Red Hat Enterprise Linux 7 server, you can run this tutorial directly on your system after configuring your system to run docker.

You will need a current Red Hat subscription that allows you to download software and updates from Red Hat. If you are interested in purchasing a Red Hat subscription for development, the Red Hat Developer Suite is an affordable choice that includes Red Hat Enterprise Linux Server, Atomic Host, Red Hat Developer Toolset, and Software Collections. For developer support with a guaranteed response time service-level agreement (SLA), Red Hat Developer Workstation Professional or Enterprise subscriptions are available.

The best way to get started with Atomic Host depends on what OS you are using on your development system:

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 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.

If you encounter difficulties at any point in this tutorial, see Troubleshooting and FAQ.

Step1 Duration

5 minutes

Step1 Title

Prepare your system

Step2 Duration

5 minutes

Step2 Title

Setup your development environment

Step3 Duration

5 minutes

Step3 Title

Hello World and your first application

Step1 Content

In this step, you will download and install the latest updates from Red Hat for your Atomic Host installation. In the process, you will verify that your system has a current Red Hat subscription and is able to receive updates.

Note: If you are not using Atomic Host and following this tutorial on Red Hat Enterprise Linux Server, see Getting Docker on RHEL 7 in the article Get Started with Docker Formatted Container Images on Red Hat Systems.

The process for installing updates on Atomic Host is different than other Red Hat systems. On Atomic Host, updates are applied atomically in a single, indivisible step. Likewise, updates can also be rolled back in a single step. Most of the file systems on Atomic Host are mounted read-only, only /etc and /var are writable. Yum is not used for installing updates or software. Instead, the rpm-ostree system is used for installing updates. Software is installed using Docker containers.

Log in to your virtual (or physical) machine running Atomic Host as the root user, or use su or sudo bash to start a root session. If you haven’t registered your system you can do it with subscription-manager. Use the same the username and password that you use for logging in to the Red Hat Customer Portal, access.redhat.com.

# subscription-manager register --username=<username> --auto-attach

To install the latest updates, use the following command:

# atomic host upgrade

To see the newly installed version, use the following command:

# atomic host status

The asterisk at the beginning of the line indicates which version the system is current running. To make the update take affect, you must reboot the system.

# systemctl reboot

After the system reboots, atomic host status should confirm that the system is running the latest version.

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

Step2 Content

This step will download and install Python 3 using a container from the Red Hat container registry. Installing the Python 3 container will make Python 3 available for other containers on your system to use. 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 Python 3. Then, when someone installs your container, their system will automatically download the required Python 3 container directly from the Red Hat container registry.

The Python 3 container is part of Red Hat Software Collections, which provides the latest development technologies 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).

Note: If you are not using Atomic Host, but instead are following this tutorial on Red Hat Enterprise Linux Server, you should have already installed docker. See Getting Docker on RHEL 7 in the article Get Started with Docker Formatted Container Images on Red Hat Systems.

If you don’t have a root session running on your container host, log in as the root user, or use su or sudo bash to start a root session.

To download and install the Python 3 container, use the following command:

# docker pull registry.access.redhat.com/openshift3/python-33-rhel7

The docker images command should show the container image that was installed as well as any others that are on your system.

# docker images

Now start a bash shell inside the Python 3 container to have a look around. 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.

# docker run -it openshift3/python-33-rhel7 /bin/bash
bash-4.2$ which python3
/opt/rh/python33/root/usr/bin/python3
bash-4.2$ python3 --version
Python 3.3.2
bash-4.2$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
default      1     0  0 21:56 ?        00:00:00 /bin/bash
default     11     1  0 21:58 ?        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:

# docker ps -a
CONTAINER ID        IMAGE                        COMMAND             CREATED             STATUS                      PORTS               NAMES
c3e47aafe6d1        openshift3/python-33-rhel7   "/bin/bash"         23 seconds ago      Exited (0) 19 seconds ago                       high_kowalevski
# docker start high_kowalevski
high_kowalevski
# docker attach high_kowalevski

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 14:53 ?        00:00:00 /bin/bash
default     10     1  0 14:53 ?        00:00:00 ps -ef
bash-4.2$ exit

Since 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 high_kowalevski

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/openshift3
# docker search registry.access.redhat.com/jboss
# docker search registry.access.redhat.com/rhel

If you need help, see Troubleshooting and FAQ.

Step3 Content

In this step, you will create a tiny Hello World container that uses Python 3 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 index.html with the following contents:

index.html
<html>Hello, Red Hat Developers World!</html>

Now in the same directory, create a file named Dockerfile with the following contents, but change the MAINTAINER line to have your name and email address:

Dockerfile
FROM openshift3/python-33-rhel7:latest

MAINTAINER Your Name "[email protected]"

EXPOSE 8000

COPY . /opt/openshift/src

CMD /bin/bash -c 'python3 web.py'

Create the file web.py with the following contents:

web.py
#
# A very simple Python HTTP server
#

import http.server
import socketserver


PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()

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 index.html.

# docker build -t myname/pythonweb .

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

# docker images

Now run the container using docker run. The Python 3 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.

# docker run -d -p 8000:8000 myname/pythonweb

The run command returns a ID for the container that you can ignore. To check that the container is running, use docker ps. Take note of the name docker assigned to the running container.

# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                              NAMES
7d3b47bfded6        myname/pythonweb    "/bin/sh -c '/bin/ba   18 seconds ago      Up 17 seconds       8080/tcp, 0.0.0.0:8000->8000/tcp   grave_brown

Use curl to access the Python web server:

# curl http://localhost:8000/
<html>Hello, Red Hat Developers World!</html>

When you are done, stop the running container with the following command using the name obtained from running docker ps:

# docker stop grave_brown

Where to go next?

Get Started with Docker Formatted Container Images on Red Hat Systems — This article explains how to install docker on Red Hat Enterprise Linux and Atomic Host. It also provides a more extensive set of docker examples.

Getting Started with Red Hat Enterprise Linux Atomic Host — This article provides an overview of Atomic Host, how it is different, and how to use it.

Red Hat Enterprise Linux 7.1 Release Notes — includes information on recent updates to Atomic Host and Docker formatted Linux containers

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 developers.redhat.com.

Follow the Red Hat Developer Blog
http://developerblog.redhat.com/

Faq section title

Troubleshooting and FAQ

Faq section

  1. 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 or an evaluation.

  2. I don’t have a current Red Hat subscription, can I get an evaluation?

    If you don’t have a Red Hat Enterprise Linux subscription, you can try it for free. Get started with an evaluation at access.redhat.com/products/red-hat-enterprise-linux/evaluation. Downloading and installing Atomic Host requires a Red Hat Enterprise Linux Server evaluation. Typically, the recommended evaluation for developers is Red Hat Enterprise Linux Developer Workstation because it includes Red Hat Software Collections and the Red Hat Developer Toolset, however that evaluation does not include Atomic Host. Therefore, you should select an evaluation of Red Hat Enterprise Linux Server.

  3. When I start Atomic Host, I don’t see a graphical environment.

    Atomic Host is specifically optimized for deploying of 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. Since it does not include a graphical user interface or development tools, Atomic Host isn’t well suited for software development activities. Instead, developers should use Red Hat Enterprise Linux, which is design to suit many purposes including desktop and server installations. See Get Started with Docker Formatted Container Images on Red Hat Systems. After an application is developed and packaged in a container, developers can test them on Atomic Host to ensure it is ready for deployment. Atomic Host can be helpful for developers who are creating continuous integration/continuous delivery (CI/CD) environments.

  4. How do I tell there is a container image available that has a newer version of Python 3?

    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/openshift3
    # docker search registry.access.redhat.com/jboss
    # docker search registry.access.redhat.com/rhel
  5. Can I run and build docker containers on Red Hat Enterprise Linux?

    Red Hat Enterprise Linux includes docker, but it is not installed by default. See Getting Docker on RHEL 7 in the article Get Started with Docker Formatted Container Images on Red Hat Systems.

  6. 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.