Use Rancher to Deploy an Application

Table of Contents

Introduction

Learn how to use Rancher to deploy an application in one or more Docker containers. Rancher offers native support for running and managing Docker Swarm containers, and allows you to define access policies and manage multiple clusters from one control plane. After you have installed and used the Rancher UI, it becomes a convenient way to launch applications in Docker containers.

Requirements

  • One or more 1&1 Cloud Servers running Linux.
  • Rancher installed and running.
  • A free Docker Hub repository account. If you do not already have one, you can sign up here.
  • A Docker image for your application. Note: For this tutorial, we will use an example Docker image and Dockerfile.

For more information on Rancher, see our article on installing and using Rancher.

For more information on Docker, consult these related articles:

Create an Example Docker Image and Dockerfile

For this tutorial, we will be creating an example Dockerfile using the official Docker training/webapp image.

If you already have a Dockerfile prepared for your application and published to Docker Hub, you can skip this section.

Launch a Test Container

As a test, run the following command:

sudo docker run --name my-python-test -d -p 49155:5000 training/webapp python app.py

This command will pull the training/webapp image, and launch the Python application app.py which outputs a simple "Hello world!" message. The command maps the container's port 5000 to the server's port 49155 so that you can see the message in a browser.

Note: If you are using a firewall, you will need to allow access to port 49155 in order to view the Docker container's application.

After the Docker container has been launched, you can view the Python application by accessing port 49155 in a browser with the URL:

You will see the message "Hello world!"

Now that you have verified that the container will run on your system, save it as a Docker image named test-python-webapp with the command:

sudo docker commit -m "Example Python webapp" -a "[your name]" my-python-test [Your Docker Hub username]/test-python-webapp

For example, if your Docker Hub username is jdoe, the command is:

sudo docker commit -m "Example Python webapp" -a "J Doe" my-python-test jdoe/test-python-webapp

Before you continue, stop the test container and remove it with the commands:

sudo docker stop my-python-test
sudo docker rm my-python-test

Publish the Image to Docker Hub

Rancher will need to be able to access your Docker image from a Docker repository in order to deploy containers. To publish your Docker image to Docker Hub, log into the website and click Create Repository.

Rancher Deploy: Click Create Repository

Fill out the name for the repository (test-python-webapp). Enter a short description and full description (optional). Set the visibility to Public, then click Create.

Rancher Deploy: Create the Docker Hub repository

You will need to authenticate with Docker Hub from the command line before you can publish your image. Back at the SSH terminal, run the command:

sudo docker login

At the prompts, enter your Docker username and password.

Publish your Docker image with the command:

sudo docker push [your Docker Hub username]/test-python-webapp

For example, if your Docker Hub username is jdoe the command is:

sudo docker push jdoe/test-python-webapp

Create the Host Server(s)

Now that the Docker image is available from the Docker Hub repository, we can use Rancher to launch a container from the image.

In a browser window, log into your Rancher UI and go to Infrastructure > Hosts.

Rancher Deploy 3: Infrastructure Hosts

This is where you will create and label the hosts that are required for your project. You can create as many hosts as you need for the application.

In this case, our project is a simple example app, and we only need to create one host to run it. Click Add Host to add a host for the app.

Rancher Deploy 4: Add the host

There are several options on the next page. You can add labels to help organize your containers, and specify the IP address where the container(s) should be launched.

For this example, just copy and paste the specified command into the command line to register the host with Rancher. Then click Close.

Rancher Deploy 5: Register the host

You will be redirected to the Hosts page. After a few moments, the host will appear on this page.

Deploy the Application

After the host is available in the Rancher UI, click Add Container.

Rancher Deploy 11: Add container

Fill out the fields:

  • Name: test-python-webapp
  • Description: "A test Python application."
  • Select Image: [your Docker Hub username]/test-python-webapp

Then click + Port Map to expand the next section.

Rancher Deploy 12: Container details

Fill out the fields in the port forwarding section:

  • Public Host [IP:] Port: 192.168.0.1:49155 (Replace the IP address with your server's IP address).
  • Private Container Port: 5000

Rancher Deploy 9: Port forwarding

Then scroll down to the next section. In the Command field enter:

python app.py

Rancher Deploy 10: Enter command

Then scroll to the bottom and click Create. You will be redirected to the Stack page, where you can watch the progress of the deployment.

After Rancher has finished the deployment, you can view the test Python application by accessing port 49155 in a browser with the URL:

Content provided by 1&1

Comments

Tags: Rancher / Management