Docker web server on Ubuntu Server 16.04

Posted: October 9, 2016 in Linux

With Docker we can create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.For detailed explanation check Linux containers and Docker explained

Installing docker

Update the system and install CA:

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates

Add the GPG key for the official Docker repository:

sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Add the Docker repository to APT sources:

Create /etc/apt/sources.list.d/docker.list file and add entry: deb https://apt.dockerproject.org/repo ubuntu-xenial main

sudo touch /etc/apt/sources.list.d/docker.list
sudo vi /etc/apt/sources.list.d/docker.list

capture

Update the package database with the Docker packages from the repository we’ve just added:

sudo apt-get update

Purge the old repo if it exists.

sudo apt-get lxc-docker

Check if new repository is successfully added:

apt-cache policy docker-engine

capture

Install docker:

sudo apt-get install -y docker-engine

Check if docker daemon is running:

sudo systemctl status docker

Capture.PNG

Test if docker runs as expected:

sudo docker run hello-world

capture

Start docker daemon with system:

update-rc.d docker enable

Creating docker web server

Now when we’ve installed docker,we can build docker web server.In Docker terminology, there are images and there are containers. The two are closely related, but distinct.

An image is really a template that can be turned into a container, essentially it’s a snapshot of a container.A container is instance of the image.To turn an image into a container, the Docker engine takes the image, adds a read-write filesystem on top and initialises various settings including network ports, container name, ID and resource limits. A running container has a currently executing process, but a container can also be stopped (or exited to use Docker’s terminology). An exited container is not the same as an image, as it can be restarted and will retain its settings and any filesystem changes.

So to build a container we need to specify an image from which container will be derived from:

In this example i used ubuntu image,it’s like clean Ubuntu installation,We can search available images:

sudo docker search ubuntu --no-trunc

In this example i searched all ubuntu images and will use selected image for docker web server creation:

Capture.PNG

docker run --name web --hostname web -m 2g -p 80:80 -P -i -t ubuntu /bin/bash

–name give container descriptive name

–hostname giving container hostname (by default container hostname is something like a23dvef)

-m 2g (allocate 2GB of RAM to this container

-p publish container port(s) to the docker host

-P without this switch,docker web site would be accessible only from docker host,not from outside computers

The -t and -i flags allocate a pseudo-tty and keep stdin open even if not attached. This will allow you to use the container like a traditional VM as long as the bash prompt is running,in this example we want to interact with container,so we attached /bin/bash

ubuntu (behind -t) is image from which we want to create container

After creating container you should be redirected to container’s bash

root@web:/# hostname
web

Update container:

apt-get update -y

Install apache,net tolls and vi editor:

apt-get install apache2 vim net-tools -y

Just for fun,i edited index.html:

vi /var/www/html/index.html

capture

Restart apache service:

service apache2 restart

Check current ip address:

root@web:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link

Test web access from docker host (it’s VM with bridged networking) with IP 192.168.0.10

capture

Now test access from Win 10 lap-top (on which Ubuntu server 16.04,as docker host is installed).During docker web server container installation,i bound port 80 of docker host (192.168.0.10) to docker port,that’s why i targeting 192.168.0.10 address from my Win 10

Capture.PNG

To leave docker container running press CTRL-P and CTRL-Q

To enter back in already running container:

docker exec -i -t web /bin/bash

Check if docker container is running:

docker inspect --format '{{.State.Running}}' web
true

Or:

docker ps

capture

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s