分类 Docker 下的文章

Docker 版本升级

在CentOS中,yum安装最高版本为1.5,所以,我们需要从官网下载最新的升级
CentOS中docker升级版本非常简单

wget https://get.docker.io/builds/Linux/x86_64/docker-latest -O docker

mv -f docker /usr/bin/docker

chmod  +x  /usr/bin/docker

service docker  restart

chkconfig docker on

Dockerfile:集成ssh和mysql

这个Dockerfile可以自动build集成ssh和mysql的image,并且可以通过修改my.cnf配置文件来自定义mysql数据库目录,同时可以指定ssh密码.
一共需要四个文件

1.Dockerfile:build镜像
2.run.sh :设置ssh,并且可以添加需要自启动的一些服务
3.set_root_pw.sh:启动容器时自定义ssh密码
4.my.cnf :mysql配置文件,可以修改相关参数

注:我在github中也提交了,地址是:https://github.com/insoz/docker-mysql

Dockerfile

FROM centos:centos6
MAINTAINER Fengnian Liu 

RUN yum -y install openssh-server epel-release && \
    yum -y install pwgen && \
    rm -f /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_rsa_key && \
    ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_ecdsa_key && \
    ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key && \
    sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config && \
    sed -i "s/UsePAM.*/UsePAM yes/g" /etc/ssh/sshd_config &&\
    yum -y install mysql mysql-devel mysql-server

VOLUME ["/data/docker/mysql","/data/mysql"]
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc/my.cnf
ADD set_root_pw.sh /set_root_pw.sh
ADD run.sh /run.sh
RUN chmod +x /*.sh
RUN yum clean all

ENV AUTHORIZED_KEYS **None**

EXPOSE 22 3306
CMD ["/run.sh"]

run.sh

#!/bin/bash

if [ "${AUTHORIZED_KEYS}" != "**None**" ]; then
    echo "=> Found authorized keys"
    mkdir -p /root/.ssh
    chmod 700 /root/.ssh
    touch /root/.ssh/authorized_keys
    chmod 600 /root/.ssh/authorized_keys
    IFS=$'\n'
    arr=$(echo ${AUTHORIZED_KEYS} | tr "," "\n")
    for x in $arr
    do
        x=$(echo $x |sed -e 's/^ *//' -e 's/ *$//')
        cat /root/.ssh/authorized_keys | grep "$x" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo "=> Adding public key to /root/.ssh/authorized_keys: $x"
            echo "$x" >> /root/.ssh/authorized_keys
        fi
    done
fi

if [ ! -f /.root_pw_set ]; then
        /set_root_pw.sh
fi
service mysqld start
/usr/bin/mysqladmin -u root password 'insoz'
sed -i "/mysqladmin/d" /run.sh
sed -i "/run/d" /run.sh
exec /usr/sbin/sshd -D

set_root_pw.sh

#!/bin/bash

if [ -f /.root_pw_set ]; then
        echo "Root password already set!"
        exit 0
fi

PASS=${ROOT_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${ROOT_PASS} ] && echo "preset" || echo "random" )
echo "=> Setting a ${_word} password to the root user"
echo "root:$PASS" | chpasswd

echo "=> Done!"
touch /.root_pw_set

echo "========================================================================"
echo "You can now connect to this CentOS container via SSH using:"
echo ""
echo "    ssh -p  root@"
echo "and enter the root password '$PASS' when prompted"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "========================================================================"

my.cnf

[client]
port		= 3306
socket		= /tmp/mysql.sock
[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
datadir = /data/mysql
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 20M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
query_cache_size = 32M
max_connections=500
wait_timeout=10
max_connect_errors = 100
wait_timeout=2880000
interactive_timeout = 2880000
log-bin=mysql-bin
binlog_format=mixed
server-id	= 1
innodb_data_home_dir = /data/mysql
innodb_data_file_path = ibdata1:500M;ibdata2:500M:autoextend
innodb_log_group_home_dir = /data/mysql
innodb_buffer_pool_size = 2G
innodb_additional_mem_pool_size = 2M
innodb_log_file_size = 512M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

使用说明

docker build

[root@insoz docker-mysql]# docker build -t insoz/mysql:yum .

启动容器

docker run --restart always -d -p 127.0.0.1:22:22 -p 3306:3306 -v /data/docker/mysql:/data/mysql --name DB insoz/mysql:yum

设置数据库文件目录

-v /data/docker/mysql:/data/mysql

设置mysql密码
编辑 run.sh

/usr/bin/mysqladmin -u root password 'insoz'

设置ssh的root密码
启动容器时做如下操作

 docker run -d -p 0.0.0.0:2222:22 -e ROOT_PASS="mypass" insoz/mysql:yum

进入容器

ssh -p 22 root@127.0.0.1
docker exec -it 容器ID /bin/bash