Sunday, June 7, 2015

Install Docker Compose on MacOSX or Ubuntu

Install docker-compose

> curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

> chmod +x /usr/local/bin/docker-compose

Check version

> docker-compose version

Run service

> docker-compose up

Check all running services

> docker-compose ps

Bash access to a running service

> docker-compose run worker bash

Saturday, June 6, 2015

Using DockerFile to create an image

Each line in Dockerfile creates a layer. An image can have a max of 127 layers

Create a Dockerfile in a new folder.

> vi Dockerfile

Paste the following inside the Dockerfile

# this is a comment
FROM ubuntu:14.04
MAINTAINER Kenneth
RUN apt-get update && apt-get install
RUN gem install json

Build the image

> docker build -t kenneth/sinatra:v2 .

Tag the image

> docker tag ouruser/sinatra:dev

Wednesday, June 3, 2015

boot2docker in terminal MacOSX

Initialize boot2docker

> boot2docker init

Start boot2docker

> boot2docker start

Set the environment variables in the current terminal.

> eval "$(boot2docker shellinit)"

Test run

> docker run hello-world

To run an Nginx server (-d for running in background):

> docker run -d -P --name web nginx

Stop

> boot2docker stop

Check status

> boot2docker status

Built an image:

> docker build -t .

Access home folder:

> cd $HOME

Mount a local directory to the image's directory

echo "my new site" > index.html
> docker run -d -P -v $HOME/site:/usr/share/nginx/html --name mysite nginx

upgrade boot2docker

> boot2docker stop
> boot2docker upgrade

Useful docker commands

Check docker version:

> docker version

Search an image named tutorial:

> docker search tutorial

Download an image:

> docker pull learn/tutorial

Install ping on your image:

> docker run learn/tutorial apt-get install -y ping

Show running processes:

> docker ps -l

Grab the ID above and commit the change with a name. Docker will return a new ID for the new image. (Only the first three characters of the ID is enought)

> docker commit 698 learn/ping

See list of running container:

> docker ps

Grab the container ID above and you can inspect the information of the container by running

> docker inspect

Inspect one element of the container specs

> docker inspect -f '{{ .NetworkSettings.IPAddress }}' nostalgic_morse

Push to a docker repository:

> docker push learn/ping

Build an image from a DockerFile:

docker build -t   .

Remove all containers:

> docker stop $(docker ps -qa)

Run an app with interactive mode

> docker run -it --rm -p 3000:8080

See mapped ports.

> docker port

If you are using boot2docker, check the ip by:

> boot2docker ip

You should get something like 192.168.59.103

Stop the container:

> docker stop

Remove the container:

> docker rm

Shows standard output of a container:

> docker logs

See the end of the standard output of a container (if you are running a web app, you can see the outputs):

> docker logs -f

See applications running insider the container

> docker top

list all local images:

> docker images

list all containers, including exited

> docker ps -a

Remove all exited containers:

> docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm 

Commit a change to an image with id 0b2616b0e5a8

> docker commit -m "Added json gem" -a "Kate Smith" \ 0b2616b0e5a8 ouruser/sinatra:v2

Use bash

> docker run -t -i training/sinatra /bin/bash

Port mapping from host to container.

> docker run -d -p 5000:5000 training/webapp python app.py

Port mapping from only localhost port to container.

> docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py

Port mapping from localhost dynamic port to container.

> docker run -d -p 127.0.0.1::5000 training/webapp python app.py

Port mapping to UDP.

> docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

Check where on the host the container is mapped to.

> docker port 5000

Change name to web and run the web app.

> docker run -d -P --name web training/webapp python app.py

Inspect name of the container.

> docker inspect -f "{{ .Name }}"

Remove a running container

> docker rm -f

Create a web container and link to a db container (--link :alias)

> docker run -d --name db training/postgres
> docker run -d -P --name web --link db:db training/webapp python app.py

Inspect the link information:

> docker inspect -f "{{ .HostConfig.Links }}" web

Output: [/db:/web/db]

When containers are linked, docker automatically creates environment variables and a /etc/host file

> sudo docker run --rm --name web2 --link db:db training/webapp env

Output:

DB_NAME=/web2/db
DB_PORT=tcp://172.17.0.5:5432
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
DB_PORT_5432_TCP_PROTO=tcp
DB_PORT_5432_TCP_PORT=5432
DB_PORT_5432_TCP_ADDR=172.17.0.5

It is recommended to use /etc/host file to set locations.

> docker run -t -i --rm --link db:webdb training/webapp /bin/bash
> cat /etc/hosts

Output:

172.17.0.7  aed84ee21bde
. . .
172.17.0.5  webdb 6e5cdeb2d300 db

Ping the address:

> apt-get install -yqq inetutils-ping
> ping webdb

Restart db: (Note that the host /etc/hosts will auto update itself)

> docker restart db

Adding a data volume with -v:

> docker run -d -P --name web -v /webapp training/webapp python app.py

Note that docker volumes are persistent. Even if the container is removed, it will still be there.

Mount host directory to container's with read-write permissions

> docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

Mount host directory to container's with read-only permission.

> docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py

Mount a single file

> docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash

Example run:


> docker run -it -d --name flask -p 3000:8080 flask/image:latest

Elastic Beanstalk with Git

In your git project directory, run

eb init

It will ask for your security access keys, you can get it here:

https://console.aws.amazon.com/iam/home?#security_credential

When asking for solution stack, use the following (if you are using docker)

50) 64bit Amazon Linux 2015.03 v1.4.1 running Docker 1.6.0

After it's done, it will show you the location with your auth info:

/Users/{username}/.elasticbeanstalk/aws_credential_file

Deploy the application by:

eb start

If you see the following boto error, install it:

ImportError: No module named boto

Instruction: https://github.com/boto/boto

Saturday, May 30, 2015

Set up MAMP with Nginx, php-fpm and symfony on MacOSX

To make this work, you will need to have MAMP installed.

The steps are as follows: install php-fpm, add your symfony config to nginx.conf, start the server.

Install php5-fpm

You can use macport or brew to install php5-fpm

After it's install, search php5-fpm by doing "which-fpm". Note that it may be called "php-fpm" in some installations.

php5-fpm requires two configurations to be set up: /etc/php-fpm.conf and /etc/php-fpm/pool.d/{whatever}.conf

Sample php-fpm.conf:

https://github.com/perusio/php-fpm-example-config/blob/tcp/fpm/php5-fpm.conf

Sample pool.d/{whatever}.conf

http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/

Start php5-fpm by

> sudo php5-fpm

In your MAMP config - /conf/nginx/nginx.conf, put in the relevant http and https server settings:

server {
listen 80;
   server_name localhost_moonlight;
   root /Users/rei999/Documents/symfony_workspace/moonlight/web;

   location / {
       try_files $uri /app_dev.php$is_args$args;
   }
   # DEV
   # This rule should only be placed on your development environment
   # In production, don't include this and don't deploy app_dev.php or config.php
   location ~ ^/(app_dev|config)\.php(/|$) {
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_split_path_info ^(.+\.php)(/.*)$;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param HTTPS off;
       }

   error_log /Applications/MAMP/logs/nginx/moonlight_error.log;
   access_log /Applications/MAMP/logs/nginx/moonlight_access.log;
}

server {
listen 443 ssl;
   server_name localhost_moonlight;

   ssl_certificate      /Applications/MAMP/conf/ssl/server.crt;
        ssl_certificate_key  /Applications/MAMP/conf/ssl/server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

   root /Users/rei999/Documents/symfony_workspace/moonlight/web;

   location / {
       try_files $uri /app_dev.php$is_args$args;
   }
   # DEV
   # This rule should only be placed on your development environment
   # In production, don't include this and don't deploy app_dev.php or config.php
   location ~ ^/(app_dev|config)\.php(/|$) {
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_split_path_info ^(.+\.php)(/.*)$;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param HTTPS off;
       }

   error_log /Applications/MAMP/logs/nginx/moonlight_error.log;
   access_log /Applications/MAMP/logs/nginx/moonlight_access.log;
}



Depending on your PHP-FPM config, the fastcgi_pass can also be fastcgi_pass 127.0.0.1:9000.

Start MAMP Nginx.

Wednesday, May 13, 2015

Handy RabbitMQ commands

Running rabbitmq in background:

rabbitmq-server -detached

Checking status:

rabbitmqctl status

Stopping:

rabbitmqctl stop

Show queues:

rabbitmqctl list_queues

Show queues with message status

rabbitmqctl list_queues name messages_ready messages_unacknowledged