Saturday, 21 June 2014

Docker and CoreOS

Docker is a container-based software framework for automating deployment of applications. “Containers” are encapsulated, lightweight, and portable application modules. The major (intended) benefit of using a container is that your application will run consistently on and between any server, be it cloud or dedicated, or of varying operating systems.

A Docker container does not include a separate operating system, instead, it relies on the kernel's functionality provided by the underlying cgroups that provide resource isolation (CPU, memory, block I/O, network, etc.) and separate namespaces to completely isolate the application's view of the operating environment.

http://www.docker.com/
http://training.docker.com/self-paced-training
http://www.docker.com/tryit/
http://xmodulo.com/2014/05/docker-containers-centos-fedora.html
https://github.com/wsargent/docker-cheat-sheet
https://github.com/dimonomid/docker-quick-ref/releases/download/latest/docker-quick-ref.pdf


# Quick install for Centos 6:
rpm -iUvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum -y install docker-io
service docker start
chkconfig docker on

# Download a Docker Container:
docker pull centos
# Run a Docker Container:
docker run -i -t centos /bin/bash



                        ##        .
                  ## ## ##       ==
               ## ## ## ##      ===
           /""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
           \______ o          __/
             \    \        __/
              \____\______/
              |          |
           __ |  __   __ | _  __   _
          /  \| /  \ /   |/  / _\ |
          \__/| \__/ \__ |\_ \__  |



The next question was: if the full-stack can be provided by a Docker containers and everything can be Dockerized, what is the minimum OS we need to run Docker images?

CoreOS seems to answer this question. CoreOS is an open source lightweight operating system based on the Linux kernel and designed for providing infrastructure to clustered deployments, while focusing on automation, ease of applications deployment, security, reliability and scalability. As an operating system, CoreOS provides only the minimal functionality required for deploying applications inside software containers, together with built-in mechanisms for service discovery and configuration sharing.

CoreOS was originally based on ChromeOS but has since been forked. With CoreOS, automatic updates happen in the background. CoreOS has two disk partitions, with only one active at a time. The inactive partition can be updated offline. You simply swap the disks and reboot to enable the updates. This reboot only takes between half a second to a second.

CoreOS provides etcd, a daemon which runs across all computers in a cluster, allowing configuration data to be easily shared by providing a dynamic configuration registry. Since the key–value data stored within etcd is automatically distributed and replicated (with automated master election), all changes are reflected across the entire cluster.

CoreOS also provides fleet, a cluster manager daemon that controls CoreOS' separate systemd instances at the cluster level. By using fleet, CoreOS creates a distributed init system that ties together separate systemd instances and a cluster-wide etcd deployment; internally, fleet daemon communicates with systemd over D-Bus. Using fleet allows single or multiple containers to be deployed cluster-wide, with more advanced options including redundancy, failover, deployment to specific cluster members, dependencies between containers, and grouped deployment of containers. 

https://coreos.com/
http://coreos.com/docs/launching-containers/building/getting-started-with-docker/
http://en.wikipedia.org/wiki/CoreOS

Cheat sheets:
http://wordpress-tomason.rhcloud.com/wp-content/uploads/2014/11/hand-over-sheets.pdf
http://deeeet.com/writing/2013/12/08/docker-cheat-with-exmaple/
Docker Cheat Sheet is a nice documentation. It provides us Docker basic commands and system and It's easy to understand. But there are less examples, I reconstructed it with real examples. You should refer above document about installation.

Set up

Pull a base image.
docker pull ubuntu
It's annoy to restore Container ID, you may forget to restore. You can set below alias. With this, you can get the ID of the last-run Container (15 Docker tips in 5 minutes)
alias dl='docker ps -l -q'

Container

To create a Container.
docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
To stop a Container.
docker stop `dl`
To start a Container.
docker start `dl`
To restart a Container.
docker restart `dl`
To Connect to a running Container.
docker attach `dl`
To copy file in a Container to the host.
docker cp `dl`:/etc/passwd .
To mount the directory in host to a Container.
docker run -v /home/vagrant/test:/root/test ubuntu echo yo
To delete a Container.
dockr rm `dl`

Info of Container

To show running Containers. With -a option, it shows running and stopped Containers.
docker ps
To show Container information like IP adaddress
docker inspect `dl`
To show log of a Container.
docker logs `dl`
To show running process in a Container.
docker top `dl`

Image

To create a image from a Container. For tag name, <username>/<imagename> is recommended.
docker run -d ubuntu /bin/sh -c "apt-get install -y hello"
docker commit -m "My first container" `dl` tcnksm/hello
To create a image with Dockerfile.
echo -e "FROM base\nRUN apt-get install hello\nCMD hello" > Dockerfile
docker build tcnksm/hello .
To login to a image.
docker run -rm -t -i tcnksm/hello /bin/bash
To push a imges to remote repository. You need to sign up to Docker index in advance. Exmple uploaded image.
docker login
docker push tcnksm/hello
To delete a image
docker rmi tcnkms/hello

Info of Image

To show all images
docker images
To show image information like IP adress.
docker inspect tcnksm/hello
To show command history of a image.
docker history tcnksm


http://nginx.com/blog/deploying-nginx-nginx-plus-docker/
# docker pull nginx
Pulling repository nginx
319d2015d149: Download complete
64e5325c0d9d: Download complete
bf84c1d84a8f: Download complete
aaa04ccb1cff: Download complete
c25a494fd450: Download complete
2b4392756878: Download complete
a58c6ba7d446: Download complete
6391a6648407: Download complete
0b6365661275: Download complete
5c1f0ec6cd7b: Download complete
36f0b0306c21: Download complete
107c338c1d31: Download complete
Status: Downloaded newer image for nginx:latest
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
zorang/hello        latest              0301693339c0        14 hours ago        274.1 MB
nginx               1                   319d2015d149        28 hours ago        132.8 MB
nginx               1.9                 319d2015d149        28 hours ago        132.8 MB
nginx               latest              319d2015d149        28 hours ago        132.8 MB
nginx               1.9.2               319d2015d149        28 hours ago        132.8 MB
centos              7                   fd44297e2ddb        8 weeks ago         215.7 MB
centos              centos7             fd44297e2ddb        8 weeks ago         215.7 MB
centos              latest              fd44297e2ddb        8 weeks ago         215.7 MB
# docker run --name mynginx1 -P -d nginx
272e72bdecdab514f7a1d5dd71fa1bb2e43c56b6baf0e0af733f994d4c70e90f
# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
272e72bdecda        nginx:1             "nginx -g 'daemon of   19 seconds ago      Up 16 seconds       0.0.0.0:49153->443/tcp, 0.0.0.0:49154->80/tcp   mynginx1
[root@centos6 ~]# curl http://localhost:49154
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
   body {
       width: 35em;
       margin: 0 auto;
       font-family: Tahoma, Verdana, Arial, sans-serif;
   }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>


<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>


<p><em>Thank you for using nginx.</em></p>
</body>
</html>


# docker run --name mynginx2 -v /var/www/html/zycom/www.zycom.com:/usr/share/nginx/html:ro  -v /etc/nginx:/etc/nginx:ro -P -d nginx
aa6da9628a16ddc2031a5b77f8d1ddbf1aa0bf9eb021289cdd0ab790e9d574f1


# docker ps -al
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
aa6da9628a16        nginx:1             "nginx -g 'daemon of   16 seconds ago      Up 12 seconds       0.0.0.0:49155->443/tcp, 0.0.0.0:49156->80/tcp   mynginx2
[root@centos6 ~]# curl http://localhost:49156
<!DOCTYPE html>
<removed>


# cat Dockerfile
FROM centos
MAINTAINER Zoran Gagic zorang@gmail.com


RUN yum install -y --nogpgcheck epel-release wget openssh-clients openssh-server
RUN yum install -y --nogpgcheck nginx
RUN yum clean all
RUN mkdir /var/run/sshd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
RUN echo root:topsecret123 | chpasswd
RUN useradd --create-home -G wheel -s/bin/bash zorang && \\
   echo -n 'zorang:topsecret123' | chpasswd
RUN echo 'export PS1="[\u@docker] \W # "' >> /root/.bash_profile


ADD motd /tmp/motd


VOLUME ["/data", "/html"]


EXPOSE 80 443


CMD ["/usr/sbin/nginx", "-g", "daemon off;"]


# docker build -t mynginx .
# docker run -d -P mynginx
# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
4edef0573702        mynginx:latest      "/usr/sbin/nginx -g    9 seconds ago       Up 7 seconds        0.0.0.0:32775->80/tcp, 0.0.0.0:32774->443/tcp   sad_swartz


Update 9/9/2015 - Good Docker overview:
http://www.flashmemorysummit.com/English/Collaterals/Proceedings/2015/20150810_PreconfJ_Bradley.pdf

Update 19/3/2016 - AWS ECS overview:
http://www.infoq.com/articles/intro-aws-ecs
http://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-basics.html
  1. Update the installed packages and package cache on your instance.
    [ec2-user ~]$ sudo yum update -y
  2. Install Docker. Amazon ECS requires a minimum Docker version of 1.5.0 (version 1.9.1 is recommended), and the default Docker versions in many system package managers, such as yum or apt-get do not meet this minimum requirement. For information about installing the latest Docker version on your particular Linux distribution, go to https://docs.docker.com/installation/.
    [ec2-user ~]$ sudo yum install -y docker git
  3. Start the Docker service.
    [ec2-user ~]$ sudo service docker start
    Starting cgconfig service:                                 [  OK  ]
    Starting docker:                                    [  OK  ]
  4. Add the ec2-user to the docker group so you can execute Docker commands without using sudo.
    [ec2-user ~]$ sudo usermod -a -G docker ec2-user
  5. Log out and log back in again to pick up the new docker group permissions.
  6. Verify that the ec2-user can run Docker commands without sudo.
    [ec2-user ~]$ docker info
    Containers: 2
    Images: 24
    Storage Driver: devicemapper
     Pool Name: docker-202:1-263460-pool
     Pool Blocksize: 65.54 kB
     Data file: /var/lib/docker/devicemapper/devicemapper/data
     Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
     Data Space Used: 702.3 MB
     Data Space Total: 107.4 GB
     Metadata Space Used: 1.864 MB
     Metadata Space Total: 2.147 GB
     Library Version: 1.02.89-RHEL6 (2014-09-01)
    Execution Driver: native-0.2
    Kernel Version: 3.14.27-25.47.amzn1.x86_64
    Operating System: Amazon Linux AMI 2014.09

No comments:

Post a Comment