docker2
Docker镜像介绍
什么是image
docker 普通用户提权限(每次都需要sudo,麻烦)
(1)添加一个docker组 #sudo groupadd docker
(2)把普通用户添加到docker组里#sudo gpasswd -a vagrant docker
(3)重启服务#sudo service docker restart
(4)测试,退出重新登录:#docker version
拉取一个image #docker pull hello-world
#docker image ls
#docker pull hello-world 获取hello-world镜像
#docker run hello-world 运行
创建目录
#mkdir hello-world
#cd hello-world
#vim hello.c
-----
#include<stdio.h>
int main()
{
printf("hello docker\n");
}
-----
#yum install gcc glibc-static
# gcc -static hello.c -o hello
[root@zizhen02 hello-world]# ls
hello hello.c
创建一个Dockerfile
# cat Dockerfile
FROM scratch
ADD hello /
CMD ['/hello']
创建一个image, -t参数是打一个tag,最后加一个点号(表示当前目录找file)
[root@zizhen02 hello-world]# docker build -t xiaoli163/hello-world .
Sending build context to Docker daemon 860.7kB
Step 1/3 : FROM scratch
--->
Step 2/3 : ADD hello /
---> d3ce8749b11d
Step 3/3 : CMD ['/hello']
---> Running in 4d72aa29e7a3
Removing intermediate container 4d72aa29e7a3
---> a821094d0306
Successfully built a821094d0306
Successfully tagged xiaoli163/hello-world:latest
[root@zizhen02 hello-world]#
# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoli163/hello-world latest a821094d0306 37 seconds ago 857kB
ubuntu 14.04 5dbc3f318ea5 6 weeks ago 188MB
hello-world latest fce289e99eb9 2 months ago 1.84kB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 hello-world]#
[root@zizhen02 hello-world]# ls -lh
total 848K
-rw-r--r-- 1 root root 40 Mar 11 23:02 Dockerfile
-rwxr-xr-x 1 root root 837K Mar 11 22:55 hello
-rw-r--r-- 1 root root 64 Mar 11 22:48 hello.c
[root@zizhen02 hello-world]#
###
镜像介绍
1, docker container commit 简写:docker commit
[root@zizhen02 ~]# docker container commit (或者简写#docker commit)
"docker container commit" requires at least 1 and at most 2 arguments.
See 'docker container commit --help'.
Usage: docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
[root@zizhen02 ~]#
2, docker image build 简写:docker build
[root@zizhen02 ~]# docker image build (简写为#docker build)
"docker image build" requires exactly 1 argument.
See 'docker image build --help'.
Usage: docker image build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
[root@zizhen02 ~]# docker build
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
[root@zizhen02 ~]#
通过已经存在的image,创建一个新的image
运行centos container安装vim,退出
[root@zizhen02 ~]# docker run -it centos
[root@adeb5ede1e7a /]# yum install -y vim
[root@adeb5ede1e7a ~]# exit
exit
[root@zizhen02 ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
adeb5ede1e7a centos "/bin/bash" 5 minutes ago Exited (0) 47 seconds ago mystifying_hermann
[root@zizhen02 ~]#
Commit成一个新的image ,
[root@zizhen02 ~]# docker container commit
"docker container commit" requires at least 1 and at most 2 arguments.
See 'docker container commit --help'.
Usage: docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
[root@zizhen02 ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
adeb5ede1e7a centos "/bin/bash" 11 minutes ago Exited (0) 6 minutes ago mystifying_hermann
[root@zizhen02 ~]# docker commit
"docker commit" requires at least 1 and at most 2 arguments.
See 'docker commit --help'.
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
[root@zizhen02 ~]# docker container commit mystifying_hermann xiaoming163/centos-vim
sha256:a2af669c930e09ae1db6124ffbe3874553c28ed52be7084e480c9991fd1dbcac
[root@zizhen02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming163/centos-vim latest a2af669c930e 7 seconds ago 335MB
xiaoli163/hello-world latest a821094d0306 38 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@zizhen02 ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
adeb5ede1e7a centos "/bin/bash" 14 minutes ago Exited (0) 9 minutes ago mystifying_hermann
[root@zizhen02 ~]# docker history
"docker history" requires exactly 1 argument.
See 'docker history --help'.
Usage: docker history [OPTIONS] IMAGE
Show the history of an image
通过docker history xxx 查看image的层级关系
[root@zizhen02 ~]# docker history 1e1148e4cc2c
IMAGE CREATED CREATED BY SIZE COMMENT
1e1148e4cc2c 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:6f877549795f4798a… 202MB
[root@zizhen02 ~]# docker history a2af669c930e
IMAGE CREATED CREATED BY SIZE COMMENT
a2af669c930e 4 minutes ago /bin/bash 133MB
1e1148e4cc2c 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:6f877549795f4798a… 202MB
[root@zizhen02 ~]#
以上不提倡创建image, 不安全;
[root@zizhen02 ~]# docker image rm a2af669c930e
Untagged: xiaoming163/centos-vim:latest
Deleted: sha256:a2af669c930e09ae1db6124ffbe3874553c28ed52be7084e480c9991fd1dbcac
Deleted: sha256:991dfb3163719f8ad5692ebe404ebe8537f3ee49b9b472c9f7e9cd8126a43085
[root@zizhen02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoli163/hello-world latest a821094d0306 38 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 ~]#
用Dockerfile 创建新的image;
[root@zizhen02 ~]# mkdir docker-centos-vim
[root@zizhen02 ~]# cd docker-centos-vim/
[root@zizhen02 docker-centos-vim]# vim Dockerfile
[root@zizhen02 docker-centos-vim]# cat Dockerfile
FROM centos
RUN yum install -y vim
[root@zizhen02 docker-centos-vim]# ls
Dockerfile
[root@zizhen02 docker-centos-vim]# ll
total 4
-rw-r--r-- 1 root root 35 Mar 13 13:25 Dockerfile
[root@zizhen02 docker-centos-vim]# docker build -t xiaoming163/centos-vim-new .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM centos
---> 1e1148e4cc2c
Step 2/2 : RUN yum install -y vim
---> Running in 8b87ae034b92
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.shu.edu.cn
* updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.4.160-5.el7 will be installed
………………..
………………..
………………..
vim-common.x86_64 2:7.4.160-5.el7
vim-filesystem.x86_64 2:7.4.160-5.el7
which.x86_64 0:2.20-7.el7
Complete!
Removing intermediate container 8b87ae034b92
---> f9f19d2bc847
Successfully built f9f19d2bc847
Successfully tagged xiaoming163/centos-vim-new:latest
[root@zizhen02 docker-centos-vim]#
[root@zizhen02 docker-centos-vim]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming163/centos-vim-new latest f9f19d2bc847 5 minutes ago 335MB
xiaoli163/hello-world latest a821094d0306 39 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 docker-centos-vim]#more Dockerfile
Docker dkfile语法讲解和案例
第1个关键字: FROM---base image
第2个关键字:LABEL 标签
run运行一些命令
第3个关键字: workdir 设定当前工作目录,cd或者mkdir
关键字:ADD 和 COPY
ADD 1,可以把文件拷贝到目录。2,还可以解压缩
COPY 1,可以拷贝文件
添加远程文件:curl wget
env 设置常量
Docker代码库:https://github.com/docker-library
Dockerfile 官方文档:https://docs.docker.com/v17.09/engine/reference/builder/
Docker在虚拟机核心组件run vs cmd vs entrypoint
[root@zizhen02 ~]# mkdir cmd-run-entrypoint
[root@zizhen02 ~]# cd cmd-run-entrypoint/
[root@zizhen02 cmd-run-entrypoint]# vim Dockerfile
[root@zizhen02 cmd-run-entrypoint]# cat Dockerfile
FROM centos
ENV name Docker
ENTRYPOINT echo "hello $name"
[root@zizhen02 cmd-run-entrypoint]# docker build -t xiaoming/centos-entrypoint-shell .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : ENV name Docker
---> Running in 6fcabefdb228
Removing intermediate container 6fcabefdb228
---> e2827f0b9fb9
Step 3/3 : ENTRYPOINT echo "hello $name"
---> Running in 9741e86b2236
Removing intermediate container 9741e86b2236
---> 344ec38e6b59
Successfully built 344ec38e6b59
Successfully tagged xiaoming/centos-entrypoint-shell:latest
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 30 seconds ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 2 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 40 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker run xiaoming/centos-entrypoint-shell
hello Docker
[root@zizhen02 cmd-run-entrypoint]#
Exec格式如下;怎样被shell执行命令
[root@zizhen02 cmd-run-entrypoint]# ls
Dockerfile
[root@zizhen02 cmd-run-entrypoint]# vim Dockerfile
[root@zizhen02 cmd-run-entrypoint]# cat Dockerfile
FROM centos
ENV name Docker
ENTRYPOINT ["/bin/echo", "hello $name"]
[root@zizhen02 cmd-run-entrypoint]# docker build -t xiaoming/centos-entrypoint-exec .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : ENV name Docker
---> Using cache
---> e2827f0b9fb9
Step 3/3 : ENTRYPOINT ["/bin/echo", "hello $name"]
---> Running in 0f0607b2e879
Removing intermediate container 0f0607b2e879
---> b2137f7cf5da
Successfully built b2137f7cf5da
Successfully tagged xiaoming/centos-entrypoint-exec:latest
[root@zizhen02 cmd-run-entrypoint]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 15 seconds ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 4 minutes ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 2 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 40 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker run xiaoming/centos-entrypoint-exec
hello $name
[root@zizhen02 cmd-run-entrypoint]#
[root@zizhen02 cmd-run-entrypoint]# vim Dockerfile
[root@zizhen02 cmd-run-entrypoint]# docker build -t xiaoming/centos-entrypoint-exec-new1 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : ENV name Docker
---> Using cache
---> e2827f0b9fb9
Step 3/3 : ENTRYPOINT ["/bin/bash","-c","echo","hello $name"]
---> Running in 73f4b46e312b
Removing intermediate container 73f4b46e312b
---> 620ace42fe56
Successfully built 620ace42fe56
Successfully tagged xiaoming/centos-entrypoint-exec-new1:latest
[root@zizhen02 cmd-run-entrypoint]# cat Dockerfile
FROM centos
ENV name Docker
ENTRYPOINT ["/bin/bash","-c","echo","hello $name"]
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 20 seconds ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a About a minute ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 10 minutes ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 14 minutes ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 2 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 41 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker run xiaoming/centos-entrypoint-exec-new1
[root@zizhen02 cmd-run-entrypoint]# docker run xiaoming/centos-entrypoint-exec-new1 .
[root@zizhen02 cmd-run-entrypoint]# docker run xiaoming/centos-entrypoint-exec-new1
[root@zizhen02 cmd-run-entrypoint]# vim Dockerfile
[root@zizhen02 cmd-run-entrypoint]# cat Dockerfile
FROM centos
ENV name Docker
ENTRYPOINT ["/bin/bash","-c","echo hello $name"]
[root@zizhen02 cmd-run-entrypoint]# docker build -t xiaoming/centos-entrypoint-exec-new2 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : ENV name Docker
---> Using cache
---> e2827f0b9fb9
Step 3/3 : ENTRYPOINT ["/bin/bash","-c","echo hello $name"]
---> Running in 62752db2bbfa
Removing intermediate container 62752db2bbfa
---> 249a9c95d9be
Successfully built 249a9c95d9be
Successfully tagged xiaoming/centos-entrypoint-exec-new2:latest
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 8 seconds ago 202MB
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 6 minutes ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a 7 minutes ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 17 minutes ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 21 minutes ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 2 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 41 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker run xiaoming/centos-entrypoint-exec-new2
hello Docker
[root@zizhen02 cmd-run-entrypoint]#
[root@zizhen02 cmd-run-entrypoint]# vim Dockerfile
[root@zizhen02 cmd-run-entrypoint]#
[root@zizhen02 cmd-run-entrypoint]# cat Dockerfile
FROM centos
ENV name Docker
CMD echo "hello $name"
[root@zizhen02 cmd-run-entrypoint]# docker build -t xiaoming/centos-cmd-new .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : ENV name Docker
---> Using cache
---> e2827f0b9fb9
Step 3/3 : CMD echo "hello $name"
---> Running in feb1347b2526
Removing intermediate container feb1347b2526
---> 14db0c7b48c3
Successfully built 14db0c7b48c3
Successfully tagged xiaoming/centos-cmd-new:latest
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-cmd-new latest 14db0c7b48c3 15 seconds ago 202MB
xiaoming/centos-cmd latest 729b948e1319 3 minutes ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 33 minutes ago 202MB
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 40 minutes ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a 41 minutes ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da About an hour ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 About an hour ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 3 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 41 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker rum xiaoming/centos-cmd-new
docker: 'rum' is not a docker command.
See 'docker --help'
[root@zizhen02 cmd-run-entrypoint]# docker run xiaoming/centos-cmd-new
hello Docker
[root@zizhen02 cmd-run-entrypoint]#
Docker发布镜像
如何创建私有的镜像仓库;registry
https://hub.docker.com/_/registry
注意:如果push镜像到docker hub里
1,一定是docker ID + image名 eg:xiaoming163/centos7,否则会报错,权限问题
2,需要通过login进行登录
https://www.cnblogs.com/Tempted/p/7768694.html 案例,搭建私有仓库;
https://blog.csdn.net/ccboy2009/article/details/54908655 一些问题
[root@zizhen ~]# docker version
Client:
Version: 18.09.3
API version: 1.39
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:33:21 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.3
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:02:24 2019
OS/Arch: linux/amd64
Experimental: false
[root@zizhen ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@zizhen ~]# docker run -d -p 5000:5000 --restart always --name registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
c87736221ed0: Pull complete
1cc8e0bb44df: Pull complete
54d33bcb37f5: Pull complete
e8afc091c171: Pull complete
b4541f6d3db6: Pull complete
Digest: sha256:3b00e5438ebd8835bcfa7bf5246445a6b57b9a50473e89c02ecc8e575be3ebb5
Status: Downloaded newer image for registry:2
0113617c2cc2b479b3fce00747ff392a6ccac820167c59b21bfec5f96eed424c
[root@zizhen ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0113617c2cc2 registry:2 "/entrypoint.sh /etc…" 7 seconds ago Up 5 seconds 0.0.0.0:5000->5000/tcp registry
[root@zizhen ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry 2 f32a97de94e1 5 days ago 25.8MB
[root@zizhen ~]#
[root@zizhen02 ~]# telnet 192.168.1.108 5000
Trying 192.168.1.108...
Connected to 192.168.1.108.
Escape character is '^]'.
^Cq
Connection closed by foreign host.
[root@zizhen02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-cmd-new latest 14db0c7b48c3 About an hour ago 202MB
xiaoming/centos-cmd latest 729b948e1319 About an hour ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a 2 hours ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 2 hours ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 2 hours ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 4 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 42 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 ~]# cd
anaconda-ks.cfg .bash_profile cmd-run-entrypoint/ hello-world/ nginx-1.15.1.tar.gz .python_history .tcshrc
.bash_history .bashrc .cshrc my_env/ pip-8.0.2.tar.gz setuptools-19.6/ test_py_dir/
.bash_logout .cache/ docker-centos-vim/ .mysql_history .pki/ setuptools-19.6.tar.gz .viminfo
[root@zizhen02 ~]# cd cmd-run-entrypoint/
[root@zizhen02 cmd-run-entrypoint]# ls
Dockerfile
[root@zizhen02 cmd-run-entrypoint]# cat Dockerfile
FROM centos
ENV name Docker
CMD echo "hello $name"
[root@zizhen02 cmd-run-entrypoint]# docker build -t 192.168.1.108:5000/centos-cmd-shell .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : ENV name Docker
---> Using cache
---> e2827f0b9fb9
Step 3/3 : CMD echo "hello $name"
---> Using cache
---> 14db0c7b48c3
Successfully built 14db0c7b48c3
Successfully tagged 192.168.1.108:5000/centos-cmd-shell:latest
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.1.108:5000/centos-cmd-shell latest 14db0c7b48c3 About an hour ago 202MB
xiaoming/centos-cmd-new latest 14db0c7b48c3 About an hour ago 202MB
xiaoming/centos-cmd latest 729b948e1319 About an hour ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a 2 hours ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 2 hours ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 2 hours ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 4 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 42 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
push到私有仓库的时候,可能不可信任的需要:
[root@zizhen02 cmd-run-entrypoint]# docker push 192.168.1.108:5000/centos-cmd-shell
The push refers to repository [192.168.1.108:5000/centos-cmd-shell]
Get https://192.168.1.108:5000/v2/: http: server gave HTTP response to HTTPS client
[root@zizhen02 cmd-run-entrypoint]# ll /etc/docker/
total 4
-rw------- 1 root root 244 Mar 9 19:48 key.json
需要1:daemon.json
[root@zizhen02 cmd-run-entrypoint]# vim /etc/docker/daemon.json
[root@zizhen02 cmd-run-entrypoint]#
[root@zizhen02 cmd-run-entrypoint]# cat /etc/docker/daemon.json
{ "insecure-registries":["192.168.1.108:5000"] }
[root@zizhen02 cmd-run-entrypoint]# vim /lib/sys
sysctl.d/ systemd/
需要2:docker.service
[root@zizhen02 cmd-run-entrypoint]# vim /lib/systemd/system/docker.service
[root@zizhen02 cmd-run-entrypoint]# vim /lib/systemd/system/docker.service
[root@zizhen02 cmd-run-entrypoint]# cat /lib/systemd/system/docker.service |grep EnvironmentFile
EnvironmentFile=-/etc/docker/daemon.json
[root@zizhen02 cmd-run-entrypoint]# systemctl restart docker
Warning: docker.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@zizhen02 cmd-run-entrypoint]# systemctl deamon-reload
Unknown operation 'deamon-reload'.
[root@zizhen02 cmd-run-entrypoint]# systemctl deamon-reload docker
Unknown operation 'deamon-reload'.
[root@zizhen02 cmd-run-entrypoint]# systemctl deamon-reload
Unknown operation 'deamon-reload'.
[root@zizhen02 cmd-run-entrypoint]# systemctl daemon-reload
[root@zizhen02 cmd-run-entrypoint]# systemctl restart docker
[root@zizhen02 cmd-run-entrypoint]#
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.1.108:5000/centos-cmd-shell latest 14db0c7b48c3 About an hour ago 202MB
xiaoming/centos-cmd-new latest 14db0c7b48c3 About an hour ago 202MB
xiaoming/centos-cmd latest 729b948e1319 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a 2 hours ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 2 hours ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 2 hours ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 4 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 43 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker push 192.168.1.108:5000/centos-cmd-shell
The push refers to repository [192.168.1.108:5000/centos-cmd-shell]
071d8bd76517: Pushed
latest: digest: sha256:7ab3703c9df0d0a52ef31881eed62a70b72c8274ad15f7816466eadeba9f4679 size: 529
[root@zizhen02 cmd-run-entrypoint]#
API 查看
https://docs.docker.com/registry/spec/api/
另外一种是: 重新pull下image 是否正常;
[root@zizhen02 cmd-run-entrypoint]# docker rmi 192.168.1.108:5000/centos-cmd-shell
Untagged: 192.168.1.108:5000/centos-cmd-shell:latest
Untagged: 192.168.1.108:5000/centos-cmd-shell@sha256:7ab3703c9df0d0a52ef31881eed62a70b72c8274ad15f7816466eadeba9f4679
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-cmd-new latest 14db0c7b48c3 2 hours ago 202MB
xiaoming/centos-cmd latest 729b948e1319 2 hours ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker pull 192.168.1.108:5000/centos-cmd-shell
Using default tag: latest
latest: Pulling from centos-cmd-shell
Digest: sha256:7ab3703c9df0d0a52ef31881eed62a70b72c8274ad15f7816466eadeba9f4679
Status: Downloaded newer image for 192.168.1.108:5000/centos-cmd-shell:latest
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.1.108:5000/centos-cmd-shell latest 14db0c7b48c3 2 hours ago 202MB
xiaoming/centos-cmd-new latest 14db0c7b48c3 2 hours ago 202MB
xiaoming/centos-cmd latest 729b948e1319 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 2 hours ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker rmi 192.168.1.108:5000/centos-cmd-shell
Untagged: 192.168.1.108:5000/centos-cmd-shell:latest
Untagged: 192.168.1.108:5000/centos-cmd-shell@sha256:7ab3703c9df0d0a52ef31881eed62a70b72c8274ad15f7816466eadeba9f4679
[root@zizhen02 cmd-run-entrypoint]# docker rmi 14db0c7b48c3
Error response from daemon: conflict: unable to delete 14db0c7b48c3 (must be forced) - image is being used by stopped container 6dd27f5709d3
[root@zizhen02 cmd-run-entrypoint]# docker rmi xiaoming/centos-cmd-new
Error response from daemon: conflict: unable to remove repository reference "xiaoming/centos-cmd-new" (must force) - container 6dd27f5709d3 is using its referenced image 14db0c7b48c3
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-cmd-new latest 14db0c7b48c3 2 hours ago 202MB
xiaoming/centos-cmd latest 729b948e1319 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a 2 hours ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 3 hours ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 3 hours ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 5 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 43 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 cmd-run-entrypoint]# docker rmi xiaoming/centos-cmd-new
Error response from daemon: conflict: unable to remove repository reference "xiaoming/centos-cmd-new" (must force) - container 6dd27f5709d3 is using its referenced image 14db0c7b48c3
[root@zizhen02 cmd-run-entrypoint]# docker rmi 14db0c7b48c3
Error response from daemon: conflict: unable to delete 14db0c7b48c3 (must be forced) - image is being used by stopped container 6dd27f5709d3
[root@zizhen02 cmd-run-entrypoint]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@zizhen02 cmd-run-entrypoint]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dd27f5709d3 xiaoming/centos-cmd-new "/bin/sh -c 'echo \"h…" 2 hours ago Exited (0) 2 hours ago loving_shamir
91bf5eae2bbe xiaoming/centos-cmd "/bin/sh -c '\"echo $…" 2 hours ago Exited (127) 2 hours ago competent_dhawan
[root@zizhen02 cmd-run-entrypoint]# docker rm $(docker container ls -aq)
6dd27f5709d3
91bf5eae2bbe
[root@zizhen02 cmd-run-entrypoint]# docker rmi 14db0c7b48c3
Untagged: xiaoming/centos-cmd-new:latest
Deleted: sha256:14db0c7b48c3d3a0fbbef0e532c2f61dac83e882fd234fc6a05855c7ac4ead7c
[root@zizhen02 cmd-run-entrypoint]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming/centos-cmd latest 729b948e1319 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 2 hours ago 202MB
xiaoming/centos-entrypoint-exec-new1 latest 620ace42fe56 3 hours ago 202MB
xiaoming/centos-entrypoint-exec-new latest 6995428fbe6a 3 hours ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 3 hours ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 3 hours ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 5 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 43 hours ago 857kB
nginx latest 881bd08c0b08 8 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
Docker Dockfile的使用和实战
[root@zizhen02 flask-hello-world]# cat app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "hello docker"
if __name__=="__main__":
app.run()
[root@zizhen02 flask-hello-world]# vim Dockerfile
[root@zizhen02 flask-hello-world]# docker build -t xiaoming163/python2.7-flask-test .
Sending build context to Docker daemon 3.072kB
Step 1/7 : FROM python2.7
pull access denied for python2.7, repository does not exist or may require 'docker login'
[root@zizhen02 flask-hello-world]# vim Dockerfile
[root@zizhen02 flask-hello-world]# docker build -t xiaoming163/python2.7-flask-test .
Sending build context to Docker daemon 3.072kB
Step 1/7 : FROM python:2.7
---> 3be5dc25d0fa
Step 2/7 : LABEL maintainer="ming xiao<xiaoming@163.com>"
---> Running in 8c55e339c620
Removing intermediate container 8c55e339c620
---> 910f77d973f1
Step 3/7 : RUN pip install flask
---> Running in 7e344b244d4e
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting flask
Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting itsdangerous>=0.24 (from flask)
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10 (from flask)
Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting Werkzeug>=0.14 (from flask)
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Collecting click>=5.1 (from flask)
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)
Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.1.1 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0
Removing intermediate container 7e344b244d4e
---> f6ed6374dfc5
Step 4/7 : COPY app.py /app
---> 391e6ad83504
Step 5/7 : WORKDIR /app
Cannot mkdir: /app is not a directory
[root@zizhen02 flask-hello-world]# cat app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "hello docker"
if __name__=="__main__":
app.run()
[root@zizhen02 flask-hello-world]# ls
app.py Dockerfile
[root@zizhen02 flask-hello-world]# cat Dockerfile
FROM python:2.7
LABEL maintainer="ming xiao<xiaoming@163.com>"
RUN pip install flask
COPY app.py /app
WORKDIR /app
EXPOSE 5000
CMD ["python3","app.py"]
[root@zizhen02 flask-hello-world]# vim Dockerfile
[root@zizhen02 flask-hello-world]# cat Dockerfile
FROM python:2.7
LABEL maintainer="ming xiao<xiaoming@163.com>"
RUN pip install flask
COPY app.py /app/
WORKDIR /app
EXPOSE 5000
CMD ["python3","app.py"]
[root@zizhen02 flask-hello-world]#
[root@zizhen02 flask-hello-world]# docker run -it 391e6ad83504 /bin/bash
root@519ff3a0d16d:/# ll
bash: ll: command not found
root@519ff3a0d16d:/# ls
app boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
root@519ff3a0d16d:/# cd app
bash: cd: app: Not a directory
root@519ff3a0d16d:/# more app
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "hello docker"
if __name__=="__main__":
app.run()
root@519ff3a0d16d:/# exit
exit
[root@zizhen02 flask-hello-world]# docker container run -it 391e6ad83504 /bin/bash
root@e89fa57ccee5:/# exit
exit
[root@zizhen02 flask-hello-world]#
[root@zizhen02 flask-hello-world]# cat Dockerfile
FROM python:2.7
LABEL maintainer="ming xiao<xiaoming@163.com>"
RUN pip install flask
COPY app.py /app/
WORKDIR /app
EXPOSE 5000
CMD ["python3","app.py"]
[root@zizhen02 flask-hello-world]# docker build -t xiaoming163/python2.7-flask-test .
Sending build context to Docker daemon 3.072kB
Step 1/7 : FROM python:2.7
---> 3be5dc25d0fa
Step 2/7 : LABEL maintainer="ming xiao<xiaoming@163.com>"
---> Using cache
---> 910f77d973f1
Step 3/7 : RUN pip install flask
---> Using cache
---> f6ed6374dfc5
Step 4/7 : COPY app.py /app/
---> 4f32b3bd79a0
Step 5/7 : WORKDIR /app
---> Running in eb15ba3d22d8
Removing intermediate container eb15ba3d22d8
---> 186e722d814c
Step 6/7 : EXPOSE 5000
---> Running in 0fa80928b8d4
Removing intermediate container 0fa80928b8d4
---> 47702d2cea5e
Step 7/7 : CMD ["python3","app.py"]
---> Running in cbf1618c0dc2
Removing intermediate container cbf1618c0dc2
---> 4612c51c922f
Successfully built 4612c51c922f
Successfully tagged xiaoming163/python2.7-flask-test:latest
[root@zizhen02 flask-hello-world]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaoming163/python2.7-flask-test latest 4612c51c922f 9 seconds ago 918MB
<none> <none> 391e6ad83504 4 minutes ago 918MB
192.168.1.108:5000/centos-cmd-shell latest 14db0c7b48c3 21 hours ago 202MB
xiaoming/centos-cmd latest 729b948e1319 21 hours ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 22 hours ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 22 hours ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 22 hours ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 24 hours ago 335MB
xiaoli163/hello-world latest a821094d0306 2 days ago 857kB
python 2.7 3be5dc25d0fa 8 days ago 914MB
nginx latest 881bd08c0b08 9 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 flask-hello-world]#
Docker 容器的基本操作:
后台运行:-d
docker run -d b66a7f86bd45
交互运行:-it
docker run –it b66a7f86bd45 /bin/bash
在容器中运行命令:exec
#docker exec –it xxxx /bin/bash
查询容器:docker container ls / docker ps
停止容器: docker container stop [container-xxxid] / docker stop [container-xxxid]
停止容器再删除image;
清理退出的容器: #docker rm $(docker container ls -aq)
给容器命名,加个参数:--name=demo
#docker run -d --name=demo image-id
容器停止: docker container stop demo
容器启动: docker container start demo
容器列出详细信息:docker container inspect [container_ID]
容器产生的logs : docker container logs [container_ID]
[root@zizhen02 flask-hello-world]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f13f4458cec4 b66a7f86bd45 "python app.py" About an hour ago Up About an hour 5000/tcp objective_cartwright
[root@zizhen02 flask-hello-world]# docker container ls -aq
f13f4458cec4
[root@zizhen02 flask-hello-world]# docker exec -it f13f4458cec4 /bin/bash
root@f13f4458cec4:/app# ls
app.py
root@f13f4458cec4:/app# ps -ef |grep python
root 1 0 0 06:08 ? 00:00:01 python app.py
root 14 7 0 07:19 pts/0 00:00:00 grep python
root@f13f4458cec4:/app# exit
exit
[root@zizhen02 flask-hello-world]# docker exec -it f13f4458cec4 python
Python 2.7.16 (default, Mar 5 2019, 06:45:30)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
hello world
>>> quit()
[root@zizhen02 flask-hello-world]# docker exec -it f13f4458cec4 ifconfig
OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"ifconfig\": executable file not found in $PATH": unknown
[root@zizhen02 flask-hello-world]# docker exec -it f13f4458cec4 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
26: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@zizhen02 flask-hello-world]#
[root@zizhen02 flask-hello-world]# docker rm $(docker ps -aq)
ba02333f4c5a
f13f4458cec4
[root@zizhen02 flask-hello-world]# docker run -d --name=demo xiaoming163/python2.7-flask-hello
313809e69f5f903e6daac996831e94fea528ee36119c36d9de9c3cb444f30534
[root@zizhen02 flask-hello-world]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
313809e69f5f xiaoming163/python2.7-flask-hello "python app.py" 9 seconds ago Up 8 seconds 5000/tcp demo
[root@zizhen02 flask-hello-world]# docker container stop demo
demo
[root@zizhen02 flask-hello-world]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@zizhen02 flask-hello-world]# docker container start demo
demo
[root@zizhen02 flask-hello-world]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
313809e69f5f xiaoming163/python2.7-flask-hello "python app.py" 59 seconds ago Up 4 seconds 5000/tcp demo
[root@zizhen02 flask-hello-world]#
[root@zizhen02 flask-hello-world]# docker container
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
[root@zizhen02 flask-hello-world]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
313809e69f5f xiaoming163/python2.7-flask-hello "python app.py" 14 minutes ago Up 13 minutes 5000/tcp demo
[root@zizhen02 flask-hello-world]# docker container inspect 313809e69f5f
[
{
"Id": "313809e69f5f903e6daac996831e94fea528ee36119c36d9de9c3cb444f30534",
"Created": "2019-03-14T07:36:36.829581789Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11573,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-03-14T07:37:30.896808523Z",
"FinishedAt": "2019-03-14T07:37:15.839461201Z"
},
"Image": "sha256:b66a7f86bd458b31fcd966441e56520f6ad975b7da77e24aaa0123a3ecd5a7d0",
"ResolvConfPath": "/var/lib/docker/containers/313809e69f5f903e6daac996831e94fea528ee36119c36d9de9c3cb444f30534/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/313809e69f5f903e6daac996831e94fea528ee36119c36d9de9c3cb444f30534/hostname",
"HostsPath": "/var/lib/docker/containers/313809e69f5f903e6daac996831e94fea528ee36119c36d9de9c3cb444f30534/hosts",
"LogPath": "/var/lib/docker/containers/313809e69f5f903e6daac996831e94fea528ee36119c36d9de9c3cb444f30534/313809e69f5f903e6daac996831e94fea528ee36119c36d9de9c3cb444f30534-json.log",
"Name": "/demo",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/738cd0e242f1d5b3358024055993a89619053546a0d897fc8c4734cebd43517d-init/diff:/var/lib/docker/overlay2/77bdb7dcbbc744f87e8905faf4e06969205ed28484657ebce64a4691471adee9/diff:/var/lib/docker/overlay2/270c390be8b2155ae2203822458fa327b69d97bccd81712d240d4dc5984a54da/diff:/var/lib/docker/overlay2/d478e1adde3d55389f102641c0810641127c79acffd3d9459ba87f55b18eb57b/diff:/var/lib/docker/overlay2/4b6d381e65c63049c046e97e390b855373f32b3c9511db5f79b412577703d3ff/diff:/var/lib/docker/overlay2/b8b00eca54160cf69aae0447b71d91f73219c3ac622dcaae255303f30f4cffbd/diff:/var/lib/docker/overlay2/dcd89ffff3cfc083f629a91e5a9ba430b60bc0e8d5d3f98c868884717b1f74ee/diff:/var/lib/docker/overlay2/bd5a26769e89f73ca51de5ee37311926729c47502fe6831495d2201d50046fb6/diff:/var/lib/docker/overlay2/c5dfdd42c858905178174d62204b9138fa0ba6a6892ce15092af4a7de3ced61d/diff:/var/lib/docker/overlay2/13ae3b272b14e5d30b40f2f03e35ed9670ab6a7ce7a3426215e145ca27370039/diff:/var/lib/docker/overlay2/cccc19db2ee03027051ab443f50b8f2bc50558a73980b955208d54319320dcd9/diff:/var/lib/docker/overlay2/f3d348923e408cf3bbf13207e58d819bdd2935f17c2b950f6dafd6e5b6750a52/diff",
"MergedDir": "/var/lib/docker/overlay2/738cd0e242f1d5b3358024055993a89619053546a0d897fc8c4734cebd43517d/merged",
"UpperDir": "/var/lib/docker/overlay2/738cd0e242f1d5b3358024055993a89619053546a0d897fc8c4734cebd43517d/diff",
"WorkDir": "/var/lib/docker/overlay2/738cd0e242f1d5b3358024055993a89619053546a0d897fc8c4734cebd43517d/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "313809e69f5f",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=C.UTF-8",
"PYTHONIOENCODING=UTF-8",
"GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF",
"PYTHON_VERSION=2.7.16",
"PYTHON_PIP_VERSION=19.0.3"
],
"Cmd": [
"python",
"app.py"
],
"ArgsEscaped": true,
"Image": "xiaoming163/python2.7-flask-hello",
"Volumes": null,
"WorkingDir": "/app",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "ming xiao<xiaoming@163.com>"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "6ed035e2ba1dc3f6ef0b68e0b9d1e93869578fe358fcaa9ab112d16e7846121d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5000/tcp": null
},
"SandboxKey": "/var/run/docker/netns/6ed035e2ba1d",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "c5d352849802244b00f695556836dc9eea14299764fe49a887b49559cdedf1e6",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "9d3506f3c471941f9a9fdbc445314a4ec770b30955b90b157f026e04480c366b",
"EndpointID": "c5d352849802244b00f695556836dc9eea14299764fe49a887b49559cdedf1e6",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
[root@zizhen02 flask-hello-world]#
[root@zizhen02 flask-hello-world]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
313809e69f5f xiaoming163/python2.7-flask-hello "python app.py" 21 minutes ago Up 20 minutes 5000/tcp demo
[root@zizhen02 flask-hello-world]# docker container logs 313809e69f5f
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[root@zizhen02 flask-hello-world]#
Docker 的 Dockerfile实战2:
压力测试
#docker run –it ubuntu
#apt-get update && apt-get -install -y stress
[root@zizhen02 ~]# mkdir ubuntu-stress
[root@zizhen02 ~]# cd ubuntu-stress/
[root@zizhen02 ubuntu-stress]# vim Dockerfile
[root@zizhen02 ubuntu-stress]# cat D`
> ^C
[root@zizhen02 ubuntu-stress]# cat Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y stress
ENTRYPOINT ["/usr/bin/stress"]
CMD []
[root@zizhen02 ubuntu-stress]# docker build -t ubuntu-stress .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM ubuntu
---> 94e814e2efa8
Step 2/4 : RUN apt-get update && apt-get install -y stress
---> Running in 96d56740c24b
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [158 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [5436 B]
Get:5 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [359 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [3910 B]
Get:8 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [10.8 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6965 B]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [950 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [717 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3659 B]
Fetched 15.6 MB in 45s (344 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
stress
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 17.5 kB of archives.
After this operation, 46.1 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 stress amd64 1.0.4-2 [17.5 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 17.5 kB in 1s (12.6 kB/s)
Selecting previously unselected package stress.
(Reading database ... 4039 files and directories currently installed.)
Preparing to unpack .../stress_1.0.4-2_amd64.deb ...
Unpacking stress (1.0.4-2) ...
Setting up stress (1.0.4-2) ...
Removing intermediate container 96d56740c24b
---> a80323110bb7
Step 3/4 : ENTRYPOINT ["/usr/bin/stress"]
---> Running in 67d668bf1352
Removing intermediate container 67d668bf1352
---> a2ecf48d7876
Step 4/4 : CMD []
---> Running in 0258bcff1082
Removing intermediate container 0258bcff1082
---> aee6e531355c
Successfully built aee6e531355c
Successfully tagged ubuntu-stress:latest
[root@zizhen02 ubuntu-stress]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-stress latest aee6e531355c 42 seconds ago 114MB
xiaoming163/python2.7-flask-hello latest b66a7f86bd45 5 hours ago 918MB
xiaoming163/python2.7-flask-test latest 4612c51c922f 5 hours ago 918MB
<none> <none> 391e6ad83504 5 hours ago 918MB
192.168.1.108:5000/centos-cmd-shell latest 14db0c7b48c3 26 hours ago 202MB
xiaoming/centos-cmd latest 729b948e1319 26 hours ago 202MB
xiaoming/centos-entrypoint-exec-new2 latest 249a9c95d9be 27 hours ago 202MB
xiaoming/centos-entrypoint-exec latest b2137f7cf5da 27 hours ago 202MB
xiaoming/centos-entrypoint-shell latest 344ec38e6b59 27 hours ago 202MB
xiaoming163/centos-vim-new latest f9f19d2bc847 29 hours ago 335MB
ubuntu latest 94e814e2efa8 2 days ago 88.9MB
xiaoli163/hello-world latest a821094d0306 2 days ago 857kB
python 2.7 3be5dc25d0fa 9 days ago 914MB
nginx latest 881bd08c0b08 9 days ago 109MB
ubuntu 14.04 5dbc3f318ea5 7 weeks ago 188MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@zizhen02 ubuntu-stress]#
[root@zizhen02 ubuntu-stress]# docker run -it ubuntu-stress
`stress' imposes certain types of compute stress on your system
Usage: stress [OPTION [ARG]] ...
-?, --help show this help statement
--version show version statement
-v, --verbose be verbose
-q, --quiet be quiet
-n, --dry-run show what would have been done
-t, --timeout N timeout after N seconds
--backoff N wait factor of N microseconds before work starts
-c, --cpu N spawn N workers spinning on sqrt()
-i, --io N spawn N workers spinning on sync()
-m, --vm N spawn N workers spinning on malloc()/free()
--vm-bytes B malloc B bytes per vm worker (default is 256MB)
--vm-stride B touch a byte every B bytes (default is 4096)
--vm-hang N sleep N secs before free (default none, 0 is inf)
--vm-keep redirty memory instead of freeing and reallocating
-d, --hdd N spawn N workers spinning on write()/unlink()
--hdd-bytes B write B bytes per hdd worker (default is 1GB)
Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s
Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size).
[root@zizhen02 ubuntu-stress]# docker run -it ubuntu-stress --vm 1
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
^Cstress: FAIL: [1] (415) <-- worker 6 got signal 2
stress: WARN: [1] (417) now reaping child worker processes
stress: FAIL: [1] (421) kill error: No such process
stress: FAIL: [1] (451) failed run completed in 88s
[root@zizhen02 ubuntu-stress]# docker run -it ubuntu-stress --vm 1 --verbose
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [6] forked
stress: dbug: [6] allocating 268435456 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 268435456 bytes
stress: dbug: [6] allocating 268435456 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 268435456 bytes
docker 资源限制Control groups
内存、CPU限制
--memory 和--memory-sawp 如果不限制--memory-sawp 的大小,那么--memory-sawp 的大小和--memory一样大小。
#docker run --memory=200M xiaoming163/ubuntu-stress --vm 1 --verbose #内存400M(--memory和--memory-sawp 一共大小 )
起一个500M的内存,不够,容器退出
#docker run --memory=200M xiaoming163/ubuntu-stress --vm 1 --verbose --vm-bytes 500M
CPU权重
#docker run --cpu-shares=10 xiaoming163/ubuntu-stress --cpu 1
#docker run --cpu-shares=5 xiaoming163/ubuntu-stress --cpu 1
权重越大,cpu占用资源的百分比越大