Murky Swamp coding life!

Run psql without password

To run psql without prompting password, you need to create $HOME/.pgpass file like below with correct file permission 600:

# host:port:database:username:password

Update it with corrent values and there you go!

Increase your disk size for boot2docker

You searched for this article, so I assume you know what boot2docker is and are using it. That’s good, so am I.

But how many times have you got errors like no space left when you tried to pull a new image and running a command with container? I’ll tell you what, there’s a way to increase your disk size.

Find ~/.boot2docker/profile or create one if you cannot find it. For Windows, try %USERPROFILE%/.boot2docker/profile. There’s a configuration called DiskSize, change it to any size you want.

Make the whole configuraion file look like:

# boot2docker profile filename: /Users/lxie/.boot2docker/profile
Init = false
Verbose = false
Driver = "virtualbox"
SSH = "ssh"
SSHGen = "ssh-keygen"
SSHKey = "/Users/lxie/.ssh/id_boot2docker"
VM = "boot2docker-vm"
Dir = "/Users/lxie/.boot2docker"
ISO = "/Users/lxie/.boot2docker/boot2docker.iso"
DiskSize = 32000
Memory = 2048
SSHPort = 2022
DockerPort = 0
HostIP = ""
NetMask = [255, 255, 255, 0]
LowerIP = ""
UpperIP = ""
DHCPEnabled = true
Serial = false
SerialFile = "/Users/lxie/.boot2docker/boot2docker-vm.sock"
Waittime = 300
Retries = 75

Destroy your existing boot2docker-vm by running boot2docker destroy, and then use boot2docker init to create a new one.

Enjoy your new environment!

Kill containers in Docker

After using Docker for a while, you may find out there are so many containers in stopped status, and they are not deleted. You can delete them one by one with docker rm, but we are developers, how can we bear this?

Thank god Docker provides a parameter named -q in docker ps command to just print container IDs on console.

  • Delete all stopped containers

    docker rm $(docker ps -f status=stopped -q)

  • Delete all containers

    docker rm -f $(docker ps -a -q)

Isn’t it cool? If not, make some alias for this, like alias drma=docker rm -f $(docker ps -a -q), that’s how it’s done!

Return text with Nginx

There is always a time that you want your web server just return some plain text to user or monitor services. One way of doing it is to create a static file and use url of this file.

Is there a better way for this?

Nginx allows user use ‘return’ to return a HTTP code and some text, which is perfect for us.

Here’s a simple example:

1 server {
2     listen 8080;
3     location /index.html {
4         return 200 "ok";
5     }
6 }

As we can see, return accepts 2 arguments, first one is HTTP Code, and last one is text you want to show. For HTTP Code, you can use 200, 204, 400, 402-406, 408, 410, 411, 413, 416 , 500-504

How to configuration Nginx with and uwsgi

Before you start, you should know how to install nginx and uwsgi, and have them already installed.

My ENV is using nginx 1.6.2, uwsgi 2.0.7 and Python 3.4.2

Add for your application

A simple application can be like below:

 1 from bottle import run, default_app
 3 @route('/')
 4 def home():
 5     return "I am running under nginx and uwsgi!"
 7 if __name__ == "__main__":
 8     run(host='', port=8080)
 9 else:
10     application = default_app()

The key point of this piece of code is that have an application object once it’s imported. uwsgi will import load this file instead of running it.

uwsgi parameters

uwsgi can be ran like below:

uwsgi -s /var/run/uwsgi/app.sock -d /var/log/app/uwsgi.log -M --chdir /var/www/app

-s means you want to use a socket for uwsgi, -d means it runs in daemon mode, and --chdir tells uwsgi where is root of your application.

Nginx configuration

 1 server {
 2     listen 80;
 3     server_name;
 4     access_log /var/log/app/access.log;
 5     error_log /var/log/app/error.log;
 6     root /var/www/app;
 8     location / {
 9         include uwsgi_params;
10         uwsgi_pass unix:/var/run/uwsgi/app.sock;
11         uwsgi_param UWSGI_CHIDIR /var/www/app;
12         uwsgi_param UWSGI_SCRIPT app; # this is with suffix
13     }
14 }

Once you start uwsgi and nginx, you will see your server running by visiting!