Docker 命令笔记

搜索镜像
docker search image

下载镜像
docker pull image

交互式终端 -t标示在心容器内指定一个伪终端或终端,-i标示允许我们对容器内的STDIN进行交互。
docker run -t -i ubuntu:14.04 /bin/bash

守护进程 -d标识告诉docker运行容器在后台模式运行
docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

docker logs命令看起来想运行在容器内并且返回标准输出
docker logs insane_babbage

查看所有容器
docker ps -a

从该镜像上创建一个容器
docker run --rm -ti ubuntu /bin/bash

容器内执行程序
docker exec -ti name /bin/bash

命名容器
docker run -d -P --name web training/webapp python app.py

基于基础镜像创建新镜像
docker commit -m="Added json gem" -a="Kate Smith" 0b2616b0e5a8 minecraft/java:170
-m标示是允许我们指定提交的信息,就像你提交一个版本控制。-a标示允许对我们的更新指定一个用户。

使用-p标识来指定容器端口绑定到主机端口
docker run -d -p 5000:5000 training/webapp python app.py

添加一个数据卷 -v 标识来给容器内添加一个数据卷
docker run -d -P --name web -v /webapp training/webapp python app.py

-v 还可以挂载本地主机目录到容器中 挂载本地目录/src/webapp到容器的/ot/webapp目录
docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
注意:如果挂载好后在容器内无法访问挂载的目录,可能是SELinux没有关闭导致的原因。

查看容器中运行着哪些进程
docker top nginx_dist

查看容器IP和主机等信息
docker inspect nginx_dist |grep 172.17

连接到容器上,--sig-proxy可以保证 Ctrl+D、Ctrl+C 不会退出
docker attach --sig-proxy=false name
当然也可以使用 Ctrl + Q + P 多按几次也能达到同样的效果

修改容器配置
docker stop test01
docker commit test01 test02
docker run -p 8080:8080 -td test02

####自动构建####
创建构建文件
vi Dockerfile

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
FROM centos:latest
MAINTAINER Srarservice@chinasrar.com

WORKDIR /opt/

RUN yum install -y wget java-1.7.0-openjdk
RUN wget http://cdn.x-speed.cc/docker.minecraft/craftbukkit-172.jar

EXPOSE 25565

ENTRYPOINT /usr/bin/java -Xms256M -Xmx1G -jar /opt/craftbukkit-172.jar
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

开始构建
sudo docker build --tag minecraft:1.7.2 .

保存镜像到一个tar包; -o, --output="" Write to an file
docker save image_name -o file_path
加载一个tar包格式的镜像; -i, --input="" Read from a tar archive file
docker load -i file_path

机器a
docker save image_name > /home/save.tar
使用scp将save.tar拷到机器b上,然后:
docker load < /home/save.tar

开启 Docker Remote API
编辑 /etc/sysconfig/docker other_args="" 修改为 other_args="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock"

安装Ping
apt-get install -yqq inetutils-ping

提示错误 无法启动容器 Error response from daemon: Cannot start container
修改Docker配置文件 /etc/sysconfig/docker other-args 改为 other_args="--exec-driver=lxc --selinux-enabled"

容器生命周期管理 — docker [run|start|stop|restart|kill|rm|pause|unpause]
容器操作运维 — docker [ps|inspect|top|attach|events|logs|wait|export|port]
容器rootfs命令 — docker [commit|cp|diff]
镜像仓库 — docker [login|pull|push|search]
本地镜像管理 — docker [images|rmi|tag|build|history|save|import]
其他命令 — docker [info|version]

C#自定义事件

声明委托

public delegate void ServerHandler(ServerConfig e);

在需要被触发的事件类内绑定委托

public event ServerHandler ServerStop;
public event ServerHandler ServerStart;

触发事件

//防止没有订阅引发异常
if(ServerStart != null) ServerStart(new ServerConfig()); 
if(ServerStop != null)  ServerStop(new ServerConfig());

订阅事件

Server.ServerStart += new ServerHandler(this.Server_Start);
Server.ServerStop += new ServerHandler(this.Server_Stop);

private void Server_Start(ServerConfig e)
{
    MessageBox.Show("服务端进程启动~");
}

private void Server_Stop(ServerConfig e)
{
    MessageBox.Show("服务端进程退出~");
}

记得释放事件 以免发生内存泄漏!

CentOS7 安装 LNMP

CentOS7 configuration:

systemctl disable firewalld && systemctl stop firewalld

Install dependency package:

yum install wget unzip tar gcc gcc-c++ make \
bison ncurses perl pcre pcre-devel zlib-devel cmake \
gnutls openssl jemalloc libaio libaio-devel ncurses \
ncurses-devel libxml2 libxml2-devel \
openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel \
libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel libvpx libvpx-devel \
libmcrypt libmcrypt-devel -y

MySQL

Download and install MySQL:

cd /usr/src/ && wget https://files.x-speed.cc/link/8132351340035751 -O mysql.tar.gz && tar -xvf ./mysql.tar.gz && cd mysql-*

Complie MySQL:

cmake \
-DCMAKE_INSTALL_PREFIX=/opt/mysql \
-DMYSQL_DATADIR=/opt/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/dev/shm/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

make -j 8 && make install

Configuration MySQL permission:

useradd -M -r --shell /sbin/nologin --home-dir /opt/mysql mysql && chown -R mysql:mysql /opt/mysql

rm -rf /etc/my.cnf && cd /opt/mysql && ./scripts/mysql_install_db --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql

cp ./support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/init.d/mysqld && systemctl enable mysqld

echo -e '\n\nexport PATH=/opt/mysql/bin:$PATH\n' >> /etc/profile && source /etc/profile

vi /etc/my.cnf

Edit MySQL configuration file.

[client]
port = 3306
socket = /dev/shm/mysql.sock

[mysqld]
user = mysql
skip-innodb
default-storage-engine=MyISAM
skip-name-resolve
bind-address = 0.0.0.0
port = 3306
socket = /dev/shm/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 240K
server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout

Launch MySQL process:

systemctl start mysqld

Nginx

Download and install Nginx:

cd /usr/src/ && \
wget https://files.x-speed.cc/link/8132351340035753 -O nginx-ct.tar.gz && tar -xvf ./nginx-ct.tar.gz && \
wget https://files.x-speed.cc/link/8149821340045787 -O openssl.tar.gz && tar -xvf ./openssl.tar.gz && \
wget https://files.x-speed.cc/link/6132321318156618 -O nginx.tar.gz && tar -xvf ./nginx.tar.gz && cd nginx-*

Complie Nginx:

./configure --prefix=/opt/nginx --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-stream --with-openssl=../openssl-1.0.2k --add-module=../nginx-ct-1.3.2

make -j 8 && make install

Edit Nginx service file:

vi /etc/init.d/nginx
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /opt/nginx/logs/nginx.pid

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $”Reloading $prog: ”
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
    esac

Configuration Nginx permission and register service:

useradd -M -r --shell /sbin/nologin --home-dir /opt/nginx www && chown -R www:www /opt/nginx && \
chmod +x /etc/init.d/nginx && \
systemctl enable nginx && \
systemctl start nginx

PHP

Download and install PHP:

cd /usr/src/ && wget https://files.x-speed.cc/link/6132321318156617 -O php.tar.gz && tar -xvf ./php.tar.gz && cd php-*

Complie PHP:

./configure --prefix=/opt/php \
--with-config-file-path=/opt/php/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--with-fpm-user=www --with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-gd

make -j 8 && make install

Configuration php:

cp ./php.ini-production /opt/php/etc/php.ini && \
cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf && \
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod +x /etc/init.d/php-fpm

Register php-fpm service:

systemctl enable php-fpm && systemctl start php-fpm

Configuration php-fpm(/opt/php/etc/php-fpm.conf):

[global]
pid = run/php-fpm.pid
log_level = error

[www]
listen = 127.0.0.1:9000
user = www
group = www
pm = dynamic
pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 5
request_terminate_timeout = 30
rlimit_files = 51200

Bukkit动态引入Jar包

需要把Jar包丢进配置文件夹的lib目录下才行
onEnable()时调用addBukkitLibraries()即可!

public void addBukkitLibraries()
{
        try
            {
                Method method = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });
                method.setAccessible(true);
                ClassLoader loader = this.getClassLoader();
                for (File file : new File(this.getDataFolder(), "lib").listFiles())
                {
                    if (file.getName().endsWith(".jar"))
                    {
                        URL url = file.toURI().toURL();
                        method.invoke(loader, new Object[] { url });
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }