Convert Any Server to a Docker Container

DEPRECATED PAGE:

UPDATED HERE

 

OLD POST:


 

This post is based on material from Docker in Practice, available on Manning’s Early Access Program. Get 39% off with the code: 39miell

dip

How and Why?

Let’s say you have a server that has been lovingly hand-crafted that you want to containerize.

Figuring out exactly what software is required on there and what config files need adjustment would be quite a task, but fortunately blueprint exists as a solution to that.

What I’ve done here is automate that process down to a few simple steps. Here’s how it works:

Blueprint_Server

You kick off a ShutIt script (as root) that automates the bash interactions required to get a blueprint copy of your server, then this in turn kicks off another ShutIt script which creates a Docker container that provisions the container with the right stuff, then commits it. Got it? Don’t worry, it’s automated and only a few lines of bash.

There are therefore 3 main steps to getting into your container:

– Install ShutIt on the server

– Run the ‘copyserver’ ShutIt script

– Run your copyserver Docker image as a container

Step 1

Install ShutIt as root:

sudo su -
(apt-get update && apt-get install -y python-pip git docker) || (yum update && yum install -y python-pip git docker which)
pip install shutit

The pre-requisites are python-pip, git and docker. The exact names of these in your package manager may vary slightly (eg docker-io or docker.io) depending on your distro.

You may need to make sure the docker server is running too, eg with ‘systemctl start docker’ or ‘service docker start’.

Step 2

Check out the copyserver script:

git clone https://github.com/ianmiell/shutit_copyserver.git

Step 3

Run the copy_server script:

cd shutit_copyserver/bin
./copy_server.sh

There are a couple of prompts – one to correct perms on a config file, and another to ask what docker base image you want to use. Make sure you use one as close to the original server as possible.

Note that this requires a version of docker that has the ‘docker exec’ option.

Step 4

Run the build server:

docker run -ti copyserver /bin/bash

You are now in a practical facsimile of your server within a docker container!

This is not the finished article, so if you need help dockerizing a server, let me know what the problem is, as improvements can still be made.

This post is based on material from Docker in Practice, available on Manning’s Early Access Program. Get 39% off with the code: 39miell

dip

Advertisements

13 thoughts on “Convert Any Server to a Docker Container”

  1. “How and Why? Let’s say you have a server that has been lovingly hand-crafted that you *want to containerize.*”

    Where is the “Why” -part? Why should I want to containerize my server? (apart from because I can)

    1. Same question here.

      “Containerize my server” is applying the same mentality that was seen when virtual machines came up, but now to containers. It’s extremely inefficient and doesn’t buy you anything. This huge container will sit there unmanaged and it’s now much more of a black box than it was before. IMHO, this is not how containers are meant to be used. They should use minimal images, have only the necessary bits and enable quick builds and deployment of new code. If you are treating a container as a VM, you’re better off staying with the VM.

      1. I agree on most points; the implication of what you are saying is that the hard part to change is the behaviour – going from mutable servers to immutable rebuilt configurations.

        However, I think that containerizing (and the benefits of a more lightweight disposable environment) can facilitate the move in this area (just as VMs did to a large extent. I’ve seen this happen first hand.

  2. It would also be an option to just start a Docker Container with Python and mount the application directory as a data volume inside your container.

    If you need persistent data you can copy the data into a Docker Volume directly. For example “docker run –volumes-from GHOSTDATA -v /srv/http/ghost:/migrate busybox cp /migrate/config.js /ghost-override” This copies local data directly into a busybox container using the docker run command.

  3. For a FIPS enabled VM, pip install shutit doesn’t work

    root@xxxx:~# pip install shutit
    You are using pip version 7.1.0, however version 8.1.2 is available.
    You should consider upgrading via the ‘pip install –upgrade pip’ command.
    Requirement already satisfied (use –upgrade to upgrade): shutit in /usr/lib/python2.6/site-packages/shutit-0.9.191-py2.6.egg
    Collecting pexpect>=4.0 (from shutit)
    /usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
    InsecurePlatformWarning
    Unsupported hash name md5 for package https://pypi.python.org/packages/b2/c1/0d2f841943604867ff237e77fff3b0d9b81cbcc80f7cf24d6b3f0cf3b378/pexpect-4.1.0-py2.py3-none-any.whl#md5=c650495cfa9c157b78e57955857b0967 (from https://pypi.python.org/simple/pexpect/)
    Downloading pexpect-4.1.0-py2.py3-none-any.whl (54kB)
    100% |████████████████████████████████| 57kB 3.3MB/s
    Exception:
    Traceback (most recent call last):
    File “/usr/lib/python2.6/site-packages/pip/basecommand.py”, line 223, in main
    status = self.run(options, args)
    File “/usr/lib/python2.6/site-packages/pip/commands/install.py”, line 290, in run
    requirement_set.prepare_files(finder)
    File “/usr/lib/python2.6/site-packages/pip/req/req_set.py”, line 334, in prepare_files
    functools.partial(self._prepare_file, finder))
    File “/usr/lib/python2.6/site-packages/pip/req/req_set.py”, line 321, in _walk_req_to_install
    more_reqs = handler(req_to_install)
    File “/usr/lib/python2.6/site-packages/pip/req/req_set.py”, line 491, in _prepare_file
    session=self.session)
    File “/usr/lib/python2.6/site-packages/pip/download.py”, line 825, in unpack_url
    session,
    File “/usr/lib/python2.6/site-packages/pip/download.py”, line 673, in unpack_http_url
    from_path, content_type = _download_http_url(link, session, temp_dir)
    File “/usr/lib/python2.6/site-packages/pip/download.py”, line 886, in _download_http_url
    _download_url(resp, link, content_file)
    File “/usr/lib/python2.6/site-packages/pip/download.py”, line 626, in _download_url
    _check_hash(download_hash, link)
    File “/usr/lib/python2.6/site-packages/pip/download.py”, line 489, in _check_hash
    if download_hash.digest_size != hashlib.new(link.hash_name).digest_size:
    AttributeError: ‘NoneType’ object has no attribute ‘digest_size’

  4. Hi zwischenzugs,

    I am trying to run ./copy_server.sh. But getting the below error. I am running it in an amazonlinux system (yum). Is there an issue due to this OS? Please help.

    ShutIt version: 1.0.27
    Linux ip-10-0-2-39 3.14.42-31.38.amzn1.x86_64 #1 SMP Wed May 13 20:33:05 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

    2017-10-04 11:50:18,809 INFO: Installing package: python-pip
    Traceback (most recent call last):
    File “/usr/local/bin/shutit”, line 11, in
    sys.exit(main())
    File “/usr/local/lib/python2.7/site-packages/shutit.py”, line 67, in main
    shutit.setup_shutit_obj()
    File “/usr/local/lib/python2.7/site-packages/shutit_class.py”, line 3942, in setup_shutit_obj
    self.do_build()
    File “/usr/local/lib/python2.7/site-packages/shutit_class.py”, line 4181, in do_build
    self.build_module(module)
    File “/usr/local/lib/python2.7/site-packages/shutit_class.py”, line 4123, in build_module
    if not module.build(self):
    File “/usr/local/lib/python2.7/site-packages/shutit_module.py”, line 59, in wrapper
    ret = func(self, shutit)
    File “/opt/shutit_copyserver/shutit_copyserver.py”, line 66, in build
    shutit.install(‘python-pip’)
    File “/usr/local/lib/python2.7/site-packages/shutit_class.py”, line 1798, in install
    loglevel=loglevel)
    File “/usr/local/lib/python2.7/site-packages/shutit_pexpect.py”, line 1393, in install
    package = package_map.map_packages(package, self.current_environment.install_type)
    File “/usr/local/lib/python2.7/site-packages/package_map.py”, line 108, in map_packages
    res = res + ‘ ‘ + map_package(package,install_type)
    TypeError: cannot concatenate ‘str’ and ‘function’ objects

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