安装Docker
- 安装https源
sudo apt-get install -y apt-transport-https - 添加https源的gpg秘钥
sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv-keys 58118E89F3A912897C070ADBF76221572C52609D - 获取操作系统的代号
lsb_release -c
4.添加文件(注意保持操作系统代号一直,Ubuntu14.04====>>>>trusty)
sudo cat < /etc/apt/sources.list.d/docker.listdeb https://apt.dockerproject.org/repo ubuntu-trusty main
EOF
5.更新软件包
sudo apt-get update
6.开始安装docker
sudo apt-get install -y docker-engine
6.1 利用官方脚本安装docker
sudo curl -sSL https://get.docker.com/ |sh - 启动docker
sudo service docker start 配置docker
为了避免每次使用docker命令都需要使用root权限,我们赋予普通用户加入docker用户组中.
sudo usermod -aG docker UER_NAME
设置之后,退出重新登录.并重启==docker sudo service docker start==
认识Images
获取镜像
docker pull NAME[:TAG]
name镜像名称,tag镜像标签,通常为版本信息
例如:下载一个Ubuntu镜像
root@ubuntu:~# docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
1d8592394ba1: Pull complete
01aa7f61ccd1: Pull complete
5dd2552a960e: Pull complete
7cbe941c5e3e: Pull complete
2549ecfb14c6: Pull complete
Digest: sha256:30204139c6ab96ebd75d72f34db390f28c4decd5e563488b4e485bf979397b67
Status: Downloaded newer image for ubuntu:14.04
-a, –all-tags=true|false 获取仓库全部地址(默认false)
注意 : 如果下载非官方镜像 需要在name前加上仓库地址
查看镜像
- 查看镜像列表
docker images
“`
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 132b7427a3b4 3 days ago 188MB
子选项-a, –all=true|false 列出所有镜像,默认false
–digests=true|false 列出镜像的数字摘要文件,默认false
-f, –filter=[] 过滤镜像,如 dangling=true 只显示没有使用的镜像
–no-trunc=true|false 对输出内容太长部分进行截断,默认是
root@ubuntu:~# docker tag ubuntu:14.04 myubuntu:14
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 14 132b7427a3b4 3 days ago 188MB
ubuntu 14.04 132b7427a3b4 3 days ago 188MB
```
- 用inspect查看镜像详细信息
docker inspect ubuntu:14.04
- 查看镜像历史信息
docker history ubuntu:14.04
- 搜索镜像
docker search --automated -s 80 redis
注意
–automated=true|false 仅显示自动创建的镜像,默认否
–no-trunc=true|false 输出信息是否截断显示,默认否
-s, –stars=x 输出指定星级以上的镜像,默认0
删除镜像
- 使用标签删除镜像
docker rmi myubuntu:14
如果成功返回:Untagged: myubuntu:14
注意:利用标签删除镜像,如果本地存在镜像id相同的镜像,不影响其他标签镜像
- 利用镜像ID删除镜像
注意: – 删除一个正在被容器使用的镜像
root@ubuntu:~# docker run ubuntu:14.04 echo 'hell, i am here!'
hell, i am here!
root@ubuntu:~# docker rmi 132b7427a3b4
Error response from daemon: conflict: unable to delete 132b7427a3b4 (must be forced) - image is being used by stopped container 5102bf6a1010
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5102bf6a1010 ubuntu:14.04 "echo 'hell, i am ..." About a minute ago Exited (0) About a minute ago hungry_thompson
root@ubuntu:~# docker rm 5102bf6a1010
5102bf6a1010
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
从上可以看出,通常情况下,我们不能直接删除一个被容器正在使用的镜像,我们需要先把这个容器删除掉,然后在清除对应镜像.
当然我们可以通过 ==docker rmi -f xxx== 强制删除镜像
6. 创建镜像
1. 基于已有镜像的容器创建
```
stormfast@ubuntu:~$ docker run -it ubuntu:14.04 /bin/bash
root@99cd2ed8fe20:/# touch test
root@99cd2ed8fe20:/# exit
exit
stormfast@ubuntu:~$ docker commit -m "added a new file" -a "Docker zx" 99cd2ed8fe20 test:0.1
sha256:6a3a026cd43a44ee811afed96361eb368f2dff832314367c2cfc965ef3b6d54f
stormfast@ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 6a3a026cd43a 8 seconds ago 188MB
ubuntu 14.04 132b7427a3b4 3 days ago 188MB
```
-it (为容器分配一个伪输入终端)
子选项:
-a, --author="" 作者信息
-c, --change=[] 提交的时候执行Dockerfile指令
-m, --message="" 提交信息
-p, --pause=true 提交时暂停容器运行
2. 基于本地模板导入
3. 基于Dockerfile创建
7. 存入和载入镜像
1. 存出镜像
```
docker save -o ubuntu_14.04.tar ubuntu:14.04
```
2. 载入镜像
docker load --input ubuntu_14.04.tar
或者
docker load < ubuntu_14.04.tar
8. 上传镜像
我们可以上传自己的镜像到远程仓库,方便之后使用
```
docker push NAME[:TAG] | [REGISTRY_HOST[REGISTRY_PORT]/]NAME[:TAG]
```
```
stormfast@ubuntu:~$ docker tag test:0.1 zxfendoubuzhi/test:latest
stormfast@ubuntu:~$ docker push zxfendoubuzhi/test:latest
The push refers to a repository [docker.io/zxfendoubuzhi/test]
ada26b46e50d: Pushed
7bf04ad1d006: Pushed
023b7696bd58: Pushed
ad37eb38337a: Pushed
350bf4dddc59: Pushed
```
*注意*:保持==zxfendoubuzhi==/test和你远程仓库的用户名相同