Docker 简单笔记 
主要是为了部署ODL
0x01 基本命令: 
不用全部掌握,会基础的命令,不常用的百度即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 管理命令:   container   管理容器   image       管理镜像   network     管理网络   node        管理Swarm节点   plugin      管理插件   secret      管理Docker secrets   service     管理服务   stack       管理Docker stacks   swarm       管理Swarm集群   system      查看系统信息   volume      管理卷      如:docker container ls 显示所有容器    普通命令:        attach     进入一个运行的容器   build      从一个DockerFile构建镜像   commit     从容器创建一个镜像   cp          从容器和主机文件系统之间拷贝文件    create      创建一个容器   diff        检查容器文件系统上的更改   events      从服务器获取实时事件   exec        在正在运行的容器中运行命令   export      将容器的文件系统导出为tar存档   history     显示镜像的历史记录   images      查看镜像列表   import      从归档文件中创建镜像   info        显示系统范围的信息   inspect     返回Docker对象的低级信息   kill        kill运行中的容器   load        从存档或者STDIN加载镜像   login       登陆docker镜像仓库   logout      退出docker镜像仓库   logs        获取一个容器的日志   pause       暂停一个或多个容器中的所有进程   port        查看端口映射或容器的特定映射列表   ps          查看容器列表   pull        从镜像仓库拉取镜像   push        将本地的镜像上传到镜像仓库,要先登陆到镜像仓库   rename      重命名容器   restart     重启容器   rm          删除容器   rmi         删除镜像   run         创建一个新的容器并运行一个命令   save        将指定镜像保存成 tar 归档文件   search      从Docker Hub搜索镜像   start       启动容器   stats       实时显示容器资源使用情况的统计信息   stop       停止容器   tag         标记本地镜像,将其归入某一仓库   top         展示一个容器中运行的进程   unpause     恢复容器中所有的进程   update      更新容器配置   version    显示Docker的版本信息   wait        阻塞直到容器停止,然后打印退出代码      如:docker images 显示所有镜像 
 
0x02 docker 上传容器: 
推荐使用阿里云的镜像管理仓库,官方的实在是太慢了
先登录: 
1 2 3 ~/Desktop$  docker login  --username=username registry.cn-hangzhou.aliyuncs.com Password:  Login Succeeded 
1 2 3 4 5 6 7 8 9 ~/Desktop$  docker ps CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES 6dc5d2ea3107        ubuntu:16.04        "/bin/bash"         42 minutes ago      Up 42 minutes                           confident_fermat ~/Desktop$  docker commit 6dc5d2ea3107 vim sha256:a3c766c860ee134abf0ee685df015e8dce543212b94054d44ff64aed7bd3eb5b ~/Desktop$  docker images REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE vim                 latest              a3c766c860ee        15 seconds ago      222MB ubuntu              16.04               330ae480cb85        2 weeks ago         125MB 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ~/Desktop$  docker tag vim registry.cn-hangzhou.aliyuncs.com/lowbee/ubuntu:vim /Users/yunlong/Desktop$  docker images REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE vim                                               latest              9db29825c783        6 minutes ago       222MB registry.cn-hangzhou.aliyuncs.com/lowbee/ubuntu   vim                 9db29825c783        6 minutes ago       222MB ubuntu                                            16.04               330ae480cb85        2 weeks ago         125MB /Users/yunlong/Desktop$  docker push registry.cn-hangzhou.aliyuncs.com/lowbee/ubuntu:vim The push refers to repository [registry.cn-hangzhou.aliyuncs.com/lowbee/ubuntu] dd3211c2ed28: Pushed  21dabaeb6afb: Pushed  f182989ffc12: Pushed  0cf0b0655e84: Pushed  88b486ee59d1: Pushed  vim: digest: sha256:693405e9175332585220f29f3ce317e8b88d51d0f4426dbec5ef1b39d6a9b958 size: 1362 
0x03 docker重启后: 
重新运行容器:docker start  容器id
进入交互命令: docker attach 容器id
也可以直接运行:docker start -ai 容器id 其中-a是attach,-i是交互 
docker ps -q -l:其中-q是指只列出容器的id,-l是最后创建的容器
0x04 DockerFile创建镜像 
此处用于部署ODL控制,已经下拉Ubuntu16.04的image
 
创建文件如下,及dockerfile文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 FROM ubuntu:16.04 ADD sources.list /etc/apt/ RUN apt-get update &&\     apt-get install unzip -y &&\     apt-get install net-tools -y &&\      apt-get install iputils-ping -y &&\      apt-get install vim -y &&\     apt-get autoclean &&\     apt-get autoremove COPY distribution-karaf-0.6.4-Carbon.zip jdk-8u231-linux-x64.tar.gz /home/ WORKDIR /home RUN tar -zxvf jdk-8u231-linux-x64.tar.gz &&\     rm jdk-8u231-linux-x64.tar.gz &&\     unzip distribution-karaf-0.6.4-Carbon.zip &&\     rm distribution-karaf-0.6.4-Carbon.zip	   ENV JAVA_HOME=/home/jdk1.8.0_231 ENV JRE_HOME=$JAVA_HOME/jre  ENV CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH ENV PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH   EXPOSE 8181 6653 6633 
将需要用到的文件包放在同一个目录下。顺便换源,此处使用阿里源:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse 
创建镜像:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 lowbee dockerfile $ls    Dockerfile1                         jdk-8u231-linux-x64.tar.gz distribution-karaf-0.6.4-Carbon.zip sources.list lowbee dockerfile $docker images REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE ubuntu              16.04               330ae480cb85        2 weeks ago         125MB lowbee dockerfile $docker build -f Dockerfile1 -t opendaylight:v1.0 . Sending build context to Docker daemon  624.5MB Step 1/11 : FROM ubuntu:16.04  --->  330ae480cb85 Step 2/11 : ADD sources.list /etc/apt/  --->  21986c4da5cf Step 3/11 : RUN apt-get update &&    apt-get install unzip -y &&    apt-get install net-tools -y &&    apt-get install iputils-ping -y &&    apt-get install vim -y &&    apt-get autoclean &&    apt-get autoremove  --->  Running in  d970f18b6aff ... ...  --->  772580ede22f Step 11/11 : EXPOSE 8181 6653 6633  --->  Running in  dffd22daa5f2 Removing intermediate container dffd22daa5f2  --->  d33d72ee7f48 Successfully built d33d72ee7f48 Successfully tagged opendaylight:v1.0 lowbee dockerfile $docker images REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE opendaylight        v1.0                d33d72ee7f48        40 seconds ago      1.75GB ubuntu              16.04               330ae480cb85        2 weeks ago         125MB 
命令会一步一步的执行,直到最后出现successfully,才算安装成功。
0x05 运行ODL进行配置 
踩坑: ~~MacOS 下主机是无法ping通容器的,一定要注意!!!~~采用端口映射将web服务映射到主机上即可, -p 主机端口:容器端口。
在更新Docker至4.3.1后可以直接相互ping
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 lowbee Desktop $docker run -it -p 8181:8181 -p 6653:6653 -p 6633:6633 --name ODL d33d72ee7f48 root@6c0cc6123ec4:/home# ls distribution-karaf-0.6.4-Carbon  jdk1.8.0_231 root@6c0cc6123ec4:/home# cd distribution-karaf-0.6.4-Carbon/bin  root@6c0cc6123ec4:/home/distribution-karaf-0.6.4-Carbon/bin# ./karaf  Apache Karaf starting up. Press Enter to open the shell now... 100%  [========================================================================] Karaf started in 2s. Bundle stats: 64 active, 64 total                                                                                                 ________                       ________                .__  .__       .__     __            \_____  \ ______   ____   ____ \______ \ _____  ___.__.|  | |__| ____ |  |___/  |_           /   |   \\____ \_/ __ \ /    \ |    |  \\__  \<   |  ||  | |  |/ ___\|  |  \   __\         /    |    \  |_> >  ___/|   |  \|    `   \/ __ \\___  ||  |_|  / /_/  >   Y  \  |           \_______  /   __/ \___  >___|  /_______  (____  / ____||____/__\___  /|___|  /__|                   \/|__|        \/     \/        \/     \/\/            /_____/      \/                                                                                                       Hit '<tab>' for a list of available commands and '[cmd] --help' for help on a specific command. Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown OpenDaylight. opendaylight-user@root> 
Opendaylight 安装属性:
1 2 3 4 5 6 7 feature:install odl-restconf feature:install odl-l2switch-switch-ui feature:install odl-openflowplugin-flow-services-ui feature:install odl-mdsal-apidocs feature:install odl-dlux-core feature:install odl-dluxapps-nodes feature:install odl-dluxapps-yangui 
在主机上访问http://localhost:8181/index.html#/login:
推荐Kitematic 进行管理容器,真香︿( ̄︶ ̄)︿