docker入门实践01

Docker 实践入门#

本文的所有操作的环境都是在一台虚拟机上操作完成的。

$ cat /etc/issue
Ubuntu 14.04.1 LTS 
 l

$ uname -r
3.13.0-32-generic

安装Docker##

必要条件###

  • 内核版本要至少为3.8
  • 64位的CPU
  • 内核必须支持一种适合的存储驱动
    • Device ManagerThe Device Manager is a Control Panel applet in Microsoft Windows operating systems. It allows users to view and control the hardware attached to the computer。)
    • AUFSAUFS implements a union mount for Linux file systems)
    • VFSA virtual file system (VFS) is an abstraction layer on top of a more concrete file system. The purpose of a VFS is to allow client applications to access different types of concrete file systems in a uniform way)
    • btrfsbtrfs is intended to address the lack of pooling, snapshots, checksums, and integral multi-device spanning in Linux file systems.)
    • 默认的存储驱动通常是 Device Mapper
  • 内核必须支持并且开启 cgroupnamespace

检查前提条件###

(1) 内核

$ uname -a
Linux jackson-virtual-machine 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

可以看到本机所装的内核版本为3.13.0-32-generic,可以满足docker的安装条件,当然如果不满足内核条件,升级内核也是很容易的。

$ sudo apt-get update  # 这一步更新包列表
$ sudo apt-get upgrade # 安装所有更新

如果不想更新那么多,可以

$ sudo apt-get install XXXX #安装相应的内核包

最后需要更新 Grub 启动加载器来加载新的内核

$ sudo update-grub

重启

$ sudo reboot

(2) 检查 Device Mapper

Device Mapper在2.6.9内核中就引入了。查看方法有两种:

$ ls -l /sys/class/misc/device-mapper
lrwxrwxrwx 1 root root 0  1月 25 21:30 /sys/class/misc/device-mapper -> ../../devices/virtual/misc/device-mapper

$ sudo grep device-mapper /proc/devices  # 如果没有,可以通过 sudo modprobe dm_mod 
252 device-mapper

(3) 检查cgroup和namespace

这两个从2.6版本就开始集成在了linux内核中。我们经常会碰到 docker,lxc,cgroup,namespace几个在一起,这几个具体什么关系呢。

  • docker(go 语言实现)是 lxc 的管理器
  • lxc 是 cgroup的管理工具
  • cgroup 是namespace 的用户空间的接口

安装docker###

使用docker团队的DEB软件仓库来安装docker。所以对于ubuntu需要添加APT仓库。

$ sudo sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"

ps: 新装的ubuntu系统的默认源都是使用/etc/apt/sources.list这个文件,而这个文件里默认的都是国外的网站,所有有时候会遇到下载软件很慢的情况,这时候只要更开一下这个文件,将文件里面的源换成国内的网站,比如163等。

接下来添加docker仓库的GPG秘钥。

$ curl -s https://get.docker.io/gpg | sudo apt-key add -

在ubunt系统中,用gpg可以实现加密和解密。在这里,我们添加新的源,需要下载一个加密文件,否则无法下载。
apt-key add filename
Add a new key to the list of trusted keys. The key is read from
filename, or standard input if filename is -.
apt-key是Debian软件包的安全管理工具。每个发布的deb包,都是通过密钥认证的,apt-key用来管理密钥。

$ sudo apt-get update 
$ sudo apt-get install lxc-docker 

完成后

$ sudo docker # 会出现help

docker使用##

docker的常用基本命令###

(1) 看docker程序是否在运行

$ sudo status docker 
docker start/running, process 3004

$ sudo stop docker
docker stop/waiting
 
$ sudo start docker
docker start/running, process 3068

或者

$ sudo service docker status
docker start/running, process 3068
 
$ sudo service docker stop
docker stop/waiting
 
$ sudo service docker start
docker start/running, process 3145

(2)看docker的基本信息(该命令会返回所有的容器,以及docker使用的执行驱动,存储驱动,docker的基本配置)

$ sudo docker info
Containers: 1
Images: 6
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 8
Execution Driver: native-0.2
Kernel Version: 3.13.0-32-generic
Operating System: Ubuntu 14.04.1 LTS
CPUs: 4
Total Memory: 3.857 GiB
Name: jackson-virtual-machine
ID: 6YTW:46CV:LZM4:HKED:6ZML:PE5V:4EJK:VG2I:I5RS:BETR:FYDN:Y6SW
Username: jacksonzhangkun
Registry: [https://index.docker.io/v1/]
WARNING: No swap limit support

(3)查看机器已有的镜像

$ sudo docker images
REPOSITORY  TAG IMAGE IDCREATED VIRTUAL SIZE
busybox latest  234ee811cd806 weeks ago 2.434 MB

新装的docker上面是什么都没有的。

(4)搜索镜像

$ sudo docker search busybox

会有很多list出来,OFFICIAL字段下有[OK]说明它是官方镜像。

(5)下载镜像

$ sudo docker pull busybox
busybox:latest: The image you are pulling has been verified

511136ea3c5a: Already exists 
df7546f9f060: Already exists 
ea13149945cb: Already exists 
Status: Image is up to date for busybox:latest

这个镜像我已经下载过了,所以显示已经存在。

(6)运行一个镜像

$ sudo docker run -t -i busybox
/ # ls
bin      etc      lib      linuxrc  mnt      proc     run      sys      usr
dev      home     lib64    media    opt      root     sbin     tmp      var
/ # 

其中-i标志保证容器的STDIN是开的,-t表示Docker要为新创建的容器分配一个伪tty终端。这样,新创建的容器,才能提供一个交互式的shell。

ps: busybox是一个集成了一百多个最常用linux命令和工具的软件工具箱。非常精简,在(3)中,我们可以看到,只有2.434 MB。

(7)看运行中的docker容器

$ sudo docker ps

如果要看所有当前容器的列表,如要加参数 -a

$ sudo docker ps -a 

如果要看最后一次运行的容器,可以

$ sudo docker ps -l

(8)启动容器

$ sudo docker start (NAMES/CONTAINER ID)

(9) 深入容器,可以通过docker inspect,获取更多的容器信息

$ sudo docker inspect (NAMES/CONTAINER ID)

(10) 删除容器

$ sudo docker rm 3b39cfcdee2e

如果要全部删除,可以

$ sudo docker rm `sudo docker ps -a -q`

(11) 删除镜像

$ sudo docker rmi <IMAGE ID>

创建一个自定义的docker容器###

首先我现在这边有个从官方下下来的一个busybox镜像,并且我运行了一个基于该镜像的一个容器。容器ID为:

$ sudo docker ps -q -l
1c6364f2f8b4

$ sudo docker start 1c6364f2f8b4
1c6364f2f8b4

$ sudo docker attach 1c6364f2f8b4

/ # hello
/bin/sh: hello: not found
/ # 	

进入之后发现没有hello这个命令,也是busybox这个镜像里面没有这个命令。下面我们自定义一个一个简单的hello命令。

/ # vi hello 
/ # sh hello 
hello world
/ # chmod +x hello 
/ # mv hello /bin/
/ # hello 
hello world
/ # exit

$ sudo docker commit 1c6364f2f8b4 busybox
082a195052cf6fb75f8cf86f467562ef94bedbab365591870fbfe127dfe91d85

docker commit提交的只是创建容器的镜像和容器当前状态之间的差异部分。提高后我们在基于新的busybox镜像创建容器。可以看到创建的时间变成了两分钟前

$ sudo docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
busybox             latest              082a195052cf        2 minutes ago       2.434 MB

$ sudo docker run -ti busybox
/ # hello 
hello world

上面演示了简单的怎么自定义docker镜像。

原文地址:https://www.cnblogs.com/zk47/p/4709140.html