cntchen.github.io icon indicating copy to clipboard operation
cntchen.github.io copied to clipboard

前端工程师的 docker 入门

Open CntChen opened this issue 6 years ago • 0 comments

前端工程师的 docker 入门

docker - the open-source application container engine

前言: 本篇文章2016年10月就记录了,但是搞丢了,最近重新找回,刚好又要搞 docker 了,重新整理完善.

背景

docker 是比较热门的技术,但是作为一名前端工程师,跟 docker 有工作交集的机会比较少。刚好公司有一个项目,需要自己搭建环境,所以试下 docker.这篇文章是我的学习笔记.

机器: Ubuntu16.10

安装

参考资料 docker 安装手册

获得最新的 docker 安装包

$ curl -sSL https://get.docker.com/ | sh

shell 会提示输入 root 的密码,然后开始执行安装过程.

曲折:安装失败.

Err:1 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 docker-engine amd64 1.12.1-0~xenia

这是因为国外的源不稳定,可以尝试墙内的安装方法

$ curl -sSL https://get.daocloud.io/docker | sh // 国内的源

查看 docker 版本

$ sudo docker version

是否安装成功

$ sudo docker run hello-world

成功标志,以下代码的最后一行:

Unable to find image 'hello-world:latest'
locallylatest: Pulling from library/hello-worldc04b14da8d14:
Pull completeDigest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9Status:
Downloaded newer image for hello-world:latest
Hello from Docker!This message shows that your installation appears to be working correctly.

入门

参考资料:docker 入门教程

搜索 docker 镜像

$ docker search node

曲折:命令执行出错.

Warning: failed to get default registry endpoint from daemon
(Cannot connect to the Docker daemon. Is the docker daemon running on this host?).
Using system default: https://index.docker.io/v1/Cannot connect to the Docker daemon.
Is the docker daemon running on this host?

出错原因:需要 root 账户执行. 解决方法把当前用户执行权限添加到相应的 docker 用户组里面

$ sudo groupadd docker// groupadd: group 'docker' already exists
// 添加当前用户到docker用户组里,注意这里的yongboy为ubuntu server登录用户名
$ sudo gpasswd -a yongboy docker// Adding user chenhanjie to group docker
// 重启Docker后台监护进程
$ sudo service docker restart# 重启之后,尝试一下,是否生效
$ docker version
// 若还未生效,则系统重启,则生效m,命令:sudo reboot
// from:http://www.wujianjun.org/2016/04/06/docker-install-issue/
// 重启docker服务
$ sudo service docker restart

搜索 docker

$ docker search node
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
node                      Node.js is a JavaScript-based platform for...   3763      [OK]
nodered/node-red-docker   Node-RED Docker images.                         53                   [OK]
strongloop/node           StrongLoop, Node.js, and tools.                 37                   [OK]
kkarczmarczyk/node-yarn   Node docker image with yarn package manage...   21                   [OK]
bitnami/node              Bitnami Node.js Docker Image                    18                   [OK]
calico/node                                                               14                   [OK]
siomiz/node-opencv        _/node + node-opencv                            10                   [OK]
dahlb/alpine-node         small node for gitlab ci runner                 8                    [OK]
  • docker image 的命名规则 在docker的镜像索引网站上面,镜像都是按照用户名/镜像名的方式来存储的。有一组比较特殊的镜像,比如 ubuntu 这类基础镜像,经过官方的验证,值得信任,可以直接用镜像名来检索到。

下载镜像

$ sudo docker pull node

下载镜像可能会非常缓慢,可以使用国内源:docker下使用daocloud/阿里云镜像加速 .

常用命令

使用 image 创建 container 并进入交互模式

$ docker run -i -t node
> var a = 1
undefined
> console.log(a)
1
undefined
  • -i:表示以“交互模式”运行容器,同步容器的 stdin/stdout 到宿主终端
  • -t:表示容器启动后进入其命令行 这两个参数经常一起使用.

端口映射

$ docker run -i -t -p 2280:80 -p 28080:8080 rap_v7
  • -p 28080:8080:参数类似端口映射的功能 容器内启动服务,然后接口映射到宿主端口.

后台启动

$ docker run -i -t -d node 
1ef49add06ccbb3fbfd47b292707781babc2a72fa15f42148db2ef4017470421
  • -d:表示 deamon ,以后台启动这个 container

安装新程序

  • 启动容器
$ docker run -i -t ubuntu
root@5b248d962f59:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@5b248d962f59:/# cowsay
bash: cowsay: command not found
  • 安装新程序
root@5b248d962f59:/# apt update && apt install cowsay

执行新程序.

root@5b248d962f59:/# whereis cowsay
cowsay: /usr/games/cowsay /usr/share/cowsay /usr/share/man/man6/cowsay.6.gz
root@5b248d962f59:/# /usr/games/cowsay 'hello'
 _______
< hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

保存对容器的修改

使用 docker commit 命令来创建新的 docker image.

  • 查看当前 container
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2fcf9d176a0f        ubuntu              "/bin/bash"         10 minutes ago      Up 10 minutes                           romantic_dubinsky
  • 从 container 生成 image
$ docker commit -m 'add cowsay' 2fcf9d176a0f ubuntu-with-cowsay
sha256:ce53d099e30cd453c416561e3919dcb7eaa0b38ebe4fd22b2f9f2ebe0f669ebc

运行新的镜像

  • 查看新 commit 的 image
$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
ubuntu-with-cowsay   latest              ce53d099e30c        3 minutes ago       211.3 MB
rap_v7               latest              de72241bf273        8 months ago        1.541 GB
  • 运行新 image
$ docker run -i -t ubuntu-with-cowsay
root@f08dc90c206a:/# /usr/games/cowsay 'hello CntChen'
 _______________
< hello CntChen >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
root@f08dc90c206a:/# 

可视化管理

kitematic

使用 kitematic 对 docker image 进行可视化管理。 linux 版本需要自行构建,主要参考:Initial Linux installer。 kitematic 使用 Electron 作为跨平台框架,安装 Electron 过程比较麻烦,反正最后我是安装上了,囧!有机会单独写文章记录安装流程。

// clone
$ git clone https://github.com/docker/kitematic.git
$ cd kitematic$ npm install
// 构建 $ sudo grunk release
// 安装
$ cd dist
$ sudo dpkg -i Kitematic_0.12.1_amd64.deb

安装后报错: kitematic

原因好像是:

基于虚拟机,用于mac与windows平台,linux不需要虚拟机直接运行docker

但是,突然国庆长假回来,就好了!!!

然后愉快地使用kitematic。界面非常好看。

~~shipyard~~

没有完成安装,因为 kitematic 已经可以使用了,如果需要应对多个 host 和 container 的场景,这个工具可能会更好. shipyard 安装参考 .

References

  • docker 安装手册

http://www.docker.org.cn/book/install/supported-platform-17.html

  • 墙内的安装方法

http://get.daocloud.io/#install-docke

  • docker 入门教程

http://www.docker.org.cn/book/docker/what-is-docker-16.html

  • Docker学习笔记之一,搭建一个JAVA Tomcat运行环境

http://www.blogjava.net/yongboy/archive/2013/12/12/407498.html

  • docker下使用daocloud/阿里云镜像加速

http://chuansong.me/n/341282551745

  • Initial Linux installer

https://github.com/docker/kitematic/pull/1677

  • kitematic

https://github.com/docker/kitematic

EOF

CntChen avatar Aug 29 '17 02:08 CntChen