Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Build your first application using Python 3.5 on RHEL 7 with containers and Red Hat Software Collections

January 19, 2017
mguerett
Related topics:
ContainersLinux
Related products:
Red Hat Enterprise Linux

Share:

    Get started building Python 3.5 applications in docker containers on Red Hat Enterprise Linux in under 15 minutes.

    Introduction and Prerequisites

    In this tutorial, you will learn how to start building Python 3.5 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 Python 3.5 container image from Red Hat Software Collections (RHSCL) 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 get a no-cost Red Hat Enterprise Linux Developer Suite subscription for development purposes by registering and downloading through developers.redhat.com.

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

     

    1. Prepare your system

    5 minutes

    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.

    $ su -
    # subscription-manager repos --list

    Enable the two additional repositories:

    # 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:

    1. Update your system with any available software updates

    2. Install docker and a few additional rpms using yum

    3. Configure the docker daemon to start at boot time

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

    $ su -
    # yum update

    Install docker and necessary additional rpms:

    # yum install docker device-mapper-libs device-mapper-event-libs

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

    # systemctl enable docker.service
    # systemctl start docker.service

    Now verify that the docker daemon is running:

    # 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 and FAQ.

     

    2. Run your first container

    5 minutes

    This step will download and install Python 3.5 using a container image from the Red Hat container registry, a repository of container images. Installing the Python 3.5 container image will make Python 3.5 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 Python 3.5. Then, when someone installs your container, their system will automatically download the required Python 3.5 container directly from the Red Hat container registry.

    The Python 3.5 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 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 Python 3.5 container image, use the following command:

    # docker pull registry.access.redhat.com/rhscl/python-35-rhel7

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

    # 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 Python 3.5 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.

    # docker run -it rhscl/python-35-rhel7 /bin/bash
    bash-4.2$ which python3
    /opt/rh/rh-python35/root/usr/bin/python3
    bash-4.2$ python3 --version
    Python 3.5.1
    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 17:58 ?        00:00:00 /bin/bash
    default     10     1  0 17: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
    84458ca538fb        rhscl/python-35-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.

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

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

     

    3. Build Hello World in a container

    5 minutes

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

    ./

    <html>Hello, Red Hat Developers World from Python!</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 rhscl/python-35-rhel7
    
    MAINTAINER Your Name "your-email@example.com"
    
    EXPOSE 8000
    
    COPY . /opt/app-root/src
    
    CMD /bin/bash -c 'python3 -u 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 web.py.

    # 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 container 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/pythonweb`

    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/pythonweb container image you created.

    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
    c7885aa23773        myname/pythonweb    "container-entrypoint"   6 seconds ago       Up 4 seconds        0.0.0.0:8000->8000/tcp, 8080/tcp   helloweb

    Use curl to access the Python web server:

    # curl http://localhost:8000/
    Hello, Red Hat Developers Worldfrom Python!

    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/pythonweb

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

    Want to know more about what you can do with RHEL?

    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
    https://developers.redhat.com/blog/

    Troubleshooting and FAQ

    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.

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

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

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

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

    Last updated: November 1, 2023

    Recent Posts

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue