Tech

How to run a Docker container in your OnApp cloud

Raymond Paxton

Raymond Paxton
Director of Product Strategy

Welcome to the first in a series of articles about the different options you have to use Container Servers in OnApp.

In this first article, we will talk about how to run a Docker container in a Container Server.

We’re also running a Container Servers webinar on 9th November, 10am EDT / 3pm GMT – so if you have more questions after reading this post, why not join us then and we can discuss in more detail!

 

So, what’s a Container Server?

OnApp Container Servers are a new feature in OnApp v5.1. They provide the ability to run Docker containers, and other services required to manage them. A Container Server is a CoreOS specialized virtual appliance that is lightweight and highly scalable. The tools required run Docker containers are installed, and OnApp exposes the ability to manage cloud-config via the OnApp web interface.

#1. Running a Docker container server

Let’s first look at how we can run a container server via cloud-config. This isn’t a complete example of a cloud-config (it doesn’t really make sense to deploy a single container inside a single container server…) but it shows the basic framework you need to get your own containers up and running.

Here’s the example cloud-config:

#cloud-config

coreos:
 units:
  - name: docker.service
   command: start

  - name: dockerapp.service
   command: start
   content: |
    [Unit]
    Requires=docker.service
    After=docker.service
    [Service]
    ExecStart=/usr/bin/docker run -d -p 80:80 nginx

Let’s break this down into parts. All cloud-configs start with ‘#cloud-config’ to denote that it’s a cloud-config file. After that, we have the ‘coreos’ section which allows you to configure different services under the CoreOS operating system – including the ‘units’ sub-sections for individual services.

Inside that sub-section we are required to add at least ‘name’ and ‘command’ fields for each service we want to run. The follow will start the Docker server within the container server:

  - name: docker.service
   command: start

Next we will create another service section with ‘dockerapp.service’ to start a Docker container, and this time we will create a ‘content’ section to provide further parameters:

  - name: dockerapp.service
   command: start
   content: |

Inside the content section we will configure parameters in two different sections.  First we will set the ‘[Unit]’ section to tell this service to wait till after the ‘docker.server’ has started, and that it’s required to run this service.  This is done using the following lines:

    [Unit]
    Requires=docker.service
    After=docker.service

Then under the ‘[Service]’ section we will tell the system to start a nginx docker container that will listen on port 80 of the container server, and translate to port 80 inside the container.  This is done by running the following commands:

    [Service]
    ExecStart=/usr/bin/docker run -d -p 80:80 nginx

#2. Running containers via command line

Now let’s look at how we can run containers via the command line.  Inside a Container Server that is already running the Docker server, you can start the same nginx docker container by running the following command:

docker-container-cli-1

If you want to view a list of docker containers running you can use:

docker-container-cli-2

Some other commands that may be of use are:

docker ps -a
  Will show a list of Docker containers including exited/failed
docker log <container_id>
  Will show the logs of that Docker container
docker top <container_id>
  Will show you the ‘top’ command of that Docker container
docker stop <container_id>
  Will stop that Docker container
docker kill <container_id>
  Will kill that Docker container

If you’re interested in viewing the command line of your new docker container, you can do so by using the following commands:

I hope this provide you with enough information to get started with Container Servers.

Look for future posts for other things you can do with a Container Server, such as running a clustered environment, and building your own container. And don’t forget the webinar!