docker in action 笔记之第二章(在容器中运行软件)

  1. 学习要点归纳:

    • 使用容器执行互动和后台终端程序
    • 容器和PID命名空间
    • 容器配置和输出
    • 容器中运行多个程序
    • 注入配置到容器
    • 持久化容器和容器的生命周期
    • 容器的清理
  2. 环境准备

    • win10 + 虚拟机(centos7)
    • docker运行在虚拟机的centos7系统上
    • 下载docker :yum install docker
    • 修改默认镜像仓库地址为国内地址,防止docker pull镜像时报错:
      • vim /etc/docker/daemon.json
      • { "registry-mirrors": ["https://registry.docker-cn.com"] }
      • systemctl daemon-reload
      • systemctl restart docker
  3. 笔记

    • 建立一个网站的监控器(熟悉常用命令)
      • docker run -d --name web nginx
      • docker run -d --name mailer goinaction/ch2_mailer
      • docker run -it --link web:web -name web_test busybox /bin/sh wget -O http://web:80
      • docker run -it --name agent --link web:insideweb --link mailer:insidemailer dockerinaction/ch2_agent
      • -d以守护模式运行容器,-it以shell模式运行容器
      • -it可以使用exit退出shell,并停止容器;也可以使用ctrl P + ctrl Q的方式只退出shell,不停止容器。
      • docker restart web
      • docker restart agent
      • docker restart mailer
      • docker logs web
      • docker logs mailer
      • docker logs agent
    • -it可以通过crtl P+ctrl Q退出shell界面
    • pid命名空间隔离(pid组)
    • 获取容器id
      • 将容器id写入文件中:docker create --cidfile /tmp/web.cid nginx
      • 通过ps命令来获取容器id:docker ps --quiet --latest(获取最新创建的容器id)
      • --quiet --latest 的缩写为-q -l
      • 通过--no-trunc参数获取完整的容器id(若不添加,默认获取的是截断的 12位ID):
      • 将容器id赋给shell变量:ID=$(docker ps --quiet --latest) echo $ID
    • 容器状态
      • 运行中
      • 暂停中
      • 已退出
      • 重启中
    • 容器依赖(--link
      • 需要以相反的依赖顺序来启动容器(如容器B依赖容器A,启动顺序A->B)
        • docker run -d --name web nginx
        • docker -it --name web_test --link web:web busybox /bin/sh
        • wget -O http://web:80
        • docker logs web查看日志
        • docker exec web_test ps
      • 原因:链接的机制是将IP地址注入所依赖的容器中。(将B的IP注入A中,但是若A没运行,docker是不会将B的IP注入到A中的)
      • 由于上述原因,在docker中建立循环依赖关系是不可能的
    • 构建与环境无关的系统
      • 只读环境系统
        • docker run -d --name wp --read-only wordpress:4
        • docker inspect --format "{{.State.Running}}" wp
        • docker run -d --name wpdb -e MYSQL_ROOT_PASSWORD=ch2demo mysql:5
        • docker run -d --name wp2 --link wpdb:mysql -p 80 --read-only wordpress:4
        • docker run -d --name wp3 --link wpdb:mysql -p 9999:80 -v /run/lock/apache2/ -v /run/apache2/ --read-only wordpress:4
          • 报错:WordPress not found in /var/www/html - copying now... Complete! WordPress has been successfully copied to /var/www/html Wed Dec 9 23:15:21 2015 (21): Fatal Error Unable to create lock file: Bad file descriptor (9)
          • 解决:添加-v /tmp
            docker run -d --name wp10 --read-only -v /run/lock/apache2/ -v /run/apache2/ -v /tmp/ --link wpdb:mysql -p 80 wordpress:4
          • 原因:Wordpress的文件锁机制(file locking mechanisms)发生了改变(ver4.2 -> ver4.3)
            • docker run -d --name wp11 --link wpdb:mysql -p 80 wordpress:4
            • docker diff wp11
              C /run
              C /run/apache2
              A /run/apache2/apache2.pid
              C /tmp```
              
      • 环境变量注入
        • docker -e MY_ENV="this is a test" busybox env
      • 容器持久化
        • 方式1:
          • 使用init进程(PID为1的进程)(init、systemd、supervisord、runit、upstart)
            • docker run -d -p 80:80 --name lamp-test tutum/lamp
            • docker exec lamp-test ps 看到supervisord PID为1
          • 使用--restart+启动脚本的方式
            • docker run --entrypoint="cat" --restart always wordpress:4 /entrypoint.sh
      • 容器清理
        • docker ps -a
        • docker rm <容器ID>
        • docker rm -fdocker stop+docker rm的区别
          • docker rm -f 发送的是SIG_KILL信号,立即终止接收过程
          • docker stop发送的是SIG_HUP信号,SIG_HUP信号的接收人可以有时间进行最后的退出和清理任务
        • docker run --rm busybox (–rm参数,容器进入退出状态就会被自动删除)
        • 删除所有容器:docker rm -vf $(docker ps -a -q)
  4. 总结

    • 容器可以基于虚拟终端,连接到用户shell或已守护模式的形式运行
    • 在默认情况下,每个docker容器都有自己的PID命名空间,隔离每个容器的进程信息
    • docker用产生的容器ID简写ID或者其他人性化的名称来标记容器
    • 容器的四种状态:运行、暂停、退出、重启
    • 用户可以通过输入或在容器创建时给 进程指定环境变量的方式,提供额外的配置
    • 使用–read-only标志创建容器时,会将挂载的容器文件系统设置为只读,防止容器被修改
    • 容器重启策略,即在容器创建时设置–restart标志,将有助于系统出现故障时进行自动修改
    • docker使用docker rm命令清理容器
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.
Summary Docker in Action teaches readers how to create, deploy, and manage applications hosted in Docker containers. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology The idea behind Docker is simple. Create a tiny virtual environment, called a container, that holds just your application and its dependencies. The Docker engine uses the host operating system to build and account for these containers. They are easy to install, manage, and remove. Applications running inside containers share resources, making their footprints small. About the Book Docker in Action teaches readers how to create, deploy, and manage applications hosted in Docker containers. After starting with a clear explanation of the Docker model, you will learn how to package applications in containers, including techniques for testing and distributing applications. You will also learn how to run programs securely and how to manage shared resources. Using carefully designed examples, the book teaches you how to orchestrate containers and applications from installation to removal. Along the way, you'll discover techniques for using Docker on systems ranging from dev-and-test machines to full-scale cloud deployments. What's Inside Packaging containers for deployment Installing, managing, and removing containers Working with Docker images Distributing with DockerHub About the Reader Readers need only have a working knowledge of the Linux OS. No prior knowledge of Docker is assumed. About the Author Jeff Nickoloff, a software engineer, has presented Docker and its applications to hundreds of developers and administrators at Desert Code Camp, Amazon.com, and technology meetups.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值