docker新手入门(1)之hello world

还记得之前kyrios大大说过工作室招新的时候docker那题没写的寒假得自学一下docker.....突然想起来这件事,于是简单地自学一下docker... 教程参考:http://www.runoob.com/docker/docker-tutorial.html 这里仅仅对于个人的学习做一个记录,觉得简单的话自己去看教程就好

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

教程开始给出这么一段话,先放着吧,等慢慢见识docker的功能以后相信原理应该也更好理解吧...

docker的安装

版本问题

我是用的ubuntu装的,docker要求ubuntu内核版本高于3.10,可以通过uname -r来查看当前的内核版本

1
2
blacsheep@blacsheep-myubuntu:~$ uname -r
4.10.0-37-generic
使用脚本安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
blacsheep@blacsheep-myubuntu:~$ sudo su
root@blacsheep-myubuntu:/home/blacsheep# wget -qO- https://get.docker.com/ sh
# Executing docker install script, commit: 1d31602
+ sh -c apt-get update -qq >/dev/null
+ sh -c apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" apt-key add -qq - >/dev/null
+ sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial edge" > /etc/apt/sources.list.d/docker.list
+ [ ubuntu = debian ]
+ sh -c apt-get update -qq >/dev/null
+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sh -c docker version
Client:
Version: 18.01.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: 03596f5
Built: Wed Jan 10 20:11:05 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm

Server:
Engine:
Version: 18.01.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: 03596f5
Built: Wed Jan 10 20:09:37 2018
OS/Arch: linux/amd64
Experimental: false
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

sudo usermod -aG docker your-user

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.

装好后有提示,如果要用非root用户直接运行docker时,需要执行sudo usermod -aG docker blacsheep,然后重新登录,否则会有问题,然后我们后台启动一下docker

1
root@blacsheep-myubuntu:/etc/docker# service docker start

至于镜像加速我是直接按教程里面来弄的...在/etc/docker/daemon.json里面写一个

1
2
3
4

{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}

安装就至此结束..

docker的使用

hello world

感觉一个栗子就让我意识到了docker的作用.... 教程开始一句话:Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。 输出hello world

1
2
3
4
5
6
7
8
9
10
11
root@blacsheep-myubuntu:/etc/docker# docker run ubuntu:16.10 /bin/echo "hello world"
Unable to find image 'ubuntu:16.10' locally
16.10: Pulling from library/ubuntu
dca7be20e546: Pull complete
40bca54f5968: Pull complete
61464f23390e: Pull complete
d99f0bcd5dc8: Pull complete
120db6f90955: Pull complete
Digest: sha256:9e39bf1a0bf29dd1bf28211e3b870134d080b5d2b6072b47c5970d13e5725fb6
Status: Downloaded newer image for ubuntu:16.10
hello world

参数解析: - docker:docker的二进制文件执行 - run:与前面的docker组合来运行一个容器 - ubuntu:16.10:docker首先本地查找镜像是否存在,如果不存在docker就会从镜像仓库docker hub里面下载公共镜像 - /bin/echo "hello world":在启动的容器里面执行的命令 完整的意思就是docker以ubuntu16.10镜像创建一个容器,容器里面运行bin/echo "hello world",然后输出结果

运行交互式容器

通过运行docker的两个参数可以实现和docker容器的交互

1
2
root@blacsheep-myubuntu:/etc/docker# docker run -i -t ubuntu:16.10 /bin/bash
root@34541a7cf8a3:/#

参数解析: - -t:在容器里指定一个终端或伪终端 - -i:允许你对容器内的标准输入进行交互 就像上面那个,我们就进入了一个新的ubuntu16.10的容器,可以ls来查看新的系统的目录,cat /proc/version来看系统的版本,然后退出的话可以用exit或者ctrl+d

后台模式
1
2
root@blacsheep-myubuntu:/etc/docker# docker run -d ubuntu:16.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
a1ccf7c2927e7032498e1b7ba3a172b0110ced4b0418f10c3e156d0d235627f1

这里解释一下-d参数,-d是表示后台运行容器,同时返回容器id 这里执行完了没有hello world显示,但是我们运行docker ps

1
2
3
root@blacsheep-myubuntu:/etc/docker# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1ccf7c2927e ubuntu:16.10 "/bin/sh -c 'while t…" About a minute ago Up About a minute agitated_northcutt

然后docker logs a1ccf7c2927e 来查看 blacsheep 发现其实容器是在后台运行的

停止容器

为了停止我们刚刚的命令,我们运行

1
2
3
4
5
root@blacsheep-myubuntu:/etc/docker# docker stop a1ccf7c2927e7032498e1b7ba3a172b0110ced4b0418f10c3e156d0d235627f1 
a1ccf7c2927e7032498e1b7ba3a172b0110ced4b0418f10c3e156d0d235627f1
root@blacsheep-myubuntu:/etc/docker# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@blacsheep-myubuntu:/etc/docker#

下面的docker ps证明已经成功停止了后台进程.

小结

这里发觉docker可以在一个新的容器里面完成各种需要的操作,对于环境的配置会简单很多很多。这是我个人目前的理解吧,这里主要是学习一下docker的操作,比较简单,后续再慢慢深入学习吧....