Automatically build after file change

I’m playing a lot with Docker lately. Building images, and then rebuilding, and then building again… It’s pretty boring. To automate this task a little I used inotify to build automatically after I changed any file. This trick could be used in many different situations.

You will need inotify-tools package:

    sudo apt-get install -y inotify-tools

Then run something like this:

while inotifywait -e modify -r .; do docker-compose build; done

This commands will rebuild my Docker images after any file change in current directory. Use Ctrl+c to exit from loop.

Install Docker Compose

When I started playing with Docker I was running a lot of commands to build image, delete containers running on old image, run containers based on new image, etc… A lot of log commands with links, volumes, etc…
Then I started searching for something to automate this task and here I get to docker-compse command, this is how you may install it:

pip install docker-compose

And install additional bash completions (run as root):

curl -L https://raw.githubusercontent.com/docker/compose/$(docker-compose version --short)/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

Then you may prepare docker-compose.yml file like:

web:
  build: .
  command: php -S 0.0.0.0:8000 -t /code
  ports:
    - "8000:8000"
  links:
    - db
  volumes:
    - .:/code
db:
  image: orchardup/mysql
  environment:
    MYSQL_DATABASE: wordpress

More informations about syntax may be found here: https://docs.docker.com/compose/compose-file/

And run such environment with:

docker-compose up

Or to run this in background:

docker-compose up -d

To stop and cleanup it use:

docker-compose stop && docker-compose rm -f -v

Other usable commands are:

docker-compose build --force-rm # to rebuild images and clean after
docker-compose ps               # to list containers

I’m still playing with volumes in this but don’t have anything interesting enough to paste here – maybe later.

Source:
https://docs.docker.com/compose/install/
https://docs.docker.com/compose/completion/

Some usefull commands in Docker

I started playing with Docker and here I will write some commands that where not so obvious at beginning 🙂

List running containers:

docker ps

List also not running containers:

docker ps -a

Remove all containers (be careful with that):

docker rm $(docker ps -a -q)

Remove all images:

docker rmi $(docker images -q)

Docker won’t remove any old volumes used by containers, so after some time you may be interested in deleting them all:

docker volume rm $(docker volume ls -q)

Run container and enter bash:

docker run --name deb -t -i debian:jessie /bin/bash

Run containter and remove it after exit:

docker run --name deb -t -i --rm debian:jessie /bin/bash

Show build logs from container:

docker logs deb

Enter bash into running container:

docker exec -it deb /bin/bash

Build image from Dockerfile in current directory:

docker build -t my_web .

After playing a little with this command I started searching for something to automate my tasks a little, and found docker-compose – you may be interested in it too.

Source:
https://techoverflow.net/blog/2013/10/22/docker-remove-all-images-and-containers/

Manual installation of Docker on Debian/Ubuntu

I've played with Docker a little in it early days but didn't stick for longer with it. It's stable now so I wanted to check how it's running now.

I really can't accept this method of installation:

$ curl -fsSL https://get.docker.com/ | sh

I think that world is going to it's end when I see such scritps… I prefer to do this manually, knowing exactly what I have to do.

Install prerequisites:

apt-get update
apt-get install -y apt-transport-https ca-certificates

Purge old packages if you used them:

apt-get purge lxc-docker*
apt-get purge docker.io*

Add GPG key:

apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Add repo – use ONLY ONE repo appropriate for your system (lsb_release -a to check):

echo "deb https://apt.dockerproject.org/repo debian-wheezy main" > /etc/apt/sources.list.d/docker.list
echo "deb https://apt.dockerproject.org/repo debian-jessie main" > /etc/apt/sources.list.d/docker.list
echo "deb https://apt.dockerproject.org/repo debian-stretch main" > /etc/apt/sources.list.d/docker.list
echo "deb https://apt.dockerproject.org/repo ubuntu-precise main" > /etc/apt/sources.list.d/docker.list
echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" > /etc/apt/sources.list.d/docker.list

Refresh repos and install Docker:

apt-get update
apt-get install -y docker-engine

Start service if it's not running:

service docker start

Grant access to docker service for non-root user

I don't like to use apps that require me to use root account. Docker even advice not to do so – service is running as root and you should add docker group and user to it to grant access to service socket:

groupadd docker
gpasswd -a ${USER} docker
service docker restart

Now logout, login again and and you should be able to use docker command:

docker version
docker info
docker run hello-world

Have fun 🙂

Source:
https://docs.docker.com/linux/
https://docs.docker.com/engine/installation/