Installing Docker on CentoOS VM with VirtualBox

New To VirtualBox and CentOS?

If you do not already have VirtualBox with a CentOS VM follow the following links:


1. Download the lastest version of VirtualBox from http://download.virtualbox.org/virtualbox/

2. Download a CentOS image http://wiki.centos.org/Download

I suggest the LiveKDE iso :)

3. Create a new machine for Type: Linux Version: Red Hat

4. After you have selected the specifications for the new machine start the machine and browse to pick the iso image that you downloaded.

6. After the machine has booted from the iso click the icon on the desktop to install to hard drive and follow the instructions and disregard any warnings about erasing the hard disk since that is the VirtualBox created disk for the VM it is ok.

CentOS 6 only step to update linux kernel

A new Linux kernel is required to run docker. If you are running CentOS 6 chances are that you have an older kernel that is not supported by docker. A modified version of docker is managed by the EPEL repositiory which should still work with kernel 2.6.

To get kernel 2.6 EPEL docker-io add the EPEL repo:

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo rpm -Uvh epel-release-6*.rpm
sudo yum --enablerep=epel-testing install docker-io

if you see below error:

Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Could not get metalink https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=x86_64 error was
14: problem making ssl connection
Error: Cannot retrieve metalink for repository: epel-testing. Please verify its path and try again

open /etc/yum.repos.d/epel-testing.repo

un-comment baseurl and comment mirrorlist than try again

See https://docs.docker.com/installation/centos

If you are using CentOS 7 or would like to use the kernel 2.6 compatible EPEL docker-io, skip to the next section.

1. Before updating your kernel make a machine snapshot with your 2.6 kernel calling the snapshot <vm name>_kernel_<2.6>

2. To install the new kernel from the EL Repository, add the following file as /etc/yum.repo.d/elrepo.repo

[elrepo]
priority=10
name=ELRepo.org Community Enterprise Linux Repository - el6
baseurl=http://elrepo.org/linux/elrepo/el6/x86_64/
mirrorlist=http://elrepo.org/mirrors-elrepo.el6
protect=0
[elrepo-testing]
enabled=0
priority=10
name=ELRepo.org Community Enterprise Linux Testing Repository - el6
baseurl=http://elrepo.org/linux/testing/el6/x86_64/
[elrepo-kernel]
enabled=0
priority=10
name=ELRepo.org
baseurl=http://elrepo.org/linux/kernel/el6/x86_64/

 

3. Install the latest mainline kernel with:

rpm --import http://elrepo.org/RPM-GPG-KEY-elrepo.org
yum install yum --enablerepo=elrepo-kernel install kernel-ml


4. Reboot VM and hit any key to enter the GRUB menu.

5. From the GRUB menu pick kernel 3.x to boot with

6. Remove old kernel headers with:

yum remove kernel kernel-devel kernel-headers > ./removedPackages.txt


7. Install new kernel headers with:

yum --enablerepo=elrepo-kernel install kernel-ml-headers kernel-ml-devel

8. Review removedPackages.txt from step 7 and reinstall removed packages with new kernel

9. Create a new snapshot for new kernel version.

Install VirtualBox Guest Additions

Install VirtualBox Guest Additions to share files from host to VM and docker containers

1. Download guest additions from http://download.virtualbox.org/virtualbox/

2. Power off VM

3. Go to Settings from VirtualBox Manager

4. Click VboxGuestAdditions.iso and click the disk icon and select the VBoxGuestAdditions.iso file that was downloaded.

5. Restart the VM

6. Yum install gcc kernel-devel kernel-headers dkms or if you have kernel-ml from above yum install gcc dkms

7. /media/VBoxLinuxAdditions.run

8. Power off VM from Machine -> close

9. From VirtualBox Manager choose VM -> Settings -> Shared folders

10. Add a new folder for sharing from host to docker, you may create as may as you like and use any name.

11. After you have restarted mount share/s with:

mkdir /mnt/for_docker
mount -t vboxsf -o uid=1000,gid=1000,rw,exec,dmode=0777,fmode=0777 for_docker /mnt/for_docker

Save the mount command in /etc/rc.local for mounting on reboot

Create a new hard drive partition for Docker data dir

1. From VirtualBox Manager -> Storage -> Controller: SATA -> Add Hard Disk

2. Create new VDI disk with dynamically allocating space.

3. Rename it newHD and a good idea is to pick 20 GB at least keep in mind one Docker image may be 200 MB to start and become 1 GB after application software is installed.

4. Start machine

5. Run fdisk -l to list all disks and look for the new disk which is not partitioned, you will see a disk which has:

Disk /dev/sdb: 21.7 GB, 21719023616 bytes
255 heads, 63 sectors/track, 2640 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

But does not yet have the partition details under it ex:

Device Boot Start End Blocks Id System
/dev/sdb1 1 2641 21209921 83 Linux

6. Run fdisk /dev/sdb

Command (m for help): n

p primary partition (1-4): p

Partition number (1-4): 1

First cylinder (1-1566, default 1): (enter)

Last cylinder, +cylinders or +size{K,M,G} (1-1566, default 1566): (enter)

Command (m for help): w

7. Format the partition to use the ext4 filesystem using:

mkfs -t ext4 /dev/sdb1

8. Mount the disk to /mnt/docker_data

mkdir /mnt/docker_data

chmod 665 /mnt/docker_data

mount /dev/sdb1 /mnt/docker_data

Add the following in /etc/fstab to mount on reboot:

/dev/sdb1 /mnt/docker_data ext4 defaults 1 2

Install Docker As Daemon

If you are running CentOS 7 or upgraded your kernel in the steps above you may download docker as stated in https://docs.docker.com/installation/binaries with:

wget https://get.docker.com/builds/Linux/x86_64/docker-latest -O docker

chmod +x ./docker

ln -s /bin/docker ./docker

1. Run mountcgroup.sh below:

!/bin/sh
# Copyright 2011 Canonical, Inc
#           2014 Tianon Gravi
# Author: Serge Hallyn 
#         Tianon Gravi 
set -e

# for simplicity this script provides no flexibility

# if cgroup is mounted by fstab, don't run
# don't get too smart - bail on any uncommented entry with 'cgroup' in it
if grep -v '^#' /etc/fstab | grep -q cgroup; then
	echo 'cgroups mounted from fstab, not mounting /sys/fs/cgroup'
	exit 0
fi

# kernel provides cgroups?
if [ ! -e /proc/cgroups ]; then
	exit 0
fi

# if we don't even have the directory we need, something else must be wrong
if [ ! -d /sys/fs/cgroup ]; then
	exit 0
fi

# mount /sys/fs/cgroup if not already done
if ! mountpoint -q /sys/fs/cgroup; then
	mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
fi

cd /sys/fs/cgroup

# get/mount list of enabled cgroup controllers
for sys in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups); do
	mkdir -p $sys
	echo $sys
	if ! mountpoint -q $sys; then
		if ! mount -n -t cgroup -o $sys cgroup $sys; then
			rmdir $sys || true
		fi
	fi
done

# example /proc/cgroups:
#  #subsys_name	hierarchy	num_cgroups	enabled
#  cpuset	2	3	1
#  cpu	3	3	1
#  cpuacct	4	3	1
#  memory	5	3	0
#  devices	6	3	1
#  freezer	7	3	1
#  blkio	8	3	1

exit 0

2. Run the following commands to enable net access in the docker containers :

sudo echo "1" > /proc/sys/net/ipv4/ip_forward
sudo sysctl net.ipv4.ip_forward

add the above commands to /etc/rc.local

3. Start docker as a daemon in debug mode and use new hard drive for data and logging:

touch /mnt/docker_data/docker.log

chmod 666 /mnt/docker_data/docker.log

docker -D -d -g=/mnt/docker_data >> /mnt/docker_data/docker.log 2>&1 &

add the last command to /etc/rc.local so docker will start as a daemon on reboot

4. View available images:

sudo docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

You should see no images for a new installation, which is correct

5. Create a new image for testing:

sudo docker pull busybox

511136ea3c5a: Pull complete
df7546f9f060: Pull complete
ea13149945cb: Pull complete
4986bf8c1536: Pull complete
Status: Downloaded newer image for busybox:latest

6. Verify image is available:

sudo docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
busybox latest 4986bf8c1536 11 days ago 2.433 MB

sudo docker run busybox uname -a
Linux b7865677b5c0 3.10.5-3.el6.x86_64 #1 SMP Tue Aug 20 14:10:49 UTC 2013 x86_64 GNU/Linux


7. Verify container can connect to net:

sudo docker run busybox ping www.docker.com

PING www.docker.com (162.242.195.82): 56 data bytes
64 bytes from 162.242.195.82: seq=0 ttl=47 time=22.952 ms
64 bytes from 162.242.195.82: seq=1 ttl=47 time=29.074 ms
64 bytes from 162.242.195.82: seq=2 ttl=47 time=36.348 ms
^C
--- www.docker.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 22.952/29.458/36.348 ms

8. Start busybox with shared dir from host:

sudo docker run -i -t -v /mnt/for_docker/:/opt/for_docker busybox /bin/bash

ls -l /opt/for_docker

Any files you place in the host shared folder for_docker should now be available to the docker container. If you experience an error here verify that the shared folder is available on the CentOS VM with:

df -H

9. Create an alias for your docker container:

add the following to ~/.bashrc

alias busybox="sudo docker run -i -t -v /mnt/for_docker/:/opt/for_docker busybox /bin/bash"

Now you can access your docker busybox image with just busybox the next time you log in.

I suggest always creating a Dockerfile when creating docker images. Be aware the "history" command does not save all commands ran on the original image when exporting and importing images to another machine.

Have fun!


More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working