четверг, 11 апреля 2013 г.

Tips for using docker: remove running containers, make new image, run image, reading logs

Docker - in two words this is a manager of LXC containers.

But I guess you know more about docker if you are here.

Note: I don't know anything about your environment and running processes, so use next commands on your own risk.

Killing and removing containers

First of all if you are experimenting with docker next command for guaranteed killing all running lxc-processes is possible will useful:

$ ps aux | egrep lxc | egrep -o "root     [0-9]{5}" | egrep -o "[0-9]{5}" | xargs sudo kill -9

Soft killing and removing of docker containers

sudo docker ps -a -q -notrunc | xargs sudo docker rm

Killing and removing of docker containers by specific image name:

sudo docker ps -a | grep "$IMAGE" | awk '{print $1}' | sudo xargs docker rm

Making new image by copying JDK and some APP directory

#!/bin/bash

JDK=/usr/lib/jvm/jdk1.7.0 # My app is java-based
APPPATH=$1
IMAGENAME=$2
DOCKERIMAGE=base # base image

# Run docker in daemon mode
(docker -d || echo "Docker daemon already running") &
# Sleep for a few seconds maybe needed here

CONTAINERBASEPATH=/tmp
# Copy all needed directories
tar -c $JDK $APPPATH | docker run -i $DOCKERIMAGE /bin/sh -c \
"tar -C $CONTAINERBASEPATH/ -x ; \
chown -R root:root $CONTAINERBASEPATH ; \
chmod -R ug=rwx $CONTAINERBASEPATH ; \
chmod -R o=rx $CONTAINERBASEPATH"

# Find current running container id
CONTAINER=$(docker ps -a \
  | egrep -v '^ID' \
  | awk '{print $1}')
# small trick based on assumption
# that there is just one current
# running docker process,
# wait for resolving this issue:
# https://github.com/dotcloud/docker/issues/252

# Find old image and drop it from docker
OLDIMAGE=$(docker images | egrep "^$IMAGENAME" | awk '{print $3}' | egrep -v '^ID$')
if [[ $OLDIMAGE ]]; then
    docker rmi "$OLDIMAGE"
fi

# Make new image
docker commit -m "Added jdk and $APPPATH" "$CONTAINER" "$IMAGENAME"

# Finally remove running container
docker rm "$CONTAINER"

Using:

$ sudo ./make-image.sh /home/your/app yourcompany/project

Check that image was created:

$ sudo docker images

Run image

#!/bin/bash
JDK=/usr/lib/jvm/jdk1.7.0
APPPATH=$1
DOCKERIMAGE=$2
SOMEPARAMETER1="1"
SOMEPARAMETER2="2"

# Run docker in daemon mode
(docker -d || echo "Docker daemon already running") &
# Sleep for a few seconds maybe needed here

CONTAINERBASEPATH=/tmp
# Build your command line by line
CMD="cd $CONTAINERBASEPATH/$APPPATH"
#CMD="$CMD ; $CONTAINERBASEPATH/$JDK/bin/java -Xdebug \
#   -Xrunjdwp:transport=dt_socket,server=y,address=\"16600\" -jar $APPJAR"
CMD="$CMD ; $CONTAINERBASEPATH/$JDK/bin/java -jar $APPJAR"
# Run app from image under some user rights and with bounded memory
JOB=$(docker run -u irc -m 536870912 -d $DOCKERIMAGE \
       /bin/sh -c "$CMD")
#echo "Daemon received: $(docker logs $JOB)"

Using:

$ sudo ./run-app.sh /home/your/app yourcompany/project "parameter1" "parameter2"

Reading logs from all running containers

sudo docker ps -a | grep -v '^ID' | awk '{print $1}' | xargs -n1 sudo docker logs

Note: You can filter containers by image name or something else of course.

Комментариев нет:

Отправить комментарий