Configure "Hello World" with Weave and Docker on Ubuntu

July 23, 2015 | By
| Reply More

Today, we'll learn about how to get started with Weave and Docker in our machines running Ubuntu as Operating System. Weave creates a virtual network that connects Docker containers with each other, deploys across multiple hosts and enables their automatic discovery. It allows us to focus on developing our application, rather than our infrastructure. It provides such an awesome environment that the applications uses the network as if its containers were all plugged into the same network without need to configure ports, mappings, link, etc. The services of the application containers on the network can be easily accessible to the external world with no matter where its running. Similarly, the existing internal systems can also be exposed to application containers though they are running in different places. It can traverse firewalls and operate in partially connected networks and the traffic can be encrypted, allowing hosts to be connected across an untrusted network. We can easily construct applications running in multiple containers running anywhere connected in the weave network.

Here in this tutorial, we'll run an application running hello world service in a docker container on a host and we'll access the application with a docker container on another host connected in the same Weave network. Here, we'll use Vagrant to create two hosts running Docker and Weave Router each, if one has two or more separate physical or virtual host, we can install docker and weave router and continue with step 4. So, here are some easy and simple way to setup the hello world service application with Weave and Docker running Ubuntu as its Operating System.

Prerequisites

Here, some prerequisites that we need to fullfill in order to get started.

Git

As we are running Ubuntu 15.04 in our machine, we'll gonna use apt-get to install it.

# apt-get install git

Virtual Box

Now, we'll gonna setup Virtual Box in our Ubuntu machine so to virtualize the Ubuntu hosts with our Vagrant. We should make sure that we have enabled multiverse in our sources.list.

# apt-get update
# apt-get install virtualbox

1. Installing Vagrant

Next, we'll need to setup Vagrant so as to virtualize and control over the two hosts running Ubuntu with Virtual Box in our Ubuntu Machine.

# wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.deb

# dpkg -i vagrant_1.7.2_x86_64.deb

Note: Please replace dpkg with the package manager you have in your distribution of Linux.

2. Setting up our hosts

We'll now clone the repository of Weave containing the vagrantfile and configuration in order to setup Vagrant hosts running Ubuntu in our machine.

# git clone http://github.com/fintanr/weave-gs

Cloning into 'weave-gs'...
remote: Counting objects: 458, done.
remote: Compressing objects: 100% (69/69), done.
remote: Total 458 (delta 31), reused 0 (delta 0), pack-reused 371
Receiving objects: 100% (458/458), 295.90 KiB | 0 bytes/s, done.
Resolving deltas: 100% (216/216), done.
Checking connectivity... done.

Now, we'll use Vagrant to setup and configure two hosts assigned with IP addresses on a private network and named weave-gs-01 and weave-gs-02 and install Docker. To do so, we'll need to run the following command.

# cd weave-gs/ubuntu-simple
# vagrant up

The above command here, will pull down and configure an Ubuntu 14.04 Trusty image which may take some time from few minutes to hours depending upon your internet connection. We may get prompted for a password when /etc/hosts is being updated during the Vagrant setup, here we'll just hit return at this point.

Once the setup of the hosts is complete we can check their status with vagrant status command.

# vagrant status

So, here are the two hosts and hostname that we'll use in this tutorial.

172.17.8.101 weave-gs-01
172.17.8.102 weave-gs-02

3. Installing Weave

Now, we'll need to install Weave in each of the hosts using Vagrant as shown below.

# vagrant ssh weave-gs-01
$ sudo wget -O /usr/local/bin/weave https://github.com/weaveworks/weave/releases/download/latest_release/weave
$ sudo chmod a+x /usr/local/bin/weave

# vagrant ssh weave-gs-02
$ sudo wget -O /usr/local/bin/weave https://github.com/weaveworks/weave/releases/download/latest_release/weave
$ sudo chmod a+x /usr/local/bin/weave

4. Starting Weave

Next, we'll need to start weave in each of our hosts.

On host weave-gs-01

$ sudo weave launch &&  weave launch-dns

Unable to find image 'weaveworks/weaveexec:1.0.1' locally
1.0.1: Pulling from weaveworks/weaveexec
511136ea3c5a: Pull complete
a5b60fe97da5: Pull complete
...
Unable to find image 'weaveworks/weavedns:1.0.1' locally
1.0.1: Pulling from weaveworks/weavedns
Digest: sha256:005b100d86af6fab5a616d15b37f19f6483b662a4ed045754cc10bc8795b62ee
Status: Downloaded newer image for weaveworks/weavedns:1.0.1
2e2841ca79c0feebbce441bd92141d27a4415dcd563656e98e9625e141cfb0a1

On host weave-gs-02

$ sudo weave launch 172.17.8.101 && weave launch-dns

Unable to find image 'weaveworks/weaveexec:1.0.1' locally
1.0.1: Pulling from weaveworks/weaveexec
511136ea3c5a: Pull complete
a5b60fe97da5: Pull complete
...
Status: Downloaded newer image for weaveworks/weave:1.0.1
944fd3604bc85d40f892f74f8c7ef1266a7a255eeb78103f4d22578774238bb4
Unable to find image 'weaveworks/weavedns:1.0.1' locally
1.0.1: Pulling from weaveworks/weavedns
Digest: sha256:005b100d86af6fab5a616d15b37f19f6483b662a4ed045754cc10bc8795b62ee
Status: Downloaded newer image for weaveworks/weavedns:1.0.1
1fe283df4541a86d34f464ea8206afe71bafc65428baace6de1f60ca218fbc95

Hence, our two hosts are now connected to each other, and any subsequent containers we launch with Weave will be visible to other connected container it is aware of. As this is our first time of running weave in our host, the above command will download a docker image for the weave router container and will launch the container. On our first host weave-gs-01, we have launched a Weave router container and on our second host weave-gs-02, we  have launched another Weave router container with the IP address of our first host so that Weave on weave-gs-02 will peer with the Weave on weave-gs-01.

As we have launched a single container in each of the hosts, we are able to see the running docker in either of the host.

$ sudo docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e2841ca79c0 weaveworks/weavedns:1.0.1 "/home/weave/weavedn 4 minutes ago Up 4 minutes 172.17.42.1:53->53/udp weavedns
0e2f1b4410d2 weaveworks/weave:1.0.1 "/home/weave/weaver 4 minutes ago Up 4 minutes 0.0.0.0:6783->6783/tcp, 0.0.0.0:6783->6783/udp weave

Then we'll see the output as shown below in which we can see the information about our running container.

We can see our peered network by running the following command.

$ sudo weave status

5. Launching Hello World Service

Now, after we have launched our weave router container successfully on both of our hosts. We'll now use weave to run a docker image containing an apache webserver with Hello World service and assign it an address of 10.0.1.1. The docker image that we are gonna run is downloaded from the Docker Hub. Now, to do so, we'll run the container in our first host ie weave-gs-01.

On weave-gs-01 run

$ sudo weave run 10.0.1.1/24 -t -i fintanr/weave-gs-simple-hw

Unable to find image 'fintanr/weave-gs-simple-hw:latest' locally
Pulling repository fintanr/weave-gs-simple-hw
86b35d37d9a8: Pulling image (latest) from fintanr/weave-gs-simple-hw
86b35d37d9a8: Pulling image (latest) from fintanr/weave-gs-simple-hw, endpoint: https://registry-1.docker.io/v1/
86b35d37d9a8: Pulling dependent layers
511136ea3c5a: Download complete
...
86b35d37d9a8: Pulling fs layer
86b35d37d9a8: Download complete
86b35d37d9a8: Download complete
Status: Downloaded newer image for fintanr/weave-gs-simple-hw:latest
78eebc1ab4dac62041a0370254ceed96c725ebd932bd84a2626a1b0d9676dd76

Here, the container is now registered with router and is accessible to other containers registered with router across multiple hosts.

6. Creating Client Container

Now, as we have out apache web server with Hello World running, we want to create a container on our second host and connect to the webserver in the container on our first host. Here, we'll run a container named fintanr/weave-gs-ubuntu-curl so that we can try to curl to our first host which has apache web server running.

$ CONTAINER=`sudo weave run 10.0.1.2/24 -t -i fintanr/weave-gs-ubuntu-curl`

Unable to find image 'fintanr/weave-gs-ubuntu-curl:latest' locally
Pulling repository fintanr/weave-gs-ubuntu-curl
0063f07cd18f: Pulling image (latest) from fintanr/weave-gs-ubuntu-curl
0063f07cd18f: Pulling image (latest) from fintanr/weave-gs-ubuntu-curl, endpoint: https://registry-1.docker.io/v1/
0063f07cd18f: Pulling dependent layers
511136ea3c5a: Download complete
...
0063f07cd18f: Pulling fs layer
0063f07cd18f: Download complete
0063f07cd18f: Download complete
Status: Downloaded newer image for fintanr/weave-gs-ubuntu-curl:latest

Here, the container id of the container we just run above will be used in future so a variable CONTAINER is assigned to it.

Now, we'll wanna attach to our Docker container using the CONTAINER variable we had assigned above.

$ sudo docker attach $CONTAINER

After we attach the container, we'll wanna run curl to the ip address of the first host which is running Apache Web Server with Hello World as Homepage.

$ curl http://10.0.1.1

After that, we'll see output in a JSON string as.

{
"message" : "Hello World",
"date" : "2015-06-25 17:37:26"
}

Now, as we can see what wanted to get ie. connect to the Apache Web server container running in a host from another container running in another host irrespective to the location where it is running, so we can now exit the container.

Conclusion

Hurray, we have successfully connected a container running Hello Word Apache web server in a host with a container running ubuntu docker container with curl in another host using the Weave Router. It is really an awesome routing docker image that creates a virtual network to connect Docker containers with each other and allows us to focus on developing our application, rather than our infrastructure. We can easily construct applications consisting of multiple containers, running anywhere. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you ! Enjoy :-)

Filed Under : UBUNTU HOWTO

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.