Saturday, June 20, 2015

Reseting git changes

To reset a commit

git reset --soft HEAD^

To reset an add command before commit

git reset

Sunday, June 14, 2015

AWS region and endpoint map

http://docs.aws.amazon.com/general/latest/gr/rande.html

Docker container cannot access mounted volumes in OSX host

If you are running Nginx or Apache in your docker images while using those to write files (ex. cache) on the host machine, chances are you will get a permission error.

Bash into your container:

> docker-compose run bash

cd into the location that has your mounted volume and do a "ls -l", you may see the following:

drwxr-xr-x 1 1000 staff 646 Jun 15 03:39 app

In Nginx or Apache, the user is usually www-data. We need to associate www-data with the UID 1000.

In your Dockerfile, add the following:

> RUN usermod -u 1000 www-data

Now if you check the permission again, you would see the correct user.

drwxr-xr-x 1 www-data staff 646 Jun 15 03:39 app

MAMP Nginx cannot connect to phpmyadmin #2002

It is a really weird error.

In file /MAMP/bin/phpMyAdmin/config.inc.php, search for the line:

$cfg['Servers'][$i]['host']          = 'localhost';

Change to:

$cfg['Servers'][$i]['host']          = '127.0.0.1';

Saturday, June 13, 2015

Create your own Docker Registry with S3

The purpose of this post is to be able to deploy your own custom image to ElasticBeanstalk using docker registry through storing the images on Amazon S3.

Let's begin by cloning Docker Registry 2.0.

git clone https://github.com/docker/distribution.git

Generate self-signed certificates.

cd distribution
mkdir certs
openssl req \
         -newkey rsa:2048 -nodes -keyout certs/domain.key \
         -x509 -days 365 -out certs/domain.crt

Add TLS to config.yml


vi ./cmd/registry/config.yml

Add the tls block to the http section like the following:

http:
    addr: :5000
    secret: asecretforlocaldevelopment
    debug:
        addr: localhost:5001
    tls: 
        certificate: /go/src/github.com/docker/distribution/certs/domain.crt
        key: /go/src/github.com/docker/distribution/certs/domain.key

Remove filesystem settings and use AWS s3 as repository storage:

storage:
   #filesystem:
   #        rootdirectory: /tmp/registry
   s3:
      accesskey: awsaccesskey
      secretkey: awssecretkey
      region: us-west-1
      bucket: bucketname
      encrypt: true
      secure: true
      v4auth: true
      chunksize: 5242880
      rootdirectory: /s3/object/name/prefix

Settings: http://docs.docker.com/registry/configuration/#storage

Save this.

Build the image with a name (ex. docker_registry)

> docker build -t docker_registry .

Tag it. Note that I am using boot2docker on MacOSX. You can get your IP address by running "boot2docker ip".

> docker tag docker_registry:latest 192.168.59.103:5000/docker_registry:latest

Run the registry.

> docker run -p 5000:5000 docker_registry

If you try to push your an image, you will get a error saying you need to add an insecure registry.

> boot2docker ssh "echo $'EXTRA_ARGS=\"--insecure-registry 192.168.59.103:5000\"' | sudo tee -a /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart"

Push an image:

> docker push 192.168.59.103:5000/{image}

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