Pages

Thursday, 19 December 2013

Docker LXC Impementation

Installing Docker

Linux kernel 3.8
Due to a bug in LXC, docker works best on the 3.8 kernel. Precise comes with a 3.2 kernel, so we need to upgrade it. The kernel you’ll install when following these steps comes with AUFS built in. We also include the generic headers to enable packages that depend on them, like ZFS and the VirtualBox guest additions.

Installing kernel 3.8 on Ubuntu Precise 12.04 (LTS) (64-bit)


# apt-get update
# apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring
# reboot

Installing kernel 3.8 on Ubuntu Raring 13.04 (64 bit)

# apt-get update
# apt-get install linux-image-extra-`uname -r`

Installing the docker binary

# mkdir /var/src
# wget --output-document=docker https://get.docker.io/builds/Linux/x86_64/docker-latest
# chmod +x docker
# cp -ap docker /usr/bin/docker

Docker initscript for ubuntu.

/etc/init.d/docker

#!/bin/sh

### BEGIN INIT INFO
# Provides:         docker
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the docker
# Description:       starts docker using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/docker
NAME=docker
DESC=docker
PID=/var/run/docker.pid
DAEMON_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d"

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

start() {
       start-stop-daemon --background --start --quiet --pidfile $PID \
           --retry 5 --exec $DAEMON --oknodo -- $DAEMON_OPTS
}

stop() {
       start-stop-daemon --stop --quiet --pidfile $PID \
           --retry 5 --oknodo --exec $DAEMON
}

case "$1" in
   start)
       log_daemon_msg "Starting $DESC" "$NAME"
       start
       log_end_msg $?
       ;;

   stop)
       log_daemon_msg "Stopping $DESC" "$NAME"
       stop
       log_end_msg $?
       ;;

   restart|force-reload)
       log_daemon_msg "Restarting $DESC" "$NAME"
       stop
       sleep 1
       start
       log_end_msg $?
       ;;
   status)
       status_of_proc -p $PID "$DAEMON" docker
       ;;

   *)
       echo "Usage: $NAME {start|stop|restart|status}" >&2
       exit 1
       ;;
esac

exit 0

Save the file and make it executable
# chmod +x /etc/init.d/docker

Add the docker in startup
# update-rc.d -f docker defaults

Start the docker
# /etc/init.d/docker start

Check the docker process
# ps ax | grep docker
Output
xxxx ?        Rl     0:01 /usr/bin/docker -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d
# netstat -tnlp | grep docker
Output
tcp6       0      0 :::4243                 :::*                    LISTEN      xxxx/docker     

Try creating and starting a test docker image
# docker run -i -t ubuntu /bin/bash
This will download the ubuntu image and start a new container

Checking Docker process
# docker ps -a

Stopping a docker
# docker stop <container-id>

Removing a docker
# docker rm <container-id>

Docker for implementing multiple applications

# docker run -i -t ubuntu /bin/bash

After getting the console of docker container follow the below steps

# apt-get update
# echo "deb http://archive.ubuntu.com/ubuntu precise universe" >> /etc/apt/sources.list
# apt-get install -y python-software-properties python g++ make
# add-apt-repository -y ppa:chris-lea/node.js
# echo "deb http://archive.ubuntu.com/ubuntu precise universe" >> /etc/apt/sources.list
# apt-get update
# apt-get install -y nodejs=0.10.22-1chl1~precise
# npm -v
# node -v
# redis-cli --version
# apt-get install redis-server
# mkdir /opt/script
# vim /opt/script/start.sh
   #!/usr/bin/sh
   /etc/init.d/redis-server start
   node /path/to/app/index.js

Once the above steps are done get one more terminal access to the server where the container is running
Now commit the image from the container that is still running

Get the container ID
# docker ps -a
CONTAINER ID        IMAGE                         COMMAND                CREATED             STATUS              PORTS                   NAMES
6972293657e2        ubuntu:latest                   /bin/bash                       5 mins ago        up about 5 mins 0                                      yellow_squirrel

Use the container id to create a new image

# docker commit <<container-id>> <<image-name>>
For ex.
# docker commit 6972293657e2 nodejs-redis

Now use the above image to create multiple docker containers

Start the container in daemon mode or detach it.
# docker run -d nodejs-redis sh /opt/script/start.sh

Inspect the current docker container
# docker inspect <container-id>

Mapping port to the container.
# docker run -d -P nodejs-redis sh /opt/script/start.sh
Finding the ports mapped to docker.
# docker port <container-id> <portno>

No comments:

Post a Comment