Chinaunix首页 | 论坛 | 博客
  • 博客访问: 564643
  • 博文数量: 211
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1480
  • 用 户 组: 普通用户
  • 注册时间: 2018-03-07 14:59
文章分类
文章存档

2019年(161)

2018年(50)

我的朋友

分类: 系统运维

2018-12-31 19:18:53

Docker是一个轻量级的基于进程的虚拟化解决方案。简单来说,Docker是将应用程序和运行环境一起打包成一个容器,进而可以发布到其他平台上运行。

环境:Ubuntu 18.04.1 LTS


安装Docker


root@jaking-virtual-machine:~# apt-get install -y docker-engine Reading package lists... Done
Building dependency tree       
Reading state information... Done
...

root@jaking-virtual-machine:~# docker version Client: Version: 18.06.1-ce
 API version: 1.38 Go version: go1.10.4 Git commit: e68fc7a Built: Fri Oct 19 19:43:14 2018 OS/Arch: /amd64 Experimental: false Server: Engine: Version: 18.06.1-ce
  API version: 1.38 (minimum version 1.12)
  Go version: go1.10.4 Git commit: e68fc7a Built: Thu Sep 27 02:39:50 2018 OS/Arch: linux/amd64 Experimental: false root@jaking-virtual-machine:~# systemctl start docker root@jaking-virtual-machine:~# systemctl enable docker Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable docker 

搜索ubuntu相关的容器

root@jaking-virtual-machine:~# docker search ubuntu NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating sys 8838 [OK]                
dorowu/ubuntu-desktop-lxde-vnc                         Ubuntu with openssh-server and NoVNC 247 [OK]
rastasheep/ubuntu-sshd                                 Dockerized SSH service, built on top of offi 184 [OK]
consol/ubuntu-xfce-vnc                                 Ubuntu container with "headless" VNC session 136 [OK]
ansible/ubuntu14.04-ansible                            Ubuntu 14.04 LTS with ansible 95 [OK]
ubuntu-upstart                                         Upstart is an event-based replacement for th 92 [OK]


下载容器


root@jaking-virtual-machine:~# docker pull ubuntu-upstart 
Using default tag: latest
latest: Pulling from library/ubuntu-upstart 8387d9ff0016: Pull complete 3b52deaaf0ed: Pull complete 4bd501fad6de: Pull complete a3ed95caeb02: Pull complete a6dc1658c730: Pull complete 9ed623dca71b: Pull complete 998ee72febf9: Pull complete 437038dc2fba: Pull complete da0ee05a1a1d: Pull complete 1e1c3e99deb1: Pull complete 4fcc22d7b2a1: Pull complete 6c7dda5571e4: Pull complete Digest: sha256:597dfb1868012dcd04a705572dbc1542cb7598bce0eaa1c2656eb3acfc8b51d2
Status: Downloaded newer image for ubuntu-upstart:latest


查看容器的镜像


root@jaking-virtual-machine:~# docker images ubuntu-upstart REPOSITORY TAG IMAGE ID            CREATED             SIZE
ubuntu-upstart      latest              b28219773b9b 2 years ago 253MB

从上面的结果可以看到,容器已经成功下载。利用下载的ubuntu-upstart容器,可以运行一个简单的程序,此处以“Hello Docker”为例:

root@jaking-virtual-machine:~# docker run ubuntu-upstart /bin/echo Hello Docker Hello Docker

还可以使用其他容器,如使用ubuntu作为容器,下载操作如下:

root@jaking-virtual-machine:~# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
32802c0cfa4d: Pull complete 
da1315cffa03: Pull complete 
fa83472a3562: Pull complete 
f85999a86bef: Pull complete 
Digest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49
Status: Downloaded newer image for ubuntu:latest
root@jaking-virtual-machine:~# docker images ubuntu
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              93fd78260bd1        10 days ago         86.2MB


Docker的容器操作


当Docker中包含了容器,就如同虚拟机中安装了操作系统一样,可以运行、安装软件、做一些设置。现在就可以运行之前下载的ubuntu:

root@jaking-virtual-machine:~# docker run -i -t ubuntu /bin/bash 
#运行一个名为ubuntu的容器 #i选项表示捕获标准输入和输出;t选项表示分配的终端和控制台 root@05559b460591:/#  root@05559b460591:/# uname -a Linux 05559b460591 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux root@05559b460591:/# exit #退出容器 exit
root@jaking-virtual-machine:~# docker ps CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

从上面的命令可以看到,使用run命令运行了一个容器,但退出之后容器也关闭了,这不是想要的结果。这时可以使用选项d让容器一直在后台运行:

root@jaking-virtual-machine:~# docker run -d -i -t ubuntu /bin/bash
b19cc95aef9cb6f402062915b527864cf045debc65dbabd23a495cea32a138dd
root@jaking-virtual-machine:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
b19cc95aef9c ubuntu "/bin/bash" 15 seconds ago      Up 14 seconds                                   kind_johnson 05559b460591 ubuntu "/bin/bash" 35 minutes ago      Exited (0) 9 minutes ago                        xenodochial_hypatia 5bc78fd29b2a        ubuntu-upstart "/bin/echo Hello Doc" 42 minutes ago      Exited (0) 42 minutes ago                       silly_jennings c54bb6d664b7 ubuntu-upstart "/bin/echo Hello Doc" 44 minutes ago      Exited (0) 44 minutes ago                       jolly_thompson

从上面的命令输出可以看到一个IDb19cc95aef9c的容器正在运行,这个ID号就是操作此容器的重要参数。
容器运行在后台时,可以使用attach登录正在运行的容器:

root@jaking-virtual-machine:~# docker attach b19cc95aef9c root@b19cc95aef9c:/# exit
exit
root@jaking-virtual-machine:~#

容器的操作还有很多命令,常用的操作还有:

docker cp :将容器中的文件复制到主机上 docker rm:删除一个容器 docker port:配置容器的端口转发 docker start:启动一个容器 docker stop:停止一个容器 docker top:显示容器中的进程 docker ps:列出容器 docker logs:获取容器的日志 除了以上这些操作外,Docker还有许多操作,可自行阅读相关文档了解。
阅读(1263) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~