Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:linux:docker

docker

常用文档:

  • docker 的 container 默认使用 8.8.8.8 作为 DNS,可修改 service 参数使用其他 DNS
    $ vi /etc/default/docker
    DOCKER_OPTS="--dns <your_dns_server_1> --dns <your_dns_server_2>"
    $ sudo service docker restart

docker 的新项目

format

Format command and log output | Docker Documentation

Docker uses Go templates which you can use to manipulate the output format of certain commands and log drivers.

``` $ docker inspect mycontainer –format 'state.status' exited

$ docker inspect –format 'join_.hostconfig.binds' mycontainer /Users/me/.ssh:/root/.ssh:rw , /Users/xp/.gitconfig:/root/.gitconfig:rw ```

我用它来获取容器的状态

2018

Linux 安装最新版 docker

centos 必须是 centos 7 以上,不支持 centos 6 了,centos 6 升级到 7 有风险,就算在 centos 6 上安装了 docker 1.7、docker-compose 1.4,也用不了

# 删除旧版本
$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

# 安装新版本
$ curl -fsSL https://get.docker.com/ | sh

$ docker version
$ sudo systemctl start docker
$ sudo systemctl status docker
$ sudo systemctl enable docker

$ docker version
Client:
 Version:      18.05.0-ce

Server:
 Engine:
  Version:      18.05.0-ce

$ vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

$ sudo systemctl restart docker

2017

  • 清理空间
    docker system prune
    # will delete ALL unused data (i.e. In order: containers stopped, volumes without containers and images with no containers).
     
    # You also have:
    docker container prune
    docker image prune
    docker network prune
    docker volume prune

dockerfile 常见问题

cli

  • pull 和 run 都可在镜像后加 tag:docker pull php:5.6-fpm

coreos

features:

  • 有两个root分区,我们暂且称其为root A和root B。CoreOS会与更新服务进行交互,查找更新并自动下载可用的更新,如果初始状态下,系统在root A下启动,更新就会被安装到root B,重新在root B下启动系统就可以完成更新。在这个过程中,被更新的机器不需要从负载集群中移除。同时,为了保证其它应用程序不被打断,CoreOS会通过Linux cgroups限制更新过程中的硬盘和网络I/O
  • Run Services with docker: 所有附加的功能都被剔除了,并将操作系统和应用程序做了完全的分离 (docker)
  • Cluster Management with fleet
  • Service Discovery with etcd

usage:

    • coreos-vagrant 默认为 NAT,不需额外设置 PROXY 就能使用 host 的 PROXY 上网
  • vagrant ssh
    • 配置 docker PROXY
      $ sudo cp /usr/lib/systemd/system/docker.service /etc/systemd/system/
       
      $ sudo vi /etc/systemd/system/docker.service
      # above the ExecStart command:
      # Environment="HTTP_PROXY=http://proxy.example.com:8080"
       
      $ sudo systemctl daemon-reload
      $ sudo systemctl restart docker

workflow

The "official" Docker workflow looks like this: * Install Docker on your dev ma... | Hacker News

  1. Install docker(linux)/boot2docker(mac, a VM runs a linux docker) on your dev machine
  2. Add a Dockerfile to your source repo, specifying how to assemble a container image from source.
  3. Use a combination of “docker build” and “docker run” to test during development. You can use docker tags to build a separate image for each git commit/tag/branch.
  4. When ready to deploy, use “docker push” to upload your image to a registry (you can run your own, or use the official registry at https://index.docker.io). Note the official registry supports private images.
  5. From your production machines (presumably CoreOS but you may have a mix of other distros too) run “docker pull” and “docker run” to deploy your app.
  6. Use Links (“docker rum –link”) to interconnect containers, for example your frontend to your database, etc.

node.js development

  • Dockerfile
    # DOCKER-VERSION 0.3.4
    FROM node:0.10.30
    
    EXPOSE  8080
    
    WORKDIR   /src
     
    CMD ["/bin/bash"]
  • 启动命令
    docker build -t toksea/node-hello .
     
    docker run -i -t --rm    \
               -v `pwd`:/src \
               -p 49160:8080 \
               toksea/node-hello
    # -i 会在交互模式下启动容器 (对比 -d 是在分离模式下). 这就意味一旦交互会话结束,容器就会退出.
    # -t 会分配一个 pseudo-tty.
    # --rm 会在退出时移除容器及其文件系统.

如果按 ADD . /src + npm i 的用法,由于 . 总变,所以 npm 无法被缓存,每次 build 都很慢。

所以有人提出可以在 ADD package.json /tmp/package.json 并在 ADD . /src 后从 cp /tmp/node_modules /src 的做法(但如果是生产环境,而非经常 rebuild 的开发环境,就按普通做法、等几分钟吧):Building Efficient Dockerfiles - Node.js - bitJudo

fig

docker 的 cli 都比较复杂,所以有了 fig 简化这些操作

  • fig up xx 遇到 container 出错时,好像不会输出错误信息,而是卡在 Attaching to 上,可以 fig run xx command 来看具体错误
  • Dockerfile 改变后, fig up 不会重新 build,需要手动 fig buildfig up
  • 目前遇到了 fig 启动后(无论 run 还是 up),docker expose 并 fig map 的端口,从 host 无法访问的问题,但 docker run 相同 Dockerfile 却能启动,未找到答案 — Xiaopei Li 2014/10/22 06:17
    • 最后发现可能是有之前的 fig service 死进程,将其 rm 后再 up 便正常了
      $ fig ps
          Name         Command     State    Ports
      -------------------------------------------
      vhome_cloud_1   node index   Exit 8
       
       
      $ fig logs
      Attaching to vhome_cloud_1
      cloud_1 |
      cloud_1 | module.js:340
      cloud_1 |     throw err;
      cloud_1 |           ^
      cloud_1 | Error: Cannot find module 'express'
      cloud_1 |     at Function.Module._resolveFilename (module.js:338:15)
      cloud_1 |     at Function.Module._load (module.js:280:25)
      cloud_1 |     at Module.require (module.js:364:17)
      cloud_1 |     at require (module.js:380:17)
      cloud_1 |     at Object.<anonymous> (/src/app.js:5:15)
      cloud_1 |     at Module._compile (module.js:456:26)
      cloud_1 |     at Object.Module._extensions..js (module.js:474:10)
      cloud_1 |     at Module.load (module.js:356:32)
      cloud_1 |     at Function.Module._load (module.js:312:12)
      cloud_1 |     at Module.require (module.js:364:17)
       
       
      $ fig rm  cloud
      Going to remove vhome_cloud_1
      Are you sure? [yN] y
      Removing vhome_cloud_1...
       
       
      $ fig ps
      Name   Command   State   Ports
      ------------------------------
       
       
      $ fig up cloud
      Creating vhome_cloud_1...
      Attaching to vhome_cloud_1
      cloud_1 | /src/cloud/data/flows_vhome_1.json
      # ok, up
       
       
      $ xp@xp-desktop:~/Develop/vhome$ fig ps
          Name         Command     State           Ports
      -----------------------------------------------------------
      vhome_cloud_1   node index   Up      0.0.0.0:8080->8080/tcp
      # 正常的 fig ps, 注意 Ports
it/linux/docker.txt · Last modified: 2018/10/17 16:05 by admin