初入docker
什么是虚拟化
什么是docker
待续
为什么需要使用docker
- 我的linux系统是centos6.8,由于上面安装的依赖太多,移植很繁琐,甚至会遇见很多不可知的问题,但是我必须使用centos7.5,那么我需要docker。
- redis3.4用的好好的,不过redis4.0出来了,性能更好了,这就需要我部署4.0,我们都知道升级软件一不注意就会造成不兼容问题。
- 写好了一个应用,但是就算移植到大致一样的操作系统上,也有可能出问题。
docker的安装
docker基本概念
image:镜像,相当于一个类。
container:容器,相当于实例化一个类
docker的基本命令
启动docker
systemctl start docker
关闭docker
systemctl stop docker
查看当前镜像
docker images
主机与container之间的相互复制文件
docker cp resource dist
docker cp /www/runoob 96f7f14e99ab:/www/
docker cp 96f7f14e99ab:/www/runoob /www/
启动一个容器
docker run -t -i image /bin/bash
- docker run:启动container
- ubuntu:你想要启动的image
- -t:进入终端
- -i:获得一个交互式的连接,通过获取container的输入
- /bin/bash:在container中启动一个bash shell
查看运行中的容器
docker ps -a
退出容器
exit
进入容器
attach
container运行在后台,如果想进入它的终端使用:
docker attach container的id
exec
使用“docker attach”命令进入container(容器)有一个缺点,那就是每次从container中退出到前台时,container也跟着退出了。
要想退出container时,让container仍然在后台运行着,可以使用“docker exec -it”命令。每次使用这个命令进入container,当退出container后,container仍然在后台运行,命令使用方法如下:
docker exec -it goofy_almeida /bin/bash
对了对于需要systemctl启动的应用,你应该:跳转另一篇文章
docker打包上传hub
构建镜像
构建Docker镜像有以下两种方法:
1、使用docker commit命令。
2、使用docker build命令和 Dockerfile 文件。
这里介绍commit。
当我们修改了一个container并退出后,可以查看退出的container的id,运行如下命令:
docker commit -m="A new custom image" --author="yangyuya" b437ffe4d630 test/apache2:webserver
解释:
-m 用来指定创建镜像的提交信息;
–author 用来列出该镜像的作者信息;
b437ffe4d630 是id;
最后自定义一个test/apache2:webserver标签。
上传镜像
特殊处理
动态端口映射
点击查看博客
我修改了他的脚本
sh docker_add_port.sh containerID add或者del 主机IP:主机端口:container端口
sh docker_add_port.sh 8d17dec159e8 add 192.168.108.59:8001:8001
#!/bin/bash
# filename: docker_expose.sh
if [ `id -u` -ne 0 ];then
echo "[EROOR] Please use root to run this script"
exit 23
fi
if [ $# -ne 3 ];then
echo "Usage: $0 <container_name> <add|del> [[<machine_ip>:]<machine_port>:]<container_port>[/<protocol_type>]"
exit 1
fi
IPV4_RE='(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
container_name=$1
action=$2
arguments=$3
# check action
if [ "$action"x != "add"x -a "$action"x != "del"x ];then
echo "[ERROR] Please use add or del parameter to add port map or delete port map"
exit 654
fi
if [ "$action"x == "add"x ];then
action="A"
else
action="D"
fi
# get container ip by container name
container_ip=`docker inspect -f '{{.NetworkSettings.IPAddress}}' $container_name 2> /dev/null`
if [ -z $container_ip ];then
echo "[ERROR] Get container's (${container_name}) IP error, please ensure you have this container"
exit 2
fi
# parse arguments
protocol_type=`echo $arguments | awk -F '/' '{print $2}'`
if [ -z $protocol_type ];then
protocol_type="tcp"
fi
# check protocol
if [ "$protocol_type"x != "tcp"x -a "$protocol_type"x != "udp"x ];then
echo "[ERROR] Only tcp or udp protocol is allowed"
exit 99
fi
machine_ip=''
machine_port=''
container_port=''
# split the left arguments
arguments=${arguments%/*}
machine_ip=`echo $arguments | awk -F ':' '{print $1}'`
machine_port=`echo $arguments | awk -F ':' '{print $2}'`
container_port=`echo $arguments | awk -F ':' '{print $3}'`
if [ -z $machine_port ];then
# arguments is: 234
container_port=$machine_ip
machine_port=$machine_ip
unset machine_ip
elif [ -z $container_port ];then
# arguments is: 234:456
container_port=$machine_ip
machine_port=$machine_port
unset machine_ip
fi
# check port number function
_check_port_number() {
local port_num=$1
if ! echo $port_num | egrep "^[0-9]+$" &> /dev/null;then
echo "[ERROR] Invalid port number $port_num"
exit 3
fi
if [ $port_num -gt 65535 -o $port_num -lt 1 ];then
echo "[ERROR] Port number $port_num is out of range(1-56635)"
exit 4
fi
}
# check port and ip address
_check_port_number $container_port
_check_port_number $machine_port
if [ ! -z $machine_ip ];then
if ! echo $machine_ip | egrep "^${IPV4_RE}$" &> /dev/null;then
echo "[ERROR] Invalid Ip Adress $machine_ip"
exit 5
fi
# check which interface bind the IP
for interface in `ifconfig -s | sed -n '2,$p' | awk '{print $1}'`;do
interface_ip=`ifconfig $interface | awk '/broadcast/{print $2}'`
if [ "$interface_ip"x == "$machine_ip"x ];then
interface_name=$interface
break
fi
done
if [ -z $interface_name ];then
echo "[ERROR] Can not find interface bind with $machine_ip"
exit 98
fi
fi
# run iptables command
echo "[INFO] Now start to change rules to iptables"
echo "[INFO] Changing POSTROUTING chain of nat table"
iptables -t nat -${action} POSTROUTING -p ${protocol_type} --dport ${container_port} -s ${container_ip} -d ${container_ip} -j MASQUERADE
if [ -z $interface_name ];then
echo "[INFO] Changing DOCKER chain of filter table"
iptables -${action} DOCKER ! -i docker0 -o docker0 -p ${protocol_type} --dport ${container_port} -d ${container_ip} -j ACCEPT
echo "[INFO] Changing DOCKER chain of nat table"
iptables -t nat -${action} DOCKER ! -i docker0 -p ${protocol_type} --dport ${machine_port} -j DNAT --to-destination ${container_ip}:${container_port}
else
echo "[INFO] Changing DOCKER chain of filter table"
iptables -${action} DOCKER -i $interface_name -o docker0 -p ${protocol_type} --dport ${container_port} -d ${container_ip} -j ACCEPT
echo "[INFO] Changing DOCKER chain of nat table"
iptables -t nat -${action} DOCKER -i $interface_name -p ${protocol_type} --dport ${machine_port} -j DNAT --to-destination ${container_ip}:${container_port}
fi