Stranice

Friday 28 August 2015

Working with directories in Docker using Windows 7 x64

This example was performed by using Docker Windows CLI 1.8.1

I will use linux shell on Windows.
The easiest way to run a shell is by installing Git with Unix tools.

Mounting directories from Windows host to Docker

 We need to put our directories into c:\Users\ directory. 
 docs: https://docs.docker.com/userguide/dockervolumes/ 
(Mount a host directory as a data volume)

There is a slight change when working in shell that worked in my case is that extra / need to be put.
So the following command mounts a directory from a host 

docker run -v //c/Users/directory:/directory image-name command

note: the volume is mounted temporary, you need to copy is into other directory,
or you can use docker cp command to copy data from host to container

Running docker with working working directory

We have the same problem as when  mounting directory.
That means we need to add extra / to run a directory from the container.
If don't do it a directory from host machine will be demanded.

This is example for first level directory in docker image

docker run -d -w //directory docker-image command


For initial startup with docker toolbox in Windows 7
Docker-machine on Windows 7 x64 




Thursday 27 August 2015

docker-machine on Windows 7 x64

I suppose you would like to use Docker toolbox on Windows 7 x64.
Boot2Docker is deprecated and new way of using Docker on Windows is docker-machine command.

If you are following official documentation they use one very cool command that sets environment variables in your shell automatically eval "$(docker-machine env machine-name)".
And then you get an error:
'eval' is not recognized as an internal or external command,
operable program or batch file.

Well, you think that there must be a command that is not on the path.
Unfortunately this is not also the case.

Uh, another linux on the Windows system.

Solution is quite simple.
1. Install Git for windows (include Unix tools) and run command prompt
    Be sure to enable PATH for git commands  
2. run sh -li (interactive shell)
3. your command line will change into linux shell
4. run above mentioned command:
    eval "$(docker-machine env machine-name)"

I have found out that usually there are usually some missing steps in the tutorials.

Well, well, something is missing here.
It seems that machine name is missing..

Ok, here is complete example from creating a new machine and connecting to it.

1. c:\Users\Name\sh -li (runs shell)
2. Name@Computer-Name ~ docker-machine create --driver virtualbox dev
3. Name@Computer-Name ~ eval "$(docker-machine env dev)"

Now your console is ready to use docker command on dev machine

Thursday 12 March 2015

Generating ranges using functions

In this blog post  Generating ranges with PostgreSQL I introduced generating ranges. As introductory post it was sufficient but what is bad about it that it has hard coded values i.e. it is not flexible. If we want to have reservation system we need to have flexible creation of intervals.

It can be done using PostgreSQL functions using plain SQL, only simple stuff for this example. When you want to get more result from functions you can have two constructs. You can return a set of values or a table. There is a small difference in syntax, usage is almost the same.

The code for the function with returning set is following:

create function generate_ts(beginning timestamp, finish timestamp, gap interval)
                                   returns TABLE(reservation tsrange) as
$$
with gen(ts) as
(
     select generate_series(beginning, finish, gap)
),
ss(start, stop) as (
    select ts, lead(ts, 1) over () from gen
)
select tsrange(start,stop)
from ss
where stop > start;
$$ LANGUAGE SQL;

We call this function with following statement:

select * from generate_ts('2014-09-17 09:00:00'::timestamp,
                                          '2014-09-17 16:00:00'::timestamp,
                                          '1 hour'::interval);

And we will have following result:

                  reservation
-----------------------------------------------
 ["2014-09-17 09:00:00","2014-09-17 10:00:00")
 ["2014-09-17 10:00:00","2014-09-17 11:00:00")
 ["2014-09-17 11:00:00","2014-09-17 12:00:00")
 ["2014-09-17 12:00:00","2014-09-17 13:00:00")
 ["2014-09-17 13:00:00","2014-09-17 14:00:00")
 ["2014-09-17 14:00:00","2014-09-17 15:00:00")
 ["2014-09-17 15:00:00","2014-09-17 16:00:00")
(7 rows)

So now we have customized time range interval. And if we want we can automatically insert into a table that we will help us create reservation system.

Example for inserting these values into system is this:

select * into reservation_system
from (select reservation
          from generate_ts('2014-09-17 09:00:00'::timestamp,
                                       '2014-09-18 16:00:00'::timestamp,
                                       '1 hour'::interval)) s;

This query will automatically create table from generate table.
So if we run query SELECT * FROM reservation_system we will have the same output as in our first call for the function generate_ts.

This is step further into creating reservation system using modern PostgreSQL.




Sunday 25 January 2015

Using Python libraries in Windows


Using Python in Windows in an adventure.

I decided to record all the steps you need to run Python normally in Windows 7 x64 system. Most of the things will be (I hope) applicable to other variants of Windows system.
Using basic Python for programming is not a problem, but when you want to install some tools and packages that it's getting pretty messy. But there is a light on the end of the tunnel (I hope).

I hope you wonder why do I grunt so much. Well, I mostly experience with Java where by using Maven and Gradle I had little problem adding extra components.

So here is required steps for using extra packages in Python.

I will describe installing libraries for up to 3.4 and 3.4. The reason is that in Python 3.4 setuptools are automatically installed with the installation. By setuptools I mean pip and easy_install programs.

When you installing Python also choose to Pyton on path, so you can easily run python executable from the command line.

This step is only required for the versions up to 3.4:

Here is the official documentation for installing setup tools:
https://pypi.python.org/pypi/setuptools

After you install the setup tools you will be able to run pip and easy_install programs. A crucial program to install with pip is wheel and another very important is virtualenv. With virtualenv we can run so called virtual environment (VE). So you can have multiple versions of various python packages on the same system.

You can read more about wheel on the official documentation page:
https://wheel.readthedocs.org/en/latest/

I will install virtualenv and wheel for the start with these commands:
  • pip install virtualenv
  • pip install wheel
Now that we have these programs installed I will create a VE
  1. c:\mkdir virtualenvs
  2. c:\cd virtualenvs
  3. c:\virtualenvs\mkdir test
  4. c:\virtaulenvs\test\virtualenv --clear venvI used option --clear so that I have clean virtualenv
    (without previously installed packages)
With this four steps we created a directory where will we store all virtualenvs and a directory for our test project. In the and we created a new environment.

With following command we will activate VE.
  • c:\virtualenvs\test\  venv\scripts\activate
  • (venv) c:\virtualenvs\test
With activate virtualenv we can install packages. If you have any package that you want to have in all VEs you should install it without VE enabled and when you you are creating VE omit --clear parameter.

Now we have three variations of installing packages. 
  1. Simple pip install
  2. Installing whl file with pip
  3. installing exe file with easy install
ad 1) To install programs just type pip install as we did when installing virtualenv and wheel

One of good place to get windows precompiled library is this web page:
http://www.lfd.uci.edu/~gohlke/pythonlibs/

ad 2) When installing whl file you need to use some extra options
         (venv) c:\virtualenvs\test\ pip install --use-wheel filename.whl
         And the file will be install

ad 3) If we have some installation file only as exe e.g. psychopg then we need to use
       easy_install  command
        (venv) c:\virtualenvs\test\ easy_install psychopg-version.exe

If make a command pip list we will have a list of installed packages in our VE. And when we choose interpreter in our IDE we choose a python.exe in the following directory:
    c:\virtualenvs\test\venv\scripts\python.exe

When we want to finish our VE session we need to run deactivate command:
     c:\virtualenvs\test\  venv\scripts\deactivate

So every time we want to install some packages in our VE we need to activate first installed programs and deactivate them at the end.