Docker In Action:从入门到放弃笔记

本文是《Docker实战》笔记,涵盖了Docker的基础概念、安装、镜像和容器管理、网络配置、资源隔离及自动化构建等方面。通过案例介绍了如何在容器中运行软件、持久化存储、构建与分发镜像,揭示了Docker在软件部署和管理中的优势。
摘要由CSDN通过智能技术生成

Part 1

章节一 欢迎来到docker

安全:想安装某个软件,但又担心中病毒?别担心,有docker。
省时:上班忙,但又担心下班没时间照顾老婆孩子?别担心,有docker。
分发应用:制作了一个很棒的软件,但又担心用户安装起来麻烦?别担心,有docker。
简单:系统庞大,难以管理?别担心,有docker。

docker的权威解释(垃圾翻译)

Docker是一个命令行程序,一个后台守护进程,并且提供了一系列的远程服务–解决通用问题、简化安装、运行、发布和移除软件。Docker使用了UNIX技术–容器(Containers)。

docker的特点(划重点,突然觉得事情变得更加有趣了)

  1. 是一个容器,
  2. 和虚拟机的本质区别是:虚拟机需要依靠硬件虚拟化,而docker直接依赖主机的linux内核,没有其他添加,保证原汁原味。
  3. 隔离应用。开启docker,实际上开启了docker cli和docker daemon,每个容器都作为docker daemon的子进程,每个容器里面有独有的内存空间和资源。
  4. 集装箱。把容器比作集装箱。镜像(image)是docker中可装载的单元。registries和indexes用来简化镜像的分发。(不明觉厉)

为什么使用docker?
想象一下,一个软件,你要考虑这个软件在哪个平台运行,需要哪些依赖软件或资源,怎么卸载,怎么保证安全?

  1. 便于管理软件。如果不使用docker,软件越多,需要引用的依赖就越复杂,整个软件系统一片混乱。使用docker,我们将软件放在一个个的容器里,互不干扰。
  2. 移植性。软件要在不同平台运行。(说的我很想用docker啊,这正是我想要的)
  3. 安全性。保护计算机不受恶意软件的攻击,反正软件在容器里面,你病毒再怎么强大,也跑不出来。

为什么docker这么重要?

  1. docker提供了抽象的特性。我们要安装一个软件,我们只需要告诉docker我们要安装这个软件,其他的事情docker会帮我们解决。
  2. docker为各大大公司所使用和维护,比如谷歌、微软、亚马逊,你还用担心docker不好用?
  3. docker之于PC,乃app store之于移动端。

什么时候什么地点使用docker
很不幸,docker只能用在linux系统软件。。

案例:Helloworld

  1. 注册docker,安装docker win客户端,貌似需要重启两次,第二次docker会为我们开启Hyper-v(坑,开启Hyper-v后不能使用vmware,也就是说,docker和vmware你只能二选一,所以我还是把docker装虚拟机吧。)
  2. 打开cmd,运行
docker run dockerinaction/hello_world

docker run的运行流程:先在本地找镜像,如果没有,那么在docker hub(docker提供的公用注册中心)上找,如果有,下载安装,创建容器运行程序。

注意:因为镜像地址在国外,所以可以使用国内的镜像资源。这里我选择阿里云的容器镜像服务,百度搜索即可。在镜像中心-镜像加速器中找到加速器地址。
win版docker配置:右下角找到docker客户端-settings-daemon-basis✔

{
   
  "registry-mirrors": ["https://dh3y1ni0.mirror.aliyuncs.com"],
  "insecure-registries": [],
  "debug": true,
  "experimental": false
}

附:docker linux环境(来自官网,一切都很顺利执行下来)

# 卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
# 更新apt-get
sudo apt-get update
# 允许apt获取https的资源
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
# 添加docker的key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 验证是否有key
sudo apt-key fingerprint 0EBFCD88
# 添加稳定的资源
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# 安装docker ce
sudo apt-get install docker-ce docker-ce-cli containerd.io
# 验证
sudo docker run hello-world

章节二 在容器中跑软件

获取docker的命令

# 列出所有顶层命令
docker help
# 列出某个命令的用法:如 docker cp --help
docker 命令名 --help

案例:创建一个监控网站

  1. 安装并启动nginx镜像
docker run --detach --name web nginx:latest

安装成功后生成一串唯一标识符:a60d96d03c2f23a1fa9cacea194c42f64506c69bf5ecf06a3eff3bd254e11329

detach的意思是启动一个后台程序,也叫做守护进程daemon。

  1. 安装并启动mail镜像
# -d是--detach的缩写
docker run -d --name mailer dockerinaction/ch2_mailer
  1. 安装并启动交互程序
# interactive表示启动一个交互程序,支持标准输入流stdin
# tty表示分配一个虚拟终端
# link表示和nginx联系起来
docker run --interactive --tty --link web:web --name web_test busybox  /bin/sh

交互程序开启了一个unix shell,跟nginx联系
测试是否连通nginx:

wget -O - http://web:80/

ctrl+p+q:终端后台挂起

  1. 安装并启动监听代理
docker run -it --name agent --link web:insideweb --link mailer:insidemailer dockerinaction/ch2_agent

代理监听的作用是监听nginx服务器,如果挂掉,那么通过mailer发送消息。

  1. 测试

5.1 查看容器状态

docker ps

5.2 重启容器

docker restart nginx

刚才启动了nginx,但是由于重启了电脑,导致docker ps找不到nginx的记录,可以通过restart命令重启nginx。

5.3 查看日志

# --follow 或 -f
docker logs --follow web

由于代理会一直测试nginx的状态,所以nginx会产生日志,从而可以判断nginx是否运行。但是这样会导致日志越来越大,第四章我们会看到volumes的使用。

5.4 关闭容器

docker stop web

关闭nginx时,代理监听到变化,调用mailer发送消息,同时mailer中也存有该日志

docker logs mailer

PID命名空间

  1. PID是linux中用来唯一标识进程的,PID命名空间是一组PID的集合
docker run -d --name namespaceA busybox /bin/sh -c "sleep 30000"
docker run -d --name namespaceB busybox /bin/sh -c "nc -l -p 0.0.0.0:80"

docker exec namespaceA ps
docker exec namespaceB ps
# 输出
PID   USER     TIME  COMMAND
    1 root      0:00 sleep 30000
    6 root      0:00 ps
  1. 当然可以不使用命名空间
docker run --pid host busy ps
  1. 如果一个容器已经占用一个应用程序的资源,那么另一个容器使用这个应用程序会访问不到资源。
docker run -d --name webConflict nginx:latest
docker logs webConflict

上面开启了另一个nginx,但是获取不到日志内容。原因是nginx的资源已经被web占用了。

docker exec webConflict nginx -g 'daemon off;'

执行上面命令,会提示端口占用错误。

排除冲突
假设你要创建多个nginx实例,如果容器名称一样,会冲突,最简单的办法是使用id去标识,我

Docker-in-Action.pdf In 2011, I started working at Amazon.com. In that first week my life was changed as I learned how to use their internal build, dependency modeling, and deployment tool- ing. This was the kind of automated management I had always known was possible but had never seen. I was coming from a team that would deploy quarterly and take 10 hours to do so. At Amazon I was watching rolling deployments push changes I had made earlier that day to hundreds of machines spread all over the globe. If big tech firms had an engineering advantage over the rest of the corporate landscape, this was it. Early in 2013, I wanted to work with Graphite (a metrics collection and graphing suite). One day I sat down to install the software and start integrating a personal proj- ect. At this point I had several years of experience working with open source applica- tions, but few were as dependent on such large swaths of the Python ecosystem. The installation instructions were long and murky. Over the next several hours, I discov- ered many undocumented installation steps. These were things that might have been more obvious to a person with deeper Python ecosystem knowledge. After pouring over several installation guides, reading through configuration files, and fighting an epic battle through the deepest parts of dependency hell, I threw in the towel. Those had been some of the least inspiring hours of my life. I wanted nothing to do with the project. To make matters worse, I had altered my environment in a way that was incompatible with other software that I use regularly. Reverting those changes took an embarrassingly long time. I distinctly remember sitting at my desk one day in May that year. I was between tasks when I decided to check Hacker News for new ways to grow my skillset. Articles about a technology called Docker had made the front page a few times that week. That evening I decided to check it out. I hit the site and had the software installed within a few minutes. I was running Ubuntu on my desktop at home, and Docker only had two dependencies: LXC and the Linux kernel itself. Licensed to Stephanie Bernal <nordicka.n@gmail.com> PREFACE xiv Like everyone else, I kicked the tires with a “Hello, World” example, but learned little. Next I fired up Memcached. It was downloaded and running in under a minute. Then I started WordPress, which came bundled with its own M y SQL server. I pulled a couple different Java images, and then Python images. Then my mind flashed back to that terrible day with Graphite. I popped over to the Docker Index (this was before Docker Hub) and did a quick search. The results came back, and there it was. Some random user had created a Graphite image. I pulled it down and created a new container. It was running. A simple but fully configured Graphite server was running on my machine. I had accomplished in less than a minute of download time what I had failed to do with several hours a few months earlier. Docker was able to demonstrate value with the simplest of examples and minimum effort. I was sold. Over the next week, I tried the patience of a close friend by struggling to direct our conversations toward Docker and containers. I explained how package management was nice, but enforcing file system isolation as a default solved several management problems. I rattled on about resource efficiency and provisioning latency. I repeated this conversation with several other colleagues and fumbled through the container story. Everyone had the same set of tired questions, “Oh, it’s like virtualization?” and “Why do I need this if I have virtual machines?” The more questions people asked, the more I wanted to know. Based on the popularity of the project, this is a story shared by many. I began including sessions about Docker when I spoke publicly. In 2013 and 2014, only a few people had heard of Docker, and even fewer had actually tried the software. For the most part, the crowds consisted of a few skeptical system administrator types and a substantial number of excited developers. People reacted in a multitude of ways. Some were pure rejectionists who clearly preferred the status quo. Others could see problems that they experienced daily solved in a matter of moments. Those peo- ple reacted with an excitement similar to mine. In the summer of 2014, an associate publisher with Manning called me to talk about Docker. After a bit more than an hour on the phone he asked me if there was enough content there for a book. I suggested that there was enough for a few books. He asked me if I was interested in writing it, and I became more excited than I had been for some time. That fall I left Amazon.com and started work on Docker in Action. Today, I'm sitting in front of the finished manuscript. My goal in writing this book was to create something that would help people of mixed backgrounds get up to speed on Docker as quickly as possible, but in such a way that they understand the underlying mechanisms. The hope is that with that knowledge, readers can under- stand how Docker has been applied to certain problems, and how they might apply it in their own use-cases.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值