docker
Posted Jason__Zhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker相关的知识,希望对你有一定的参考价值。
一台服务器同时运行一百个docker成为现实
docker 基于go语言
docker 1.10.3
安装docker
#docker安装软件问题
root@268e83f76c36:/# apt-get install build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package build-essential
#解决
apt-get update
#然后可以使用
apt-get install net-tools
#docker ip
[root@node .ssh]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:beff:fed9:4808 prefixlen 64 scopeid 0x20<link>
ether 02:42:be:d9:48:08 txqueuelen 0 (Ethernet)
RX packets 8575 bytes 368509 (359.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11048 bytes 66640365 (63.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#centos 7
sudo yum install -y docker
#启动服务
sudo service docker start
#安装最新的ubuntu
sudo docker pull ubuntu
sudo docker pull ubuntu:14.04
sudo docker pull ubuntu
dev.aliyun.com
sudo docker pull ubuntu
sudo docker pull usgmxag3.mirror.aliyuncs.com centos
https://usgmxag3.mirror.aliyuncs.com
#docker 运行bash
sudo docker run -t -i ubuntu /bin/bash
#显示结果
root@dbb94dd813a0:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"
root@dbb94dd813a0:/#
#显示机器上已有的镜像
[ceph_user@node ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 0f192147631d 5 days ago 132.7 MB
[ceph_user@node ~]$
#显示docker 的全部信息
sudo docker inspect 0f192147631d
#搜寻镜像
sudo docker search mysql
#查看本机存在的所有容器
[ceph_user@node ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbb94dd813a0 ubuntu "/bin/bash" 18 minutes ago Exited (0) 16 minutes ago sleepy_elion
33bf5dea5890 ubuntu "/bin/bash" 22 minutes ago Exited (0) 20 minutes ago trusting_ritchie
[ceph_user@node ~]$
docker的创建
- 已有镜像导入
sudo docker commit 688b91ec4b28 test
本地模板导入
dockerfile创建
比较复杂,最后讲
容器
#创建容器
[ceph_user@node ~]$ sudo docker create -it ubuntu
6a0550ce62637c6faf5aee2544c52a5dec3c58c658f93b180be8a38cfd85980d
[ceph_user@node ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a0550ce6263 ubuntu "/bin/bash" 3 minutes ago Created
#让docker 以守护进程实现
docker run -d ubuntu /bin/sh -c "while true; do echo hello world;sleep 1;done"
#查看具体信息
[root@node ceph_user]# docker logs cf
hello world
hello world
hello world
#停止容器
docker stop ID -t 10
#进入容器
[root@node ceph_user]# docker run -idt ubuntu
d6231078272f7e25165774182433d42608945f445090c90d15be16e76dfa9d03
[root@node ceph_user]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6231078272f ubuntu "/bin/bash" 4 seconds ago Up 3 seconds sick_borg
[root@node ceph_user]#
[root@node ceph_user]# docker exec -ti 12d /bin/bash
root@12d0001e3ec5:/#
#导出导入容器
[root@node ceph_user]# docker export 12d0001e3ec5 > test.tar
[root@node ceph_user]# ls
a docker_help.txt rados-java test.tar
[root@node ceph_user]#
#导入容器
[root@node ceph_user]# cat test.tar | docker import - test/ubuntu:v1.0
sha256:95ee9999b31c814b912ef560429e8044e44737086fe23fe5c00be2e18acb666e
[root@node ceph_user]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.0 95ee9999b31c 7 seconds ago 118.3 MB
test latest 117b3dd07416 12 hours ago 132.7 MB
docker.io/ubuntu latest 0f192147631d 5 days ago 132.7 MB
[root@node ceph_user]#
仓库
镜像的管理
6数据管理
- 数据卷
- 数据卷容器
数据卷
#创建数据卷
[root@node ceph_user]# docker run -dti -P --name web_test -v /webapp ubuntu
268e83f76c3657588c7fc66c9f6fef50a310e354b4a8284a12288dd8109c3e5e
[root@node ceph_user]# docker exec -ti 268 /bin/bash
root@268e83f76c36:/#
root@268e83f76c36:/# ls | grep web
webapp
root@268e83f76c36:/#
#将本地 /home/ceph_user 挂载到容器的 /webapp
docker run -tid -P --name web_file -v /home/ceph_user:/webapp ubuntu
数据卷容器
#建立容器
docker run -it -v /dbdata --name dbdata ubuntu
#其他容器挂载dbdata的数据卷
docker run -it --volumes-from dbdata --name db1 ubuntu
root@facc49209c57:/# ls
bin core dev home lib64 mnt proc run srv tmp var
boot dbdata etc lib media opt root sbin sys usr
#在dbdata文件夹中的数据,所有的容器都有可以看到
7 网络基础配置
[root@node ceph_user]# docker run -tid -P --name net ubuntu
14f3130362c76a12520db7c4f35fd94de5046a7d3d58422675d8f0fc8b6e575d
[root@node ceph_user]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14f3130362c7 ubuntu "/bin/bash" 7 seconds ago Up 6 seconds net
[root@node ceph_user]#
8 Dockerfile创建镜像
9 操作系统
#创建支持ssh的镜像
apt-get install openssh-server
mkdir -p /var/run/sshd
#commit
docker commit 2c17 sshd:ubuntu
[root@node ceph_user]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd ubuntu 7e5180cc2e4f About a minute ago 259.9 MB
test/ubuntu v1.0 95ee9999b31c 5 hours ago 118.3 MB
test latest 117b3dd07416 18 hours ago 132.7 MB
docker.io/ubuntu latest 0f192147631d 5 days ago 132.7 MB
docker.io/busybox latest 2b8fd9751c4c 11 days ago 1.093 MB
[root@node ceph_user]#
#启动容器
[root@node ceph_user]# docker run -p 10022:22 -d sshd:ubuntu /run.sh
7ccac1087eb9456bec7a01faa56c560f8ebc59de86fc736c480391cc69bd534e
[root@node ceph_user]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ccac1087eb9 sshd:ubuntu "/run.sh" 44 seconds ago Up 43 seconds 0.0.0.0:10022->22/tcp loving_goodall
#通过本机的 10.0.0.128 访问端口 10022
[root@node ceph_user]# ssh 10.0.0.128 -p 10022
The authenticity of host '[10.0.0.128]:10022 ([10.0.0.128]:10022)' can't be established.
ECDSA key fingerprint is 71:36:25:29:17:7b:b5:cc:4d:06:e0:b9:4e:ed:98:e7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.0.128]:10022' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04 LTS (GNU/Linux 3.10.0-327.22.2.el7.x86_64 x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
root@7ccac1087eb9:~#
12 数据库应用
docker pull registry.mirrors.aliyuncs.com/library/centos
docker 核心技术
root 2609 1 0 Jul05 ? 00:00:00 /bin/sh -c /usr/bin/docker-current daemon --exec-opt native.cgroupdriver=systemd $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY 2>&1 | /usr/bin/forward-journald -tag docker
root 2610 2609 0 Jul05 ? 00:02:01 /usr/bin/docker-current daemon --exec-opt native.cgroupdriver=systemd --selinux-enabled --log-driver=journald
root 2611 2609 0 Jul05 ? 00:00:00 /usr/bin/forward-journald -tag docker
root 10101 2610 0 00:02 ? 00:00:01 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 10022 -container-ip 172.17.0.6 -container-port 22
root 11311 3724 0 04:12 pts/3 00:00:00 grep --color=auto docker
以上是关于docker的主要内容,如果未能解决你的问题,请参考以下文章
Docker学习__docker命令[docker version 和 docker info]