1. docker 组件:
1. Docker client, Docker server(daemon, container) C/S 架构
2. Docker Image
3. Registry ( Dokcer Hub)
4. Docker container
2. 什么是Docker: 一个镜像格式,一系列标准的操作,一个执行环境。
3. cdDocker 安装
只支持 64bit
Kernal: 3.8 以上
dongli@ubuntu:~$ uname -a
Linux ubuntu 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
dongli@ubuntu:~$
第一次添加Docker的repository到你的本地秘钥库
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
将Docker添加到你的apt软件安装源中,然后update和install lxc-docker,期间会遇到一个警告,说这个包不可靠,你只需要回复yes然后继续安装就可以了!
sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get install lxc-docker
进入 DEBUG mode: DEBUG=1 docker -d
4. 查看docker程序是否存在
dongli@ubuntu:~$ sudo docker info
Containers: 0
Images: 0
Server Version: 1.9.1
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 0
Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 4.4.0-31-generic
Operating System: Ubuntu 16.04.1 LTS (containerized)
CPUs: 2
Total Memory: 1.937 GiB
Name: ubuntu
ID: H7FQ:ZCAP:UPBA:BRPV:TQMZ:BYDK:VEBF:HWYH:KQLF:TQIC:TY7M:CR6W
WARNING: No swap limit support
5. 启动docker:
Sudo docker run -i -t ubuntu /bin/sh
-i: 保证容器中的STDIN是开启的
-t: 持久的标准输入是交互式shell的‘半边天’, -t 标注则是另外的‘半边天’它告诉dockere为要创建的容器分配一个伪tty终端。
ubuntu:使用ubuntu镜像。为基础镜像。
7. 查看容器列表:
Sudo docker ps -a
dongli@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f320c5dc241 ubuntu "/bin/bash" 2 days ago Created cranky_bhabha
8022d81a6aeb ubuntu "/bin/bash" 2 days ago Created stoic_raman
8. 重新启动一个容器:
dongli@ubuntu:~$ sudo docker start 4f320c5dc241
4f320c5dc241
dongli@ubuntu:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f320c5dc241 ubuntu "/bin/bash" 2 days ago Up 11 seconds cranky_bhabha
9. 附着到容器上:
dongli@ubuntu:~$ sudo docker attach 4f320c5dc241
root@4f320c5dc241:/#
第一个容器:
root@4f320c5dc241:/# hostname
4f320c5dc241
root@4f320c5dc241:/# cat /etc/hosts
172.17.0.2 4f320c5dc241
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
root@4f320c5dc241:/# ip a
bash: ip: command not found
root@4f320c5dc241:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 14:53 ? 00:00:00 /bin/bash
root 16 1 0 15:01 ? 00:00:00 ps -ef
root@4f320c5dc241:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 18240 3176 ? Ss 14:53 0:00 /bin/bash
root 17 0.0 0.1 34424 2796 ? R+ 15:01 0:00 ps -aux
root@4f320c5dc241:/# apt-get update && apt-get install vim // install VIM
10. 创建守护式容器:
适合运行应用程序和服务
dongli@ubuntu:/etc$ sudo docker run --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo Hello Word; sleep 1; done"
755bea0d27809e5b1eac192127a43f3842c0e2fd1a3f86395bd585fa7a8e0761
11. 查看容器在干什么:
dongli@ubuntu:/etc$ sudo docker logs daemon_dave
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
dongli@ubuntu:/etc$ sudo docker logs -f daemon_dave类似tail -f
12. 查看容器中的进程:
Sudo docker top daemon_dave
在容器内部运行进程:
dongli@ubuntu:/etc$ sudo docker exec -d daemon_dave touch /etc/new_config_file
可以打开shell交互式任务
dongli@ubuntu:/etc$ sudo docker exec -t -i daemon_dave /bin/bash
root@755bea0d2780:/#
13. 停止守护式容器:
Sudo docker stop daemin_dave
14. 自动重启容器:
Docker run 增加参数 --restart = always/on-failure
Always 无论退出代码是什么都会重启
On-failure 退出代码非0 重启
15. 深入容器:
dongli@ubuntu:~$ sudo docker inspect daemon_dave
[
{
"Id": "755bea0d27809e5b1eac192127a43f3842c0e2fd1a3f86395bd585fa7a8e0761",
"Created": "2016-09-04T11:36:17.075016843Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true; do echo Hello Word; sleep 1; done"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2016-09-04T11:36:17.206658605Z",
"FinishedAt": "2016-09-04T11:46:15.00822756Z"
},
Docker inspect 对容器详细检查,并返回配置信息。
产看指定信息 -f 或者 --format
dongli@ubuntu:~$ sudo docker inspect --format='{{.State.Running}}' daemon_dave
false
dongli@ubuntu:~$ sudo docker inspect --format='{{.Name}} {{.State.Running}}' daemon_dave cranky_bhabha
/daemon_dave false
/cranky_bhabha false
16. 删除容器:
Sudo docker rm
17. Docker 镜像
最低端是一个文件引导系统,即bootfs
第二层root文件系统
创建容器时, Docker会构建一个镜像栈,并在栈的最顶端添加一个读写层。
18. 列出镜像
sudo docker images
19. 拉取镜像
Sudo docker pull
20. 查找镜像
dongli@ubuntu:~$ sudo docker search puppet
[sudo] password for dongli:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
macadmins/puppetmaster Simple puppetmaster based on CentOS 6 22 [OK]
devopsil/puppet Dockerfile for a container with puppet ins... 18 [OK]
macadmins/munki-puppet Basic munki nginx image with Puppet used f... 7 [OK]
21. 构建镜像
1)docker commit
- Docker build 和 dockerfile // 建议使用
21.1创建docker hub 账号
sudo docker login like cvs login 信息会存在.dockercfg
21.2 用docker的commit命令创建镜像
- 创建一个容器
Sudo docker run -I -t ubuntu /bin/bash
- 配置容器
例如安装apache2 并启动WEB 服务
- Exit退出容器
- Sudo docker commit
Sudo docker commit 4aab3asdfadf jamtur01/apache2
sudo docker commit -m="A new custom image" --author="Edgar Li" 4aab3asdfadf jamtur01/apache2:webserver
查看镜像详细信息:
Sudo docker inspect jamtur01/apache2:webserver
- 如何使用新的镜像
Sudo docker run -t -i jamtur01/apache2:webserver /bin/bash
21.3 用Dockerfile构建镜像
Dockerfile 基于DSL语言
Dockerfile:
dongli@ubuntu:~/Docker/static_web$ cat Dockerfile
#Version :0.0.1
FROM ubuntu:14.04 <- 指明了基础镜像
RUN apt-get update <-run 指令 ,更新apt, 安装nginx, 创建了 index.html
RUN apt-get install -y nginx
RUN echo 'Hi,I am in your container' \
> /usr/share/nginx/html/index.html
EXPOSE 80 <-指明 docker 端口
dongli@ubuntu:~/Docker/static_web$
sudo docker build -t="static_web:v1" .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:14.04
14.04: Pulling from library/ubuntu
bc2dc606ff95: Downloading 8.109 MB/67.11 MB
bc2dc606ff95: Pull complete
e1d4630ab77c: Pull complete
978638602519: Pull complete
f7b341b43ddd: Pull complete
701e05e0f5d6: Pull complete
e5ada6f7fa49: Pull complete
Digest: sha256:3e8f7386492c772d2135cae0ef0e30efe5586b84f463a61d4f8a264f99107952
Status: Downloaded newer image for ubuntu:14.04
---> e5ada6f7fa49
---> Running in dd45d0fee8da
---> 58ccd7d7199e
Removing intermediate container dd45d0fee8da
Step 3 : RUN apt-get update
---> Running in 5ff595c15925
。。。。。。。。。。。。。。。。。
Fetched 21.1 MB in 1min 42s (206 kB/s)
Reading package lists...
---> 96073c6fc657
Removing intermediate container 5ff595c15925
Step 4 : RUN apt-get install -y nginx
---> Running in f1c78f00753b
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
libxslt1.1 nginx-common nginx-core sgml-base xml-core
Suggested packages:
libgd-tools geoip-bin fcgiwrap nginx-doc sgml-base-doc debhelper
The following NEW packages will be installed:
fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
libxslt1.1 nginx nginx-common nginx-core sgml-base xml-core
0 upgraded, 25 newly installed, 0 to remove and 5 not upgraded.
Need to get 5589 kB of archives.
After this operation, 19.8 MB of additional disk space will be used.
。。。。。。。。。。。。。。。。。。。。。。。
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-core (1.4.6-1ubuntu3.8) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.4.6-1ubuntu3.8) ...
Processing triggers for libc-bin (2.19-0ubuntu6.13) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
---> 44516fcf099c
Removing intermediate container f1c78f00753b
Step 5 : RUN echo 'Hi,I am in your container' > /usr/share/nginx/html/index.html
---> Running in 8146e4ea840a
---> 3099173f8348
Removing intermediate container 8146e4ea840a
Step 6 : EXPOSE 80
---> Running in 82293929b2f9
---> 3978b420f894
Removing intermediate container 82293929b2f9
Successfully built 3978b420f894
查看新镜像:
dongli@ubuntu:~/Docker/static_web$
sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
static_web v1 3978b420f894 4 minutes ago 229.9 MB
ubuntu 14.04 e5ada6f7fa49 5 weeks ago 188 MB
ubuntu latest ae81bbda2b6c 14 months ago 126.6 MB
fedora 20 fc9f2c0fc04b 16 months ago 290.6 MB
从新镜像启动容器:
dongli@ubuntu:~/Docker/static_web$
sudo docker run -d -p 80 --name static_web static_web:v1 nginx -g "daemon off;"
a313eeb76ca22215c56624fa1dc0ea10eda7bd5546f0be554d74f8e07a933f0a
dongli@ubuntu:~/Docker/static_web$
sudo docker run -d -p 127.0.0.1::80 --name static_web static_web:v1 nginx -g "daemon off;"
0ed2482d4248b63ac832f7607bae9a48dfc5fdb669a33d69c14f8c88b2684828
dongli@ubuntu:~/Docker/static_web$
dongli@ubuntu:~/Docker/static_web$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ed2482d4248 static_web:v1 "nginx -g 'daemon off" 21 seconds ago Up 20 seconds 127.0.0.1:32768->80/tcp static_web
dongli@ubuntu:~/Docker/static_web$ curl 127.0.0.1:32768
Hi,I am in your container
dongli@ubuntu:~/Docker/static_web$
22. Dockerfile 指令:
1. CMD
#Version :0.0.1
FROM ubuntu:14.04
CMD ["/bin/bash","-l"]
可以被docker run 中的命令覆盖
只能有一条
2. ENTRYPOINT
不会docker run 覆盖
#Version :0.0.1
FROM static_web:v1
ENTRYPOINT ["/usr/sbin/nginx"]
dongli@ubuntu:~/Docker/test$ sudo docker run -d -p 127.0.0.1::80 --name test test -g "daemon off;" ( 等同 sudo docker run -d -p 127.0.0.1::80 --name test test nginx -g "daemon off;"
6742699ca88ceb06b7b53af48bb1af589a7777c8a53e1b3519220c45d25d7f03
dongli@ubuntu:~/Docker/test$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6742699ca88c test "/usr/sbin/nginx -g '" 7 seconds ago Up 6 seconds 127.0.0.1:32771->80/tcp test
dongli@ubuntu:~/Docker/test$ curl 127.0.0.1:32771
Hi,I am in your container
dongli@ubuntu:~/Docker/test$
docker run 命令中指定的任何参数都会被当作参数再次传递给ENTRYPOINT指令中的指定的命令。
3. WORKDIR
创建一个新容器时, 在容器内部设置一个工作目录,ENTRYPOINT和 CMD指定的程序会在这个目录下执行。
WORKDIR /opt/webapp/db
RUN bundle install
WORKDIR /opt/webapp
ENTRYPOINT ["rackup"]
docker run -w /var/log(覆盖工作目录)
4. ENV
指定构建环境变量
ENV RVM_PATH /home/rvm
RUN gem install unicorn -> RVM_PATH=/home/rvm gem install unicorn
ENV TARGET_DIR /opt/app
WORKDIR $TARGET_DIR
docker run -e "WEB_PORT=8080" -e: 传递环境变量
5. USER
指定镜像会以什么样的用户去运行
USER user
USER user:group
USER uid
USER uid:gid
USER user:gid
USER uid:group
docker run -u : 指定用户。 不指定default: root
6. VOLUME
指令用于向基于镜像创建的容器添加卷。
6.1 卷可以在容器间共享和重用
6.2 一个容器可以不是必须和其他容器共享卷
6.3 对卷的修改是立时生效的
6.4 对卷的修改不会对更新镜像产生影响
6.5 卷会一直存在直到没有任何容器再使用它。
VOLUME ["/opt/project","/data"]
7. ADD
将构建环境下的文件和目录复制到镜像中。
ADD software.lic /opt/application/software.lic
ADD http://word.com/latest.zip /root/latest.zip //目前docker 不支持以URL方式指定的源位置中使用归档文件
ADD latest.tar.gz /root //会自动解压,如果有文件一存在 ,不会覆盖
ADD 使得构建缓存变得无效。
8. COPY
类似ADD,但是不会解压和提取文件。 所有文件挥着目录都要与Dockerfile在同一目录
9. ONBUILD
为镜像添加触发器
#Version :0.0.1
FROM static_web:v1
ONBUILD ADD . /app/src
dongli@ubuntu:~/Docker/test$ sudo docker build -t="test" .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM static_web:v1
---> 3978b420f894
---> Running in d0cb19334714
---> 1e07c15a9c10
Removing intermediate container d0cb19334714
Step 3 : ONBUILD add.
---> Running in 37b812a8d6b1
---> 98b5da40769f
Removing intermediate container 37b812a8d6b1
Successfully built 98b5da40769f
dongli@ubuntu:~/Docker/test2$ cat Dockerfile
#Version :0.0.1
FROM test
dongli@ubuntu:~/Docker/test2$ sudo docker build -t="test2" .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM test
# Executing 1 build trigger...
Step 1 : ADD . /app https://web.sogou.com/?11661-0026
/src
---> 8b87ef7392ab
Removing intermediate container 9047e89ccbc1
---> Running in 7b6eb880dd7b
---> bffc2b3f7280
Removing intermediate container 7b6eb880dd7b
Successfully built bffc2b3f7280
23. 将镜像推送到Docker Hub
sudo docker push static_web
24. 删除jingxiang
sudo docker rmi
25. 运行自己的Docker Registry
dongli@ubuntu:~/Docker/test2$ sudo docker run -d -p 127.0.0.1:5000:5000 registry
dongli@ubuntu:~$ sudo docker tag 3978b420f894 127.0.0.1:5000/static_web
dongli@ubuntu:~$ sudo docker push 127.0.0.1:5000/static_web
The push refers to a repository [127.0.0.1:5000/static_web] (len: 1)
3978b420f894: Pushed
3099173f8348: Pushed
44516fcf099c: Pushed
96073c6fc657: Pushed
58ccd7d7199e: Pushed
701e05e0f5d6: Pushed
f7b341b43ddd: Pushed
978638602519: Pushed
e1d4630ab77c: Pushed
bc2dc606ff95: Pushed
latest: digest: sha256:d84bc72789021de239150992d79d2a231d71ed7c8b4a6d5a90a35b2f067b03a3 size: 13360
dongli@ubuntu:~$ sudo docker run -t -i 127.0.0.1:5000/static_web /bin/bash
root@a3ff6b024abe:/#