设为首页 收藏本站
查看: 1023|回复: 0

[经验分享] Docker学习与实践 Ⅰ

[复制链接]

尚未签到

发表于 2019-2-21 12:18:21 | 显示全部楼层 |阅读模式
一、docker的安装

1.依赖包安装

yum install -y yum-utils device-mapper-persistent-data lvm2
2.添加yum源

yum-config-manager \
--add-repo \
https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-edge
#配置为安装最新版Docker CE
3.安装docker

yum install docker-ce
  *官方为了简化安装流程提供了便捷的安装脚本

curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh --mirror Aliyun
4.启动docker

systemctl enable docker
systemctl start docker
5.建立docker用户和组

6.测试docker是否安装完成

[docker@dockertest ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
7.配置镜像加速器并重启服务

cat /etc/docker/daemon.json
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
systemctl daemon-reload
systemctl restart docker
二、使用镜像

1.获取镜像

[root@dockertest ~]# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
22dc81ace0ea: Pull complete
1a8b3c87dba3: Pull complete
91390a1c435a: Pull complete
07844b14977e: Pull complete
b78396653dae: Pull complete
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:16.04
2.查看获取的镜像

[root@dockertest ~]# docker image ls
  #使用--help可以查看参数选项,比如自己定义显示的列

[root@dockertest ~]# docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID            REPOSITORY          TAG
f975c5035748        ubuntu              16.04
f2a91732366c        hello-world         latest

  #查看空间的占用

[root@dockertest ~]# docker system df
3.删除镜像
  #删除本地镜像可以使用镜像短ID、长ID、镜像名或镜像摘要来删除

[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        7 days ago          112MB
centos              latest              2d194b392dd1        8 days ago          195MB
mysql               latest              5d4d51c57ea8        2 weeks ago         374MB
nginx               latest              e548f1a579cf        3 weeks ago         109MB
hello-world         latest              f2a91732366c        3 months ago        1.85kB
[root@dockertest ~]# docker image rm 5d4
[root@dockertest ~]# docker image rm nginx
[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        7 days ago          112MB
centos              latest              2d194b392dd1        8 days ago          195MB
hello-world         latest              f2a91732366c        3 months ago        1.85kB
  #还可以使用docker image ls命令来配合

docker image rm $(docker image ls -q ubuntu)
docker image rm $(docker image ls -q -f before=centos)

三、容器的操作

1.容器的启动
  ①新建并启动

[root@dockertest ~]# docker run ubuntu:16.04 /bin/echo "hello world"
hello world
  ②启动一个bash终端,允许用户进行交互。

[root@dockertest ~]# docker run -ti ubuntu:16.04 /bin/bash
root@edf8af896cc0:/# pwd
/
#选项 '-t' 开启一个伪终端,'-i' 让容器的标准输入保持打开。
  ③后台运行

[root@dockertest ~]# docker run -d ubuntu:16.04 /bin/sh -c "while true; do echo hello world; sleep 10; done"
cab4112e91fb0d6eae3762ec2a07b13798e908503a8f58799ac50fd9b4aa7e37
  #此时容器会在后台运行并不会把输出的结果打印到宿主机上面,可以使用docker logs查看输出信息。

[root@dockertest ~]# docker logs cab41
hello world
hello world
hello world
hello world
2.容器的终止

[root@dockertest ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cab4112e91fb        ubuntu:16.04        "/bin/sh -c 'while t…"   5 minutes ago       Up 5 minutes                     
[root@dockertest ~]# docker container stop cab41
cab41

  #查看终止的容器

[root@dockertest ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
cab4112e91fb        ubuntu:16.04        "/bin/sh -c 'while t…"   7 minutes ago       Exited (137) About a minute ago                       angry_wescoff
  #可以使用docker container start来启动处于终止状态的容器

[root@dockertest ~]# docker container start cab41
cab41
[root@dockertest ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cab4112e91fb        ubuntu:16.04        "/bin/sh -c 'while t…"   10 minutes ago      Up 5 seconds                            angry_wescoff
[root@dockertest ~]# docker attach 4417
root@4417b64c1c7f:/# pwd
/
root@4417b64c1c7f:/# exit
exit
3.进入容器
  ①attack(输入exit会导致容器的终止)

[root@dockertest ~]# docker run -itd ubuntu
4417b64c1c7f5c59bc446467e6d3c033ebbe6412f6e51fa722e463ff2b41a3a9
[root@dockertest ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4417b64c1c7f        ubuntu              "/bin/bash"         8 seconds ago       Up 7 seconds                            hungry_easley
  ②exec(输入exit不会导致容器的终止)

[root@dockertest ~]# docker run -itd ubuntu
5276352cf8d11ec07c12d07adfa209453299822aae779ac9ff2f401e7ff1fcb1
[root@dockertest ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5276352cf8d1        ubuntu              "/bin/bash"         8 seconds ago       Up 8 seconds                            blissful_edison
[root@dockertest ~]# docker exec -it 5276 bash
root@5276352cf8d1:/# whoami
root
root@5276352cf8d1:/# exit
exit
#也可以使用组合键退出,仍然保持容器运行,我们可以随时回来到这个bash中来,组合键是 Ctrl-p Ctrl-q,先同时按下Ctrl和p,再按Ctrl和q。就可以退出到我们的宿主机了。
4.容器的导出和导入
  ①导出

[root@dockertest ~]# docker export 4417 > test.tar
[root@dockertest ~]# ls
test.tar
  ②导入

[root@dockertest ~]# cat test.tar | docker import - test/ubuntu:v1.1
sha256:fb0e8d9dcaba2d018ec1fe26394f3b4989db55ace695d1c685c5e753fbe8ed9e
[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.1                fb0e8d9dcaba        3 seconds ago       85.8MB
  #也可以通过指定URL或者某个目录来导入

[root@dockertest ~]# docker import /root/test.tar test/ubuntu:v1.2
sha256:e33803dd759481e27cd807536f28fe8ab0cfddd9c3bae2a15b95ea8b7645172b
[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.2                e33803dd7594        3 seconds ago       85.8MB
test/ubuntu         v1.1                fb0e8d9dcaba        3 minutes ago       85.8MB

5.删除容器
  ①删除一个处于终止状态的容器

[root@dockertest ~]# docker container rm edf8af
edf8af
#如果要删除一个运行中的容器,可以添加 -f 参数来删除。
  ②清除所有处于终止状态的容器

[root@dockertest ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
4417b64c1c7f5c59bc446467e6d3c033ebbe6412f6e51fa722e463ff2b41a3a9
5276352cf8d11ec07c12d07adfa209453299822aae779ac9ff2f401e7ff1fcb1
99aee81f7684d59c72d2c1bbc933fd052f2158e833fb08988803108de613de24
cab4112e91fb0d6eae3762ec2a07b13798e908503a8f58799ac50fd9b4aa7e37
  #学习文档地址:https://github.com/yeasy/docker_practice/blob/master/SUMMARY.md





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-675346-1-1.html 上篇帖子: Docker简介以及使用领域和架构 下篇帖子: 阿里云docker
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表