docker入门(一)
docker安装
yum install -y docker-io
[root@centos ~]# yum install -y docker-io
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker.x86_64.0.1.10.3-46.el7.centos.14 将被 安装
--> 正在处理依赖关系 docker-common = 1.10.3-46.el7.centos.14,它被软件包
.....
docker版本查询
docker version
Client:
Version: 1.10.3
API version: 1.22
Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
Go version: go1.6.3
Git commit: cb079f6-unsupported
Built: Fri Sep 16 13:24:25 2016
OS/Arch: linux/amd64
Server:
Version: 1.10.3
API version: 1.22
Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
Go version: go1.6.3
Git commit: cb079f6-unsupported
Built: Fri Sep 16 13:24:25 2016
OS/Arch: linux/amd64
启动docker
旧式语法
service docker start
centos7新式语法
systemctl start docker.service
配置开机启动
旧式语法
chkconfig docker on
centos7新式语法
systemctl enable docker.service
centos7 启动docker失败--selinux-enabled=false(时间2018-07-13)
当前新版本为1.13,默认安装好后启动失败。
如不想做过多设置,可以关闭selinux-enabled=false,就可以正常启动。
重新编辑docker配置文件:
vi /etc/sysconfig/docker
找到以下这行
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
将--selinux-enabled改为--selinux-enabled=false
配置加速器(阿里云)
阿里云的介绍:
配置Docker加速器
您可以使用如下的脚本将mirror的配置添加到docker daemon的启动参数中。
系统要求 CentOS 7 以上,Docker 1.9 以上。 (其它linux请详见阿里云介绍)
sudo cp -n /lib/systemd/system/docker.service /etc/systemd/system/docker.service
sudo sed -i "s|ExecStart=/usr/bin/docker daemon|ExecStart=/usr/bin/docker daemon --registry-mirror=https://ir6ei990.mirror.aliyuncs.com|g" /etc/systemd/system/docker.service
sudo systemctl daemon-reload
sudo service docker restart
执行后不起作用,也许因为docker版本不一致,本人是手动修改成功的
找到这行
ExecStart=/usr/bin/docker-current daemon \
将自己的加速地址加进去,最后修改为
ExecStart=/usr/bin/docker-current daemon --registry-mirror=https://ir6ei990.mirror.aliyuncs.com \
然后重启docker
systemctl daemon-reload
systemctl restart docker.service
下载镜像(测试用,找个小的镜像)
docker pull busybox
Using default tag: latest
Trying to pull repository docker.io/library/busybox ...
latest: Pulling from docker.io/library/busybox
56bec22e3559: Pull complete
Digest: sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912
Status: Downloaded newer image for docker.io/busybox:latest
查看已下载的镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest e02e811dd08f 4 weeks ago 1.093 MB
这就是刚才上面下载的busybox
启动镜像变为容器
docker run -d -it --name testbusybox busybox
44391ec69021707afbeba0c1dda7633c4b0507838fecdf37ae3ad56182754710
-d 后台启动
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
进行交互式操作(例如Shell脚本),那我们必须使用-i -t参数同容器进行数据交互。但是当通过管道同容器进行交互时,就不需要使用-t参数
--name 容器名称(方便后续操作)
查看已经启动的容器(镜像)
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d713216033b busybox "sh" 5 minutes ago Up 5 minutes testbusybox
进入运行中的容器
刚才启动的容器为后台运行。我们可以通过一下命令进入启动的容器中
docker exec -it testbusybox /bin/sh
/ # pwd
/
/ # ls
bin dev etc home proc root run sys tmp usr var
退出容器
exit
/ #
/ # exit
[root@centos ~]#
重启or停止容器运行
docker restart testbusybox
[root@centos ~]# docker restart testbusybox
testbusybox
docker stop testbusybox
[root@centos ~]# docker stop testbusybox
testbusybox
[root@centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
发现容器关闭后docker ps 命令看不到了,默认显示启动的容器,我们可以通过加-a参数来显示停止的docker容器
[root@centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d713216033b busybox "sh" 22 hours ago Exited (137) About a minute ago testbusybox
44391ec69021 busybox "sh" 22 hours ago Exited (137) 22 hours ago trusting_wright
2810cbbefdbe busybox "sh" 22 hours ago Exited (0) 22 hours ago drunk_bell
ddf3b42c2970 c9bd19d022f6 "/entrypoint.sh /etc/" 2 days ago Exited (2) 2 days ago stoic_brattain
这些容器都是实际存在的。
以及启动成容器的镜像,无法再次run。除非删除容器后再run。
我们拿windows的ghost打比方:
镜像就是ghost光盘,容器就是已经装入电脑后的系统,ghost光盘可以为一台机器安装无限次,但是docker有点区别,已经装了A镜像的电脑,无法再次安装A的镜像。除非
1:删除已安装的容器,再次run A镜像进行安装。
2:换一个容器名字
[root@centos ~]# docker run -d -it --name testbusybox busybox
docker: Error response from daemon: Conflict. The name "/testbusybox" is already in use by container 8d713216033bd01610aa444647ce971e9d47786c81637e12b5a85659d0006b63. You have to remove (or rename) that container to be able to reuse that name..
See '/usr/bin/docker-current run --help'.
换了容器名后启动成功
[root@centos ~]# docker run -d -it --name testbusybox1 busybox
3ec4539e98a82d07fa362c7d90ff1a27547130a642662a1b8904742ce4ed2419
删除容器
docker rm -f testbusybox1
[root@centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ec4539e98a8 busybox "sh" 57 seconds ago Up 56 seconds testbusybox1
8d713216033b busybox "sh" 22 hours ago Exited (137) 11 minutes ago testbusybox
44391ec69021 busybox "sh" 22 hours ago Exited (137) 22 hours ago trusting_wright
2810cbbefdbe busybox "sh" 22 hours ago Exited (0) 22 hours ago drunk_bell
ddf3b42c2970 c9bd19d022f6 "/entrypoint.sh /etc/" 2 days ago Exited (2) 2 days ago stoic_brattain
[root@centos ~]#
[root@centos ~]# docker rm -f testbusybox1
testbusybox1
[root@centos ~]# docker rm -f testbusybox
testbusybox
[root@centos ~]# docker rm -f trusting_wright
trusting_wright
[root@centos ~]# docker rm -f drunk_bell
drunk_bell
[root@centos ~]# docker rm -f stoic_brattain
stoic_brattain
[root@centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
修改镜像tag
docker tag busybox registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
docker images
docker.io/busybox latest e02e811dd08f 4 weeks ago 1.093 MB
registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox 1.0.0 e02e811dd08f 4 weeks ago 1.093 MB
将busybox修改为
registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
推送到阿里云仓库
前面已经修改了对应的阿里云仓库名/镜像名:Tag
推送的时候就会发送到registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox这个仓库中去。
具体阿里云docker镜像仓库可以自行查阅阿里云docker的帮助文档,很简单的。
发送前需要登录阿里云docker仓库
docker login --username=wzgdxg registry.cn-hangzhou.aliyuncs.com
Password:
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
推送
docker push registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox]
e88b3f82283b: Pushed
1.0.0: digest: sha256:9393222c6789842b16bcf7306b6eb4b486d81a48d3b8b8f206589b5d1d5a6101 size: 505
删除镜像
docker rmi -f registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
Untagged: registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
[root@centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest e02e811dd08f 4 weeks ago 1.093 MB
从阿里云docker仓库拉取镜像(刚才推送上去的镜像)
docker pull registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
Trying to pull repository registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox ...
1.0.0: Pulling from registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox
Digest: sha256:9393222c6789842b16bcf7306b6eb4b486d81a48d3b8b8f206589b5d1d5a6101
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
[root@centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest e02e811dd08f 4 weeks ago 1.093 MB
registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox 1.0.0 e02e811dd08f 4 weeks ago 1.093 MB