Docker and Oracle Container Cloud Service - a practical introduction
The objective of this post is to give a hands-on view on how to get started with Docker with a scenario where we containerize a local web application, push the corresponding image to docker hub, and deploy the container to Oracle public cloud with Container Cloud Service.
1. Hands-on introduction to Docker containers
Pre-requisite : create an account on Docker Hub (hub.docker.com) and one on Oracle Container Cloud Service (free trials available).
My config is a Virtualbox with Ubuntu Mate running on it, with installed Docker on it (version used here is 1.13)
Once Docker installed on my local linux virtual machine, I make sure my Docker config is up and running with this command :
systemctl status docker
Which should give something like :
Next, make sure your configuration is OK, especially if you're in an internal network it's time to check that you don't have a connectivity issue towards the public Docker registry :
docker run hello-world
Now we see that the image is pulled from that registry and we get as an output this
status: Downloaded newer image for hello-world:latest Hello from Docker.
My next step is to pull and run a Docker image from the public registry. I choose Tomcat official image from Docker Hub. The beauty of the Docker run command is that it first checks locally for the image, and if it doesn't find it - which is the case the first time here - it will pull the image from Docker public registry. MyTomcatServer2 is the name we give to our container, while -d allows to run this in background.
docker run -it -d --rm --name MyTomcatServer2 tomcat
If you do a docker ps you should have two running containers, a HelloWorld and MyTomcatServer2 .
Here is a useful command which is not very clearly documented in the different tutorials I've seen, the exec command. It allows you to explore a container as you were inside it.
docker exec -t -i MyTomcatServer2 /bin/bash
From there, you shoud recognize a more familiar directory structure of Tomcat, especially we notice the /webapps folder where we usually drop our sources/war files.
Once I've done that, I go back to my normal prompt just by typing exit
To make sure everything is fine, we can check from our VM browser that Tomcat is up and running.
The IP address and port are visible from the Docker inspect command :
docker inspect MyTomcatServer2
Going back to our browser, we can now type safely the ip address and the right port :
Now let's deploy an application in our container. In my case, I've developed a HTLM5/CSS responsive webdesign template using the Responsive.gs library. The code can be found here :
https://github.com/hajeriwalid/HTML5tests.git
To do that I've used one method which is the copy command. Notice the double quotes because of the whitespace :
docker cp "/media/sf_DevAndTests/Web/HTML5tests/responsive project" MyTomcatServer2 :/usr/local/tomcat/webapps/
Going back to the browser now, using the right url to access our newly deployed web app:
Once our container is running well, I decide to create my own image from it, and to push it to my docker public repository.
This command will create the image :
docker commit -a "Walid Hajeri" -m "first commit - tomcat server with sample web app" MyTomcatServer2 waldock/walidhajerirepo:dev
The last section of the command had to follow a certain convention, and the confusion might come from the narrow definition of a repository according to Docker, ie a collection of different docker images with same name, that have different tags :
<dockerhubusername>/<repositoryname>:tag
After executing this command, we now have a new Docker image, which is static and reusable, and from which we can re-create new containers in the future. To check that we succesfully created an image, we see it listed among other images with the docker images command :
From there we just need to login to Docker Hub and so that image can be pushed with this simple command :
To double check that our push has been done towards the right place, a web browser is enough. We can see that even the dev tag name is there.
2. Deploy a first container to Oracle Container Cloud Service
Great, so far we've worked between our local virtual machine and our public repository on docker hub. What I'm going to do right now is to deploy a container from that public repository to Oracle Public Cloud.
Oracle Container Cloud Service provides an easy and quick way to create a container infrastructure. It provides tooling to compose, deploy, orchestrate and manage Docker container-based applications.
First and foremost, we need to decide how much computing resources we will allocate to deploy our container ( and container stacks). That's the objective of the instance creation step (create service button) which is really straightforward :
The interesting part is the number of worker nodes because that's where our container will be running. Each worker node is in fact a virtual machine running on Oracle public cloud. The role of the on the manager node is to host Oracle Container Cloud Service software and to orchestrate the deployment of Docker containers to the worker nodes within the same instance. I generate the ssh key pair directly from the interface and download it in a safe place locally.
After some time we get our compute resources ready for container deployments :
Once our resources are ready, we can click on the "hamburger" menu on the right to access Container Console with the credentials we just defined.
I won't show here a full real-world process nor all the features, but I'll go for one quick way to deploy our container by clicking on the Quick Start Wizard on the top right.
I leave the resource pool organization default, meaning my 4 worker nodes on the default pool, but I could reorganize my resources into a dev/test/prod setting if I wanted.
The second step is about selecting an image. Instead of taking a pre-built image, you'll recognize the same command we used locally to create and launch our container. We're in a simple use case here with a single container, but Container Cloud Service has a lot more to offer when you have multiple containers to deploy with its orchestration and scheduling features. The Advanced editor will enable you for example to
- Decide the policy in terms of deployment of your container, for example launching a container in the host with the least amount of CPU and available memory.
- Import a Docker Compose file (https://docs.docker.com/compose/compose-file/ )
Our future container will be part of a deployment stack, this will be useful in the future when we need to add a database container, a load balancer container, to the same stack, so that we can easily redeploy them together pre-configured as a stack. Stacks in OCCS are YAML files, very similar to Docker Stacks but I'm not sure if both formats are fully compatible at this stage.
That's it, I need one container only, deployed to my default resource pool :
It took me less than 10 seconds to get my container created from the previously pushed image deployed to Container Cloud Service. Here is the result :
If you're curious and want to see exactly in which worker node of your resource Pool the container has been deployed, the Resource Pool shows it.
That's it ! Want to try Oracle Cloud Platform - and especially Container Cloud Service ? Start here https://cloud.oracle.com/en_US/tryit
Note: The views expressed on this post are my own and do not necessarily reflect the views of Oracle.