Skip to content

Instantly share code, notes, and snippets.

@aminamid
Last active August 29, 2015 14:00
Show Gist options
  • Save aminamid/11277041 to your computer and use it in GitHub Desktop.
Save aminamid/11277041 to your computer and use it in GitHub Desktop.
cheatsheets

netstatの代替

ss -onapet | cat
ss -onapet | grep `pgrep imdircacheserv`
ss -onapet4l | sort -k5

初期化

export MIRROR_URL="http://ftp.riken.jp/Linux/centos/6.5/os/x86_64/"
export MIRROR_URL_UPDATES="http://ftp.riken.jp/Linux/centos/6.5/updates/x86_64/"
febootstrap -i bash -i coreutils -i tar -i bzip2 -i gzip -i vim-minimal -i wget -i patch -i diffutils -i iproute -i yum centos centos65  $MIRROR_URL -u $MIRROR_URL_UPDATES

touch centos65/etc/resolv.conf
touch centos65/sbin/init

rm -rf centos65/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
rm -rf centos65/usr/share/{man,doc,info,gnome/help}
rm -rf centos65/usr/share/i18n
rm -rf centos65/var/cache/yum
mkdir -p --mode=0755 centos65/var/cache/yum
rm -rf centos65/sbin/sln
rm -rf centos65/etc/ld.so.cache var/cache/ldconfig
mkdir -p --mode=0755 centos65/var/cache/ldconfig


tar --numeric-owner -Jcpf centos-65.tar.xz -C centos65 .

cat centos-65.tar.xz | docker import - centos65_x86_64

sshd起動

docker build -t host.of.registry.com:5000/sshd:1.0 .
FROM centos65_x86_64:1.0
MAINTAINER Akihiro Minamida

ENV ROOT_PASSWORD root
ENV USER username

ADD epel.repo /etc/yum.repos.d/epel.repo

RUN yum install -y openssh openssh-client openssh-server sudo syslog nginx monit mysql-server pexpect passwd yp-tools unzip
RUN yum clean all

RUN cp -p /usr/share/zoneinfo/Japan /etc/localtime

ADD monit.sshd /etc/monit.d/sshd
ADD monit.mysqld /etc/monit.d/mysqld
ADD monit.nginx /etc/monit.d/nginx
ADD monit.httpport /etc/monit.d/httpport

RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config && sed -ri 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config
RUN sed -ri "s/%%IPADDRESS%%/0.0.0.0/" /etc/monit.conf

RUN touch /etc/sysconfig/network

RUN echo "$ROOT_PASSWORD" | passwd --stdin root
RUN useradd $USER && echo "$USER" | passwd --stdin $USER

RUN service mysqld start && /usr/bin/mysqladmin -u root password "$ROOT_PASSWORD"
RUN /etc/init.d/sshd start
RUN /etc/init.d/sshd stop

ADD parent.sh /root/parent.sh
ADD child.sh /root/child.sh

RUN chmod u+x /root/parent.sh
RUN chmod u+x /root/child.sh

ENTRYPOINT ["/bin/bash"]
CMD ["/root/parent.sh"]
#!/bin/bash
# parent.sh
# Original: http://d.hatena.ne.jp/toromoti/20140421/1398085281
#---------------------------------------------------------------
# forkness: child-process-manager in docker container
# USAGE:
#   forkness "foreground_command" [execute_user (default 'root')]
_forkness_pid_list=()
forkness() {
  local user=${2:-root}
  local pid_tmp_file=/tmp/su.$user.$$
  su -l $user -c "$1 3>&- & echo \$! 1>&3 && wait" 3> $pid_tmp_file &
  while [ ! -s $pid_tmp_file ]; do :; done
  _forkness_pid_list+=( $(<$pid_tmp_file) )
  rm -f $pid_tmp_file
}
_forkness_trap_action() {
  for pid in ${_forkness_pid_list[@]}; do
    kill -15 $pid
  done
  wait
}
trap _forkness_trap_action 15
#---------------------------------------------------------------

forkness "/etc/init.d/monit start"
forkness "/root/child.sh 2"
#forkness "/child.sh 3" bob     # bobユーザで実行
#forkness "/child.sh 4" john    # johnユーザで実行
#forkness "/child.sh 5" mary    # maryユーザで実行
#forkness "/child.sh 6"         # 第2引数になにもなければrootで実行
#forkness "/child.sh 7"
#forkness "/child.sh 8"

wait
#!/bin/bash
# child.sh
# Original: http://d.hatena.ne.jp/toromoti/20140421/1398085281

# SIGINTを受け取ったらログを書いて5秒後にexitする
trap "echo child $1 [$$] >> stop.log; sleep 5; exit" 15

# 無限ループ
while :; do :; done
[epel]
name=epel repo
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
check process sshd with pidfile /var/run/sshd.pid
    start program = "/etc/init.d/sshd start"
    stop  program = "/etc/init.d/sshd stop"
set httpd port 2812 and
  use address 0.0.0.0
  allow root:root
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
    start program = "/etc/init.d/mysqld start"
    stop  program = "/etc/init.d/mysqld stop"
check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop  program = "/etc/init.d/nginx stop"

起動

ホストOS上でdnsmasqをインストールして起動し、ホストOSの/etc/hostsに下記で指定するhost名を書くと、名前解決してくれる。

P=21 ; docker run -d -p ${P}022:22 -p ${P}443:443 -p ${P}080:080 -p ${P}081:2812 -v /home/kits:/mnt -h host${P}.name.of.container.com --dns=172.17.42.1 --name=cont${P} -t host.of.registry.com:5000/sshd:1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment