Docker基础用法

来自CloudWiki
跳转至: 导航搜索

准备工作

查看Docker信息

docker info

Containers: 1
 Running: 0
 Paused: 0
 Stopped: 1
Images: 1
Server Version: 18.06.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs

Docker的镜像操作

搜索镜像

maxin@maxin-virtual-machine:~$ sudo docker search centos

NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4419                [OK]
ansible/centos7-ansible            Ansible on Centos7                              114                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x86_…   97                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   57                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56 

获得一个镜像

下载一个官方的 CentOS 镜像到本地

docker pull centos

下载好的镜像就会出现在镜像列表里

docker images

查看已下载的镜像

maxin@maxin-virtual-machine:~$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED                                                                                          SIZE
hello-world         latest              2cb0d9787c4d        9 days ago                                                                                       1.85kB
centos              latest              49f7960eb7e4        6 weeks ago                                                                                      200MB

镜像的导出

maxin@maxin-virtual-machine:~$ sudo docker save 2cb0d9787c4d > ~/backup.tar

maxin@maxin-virtual-machine:~$ ls

backup.tar  Desktop  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos

镜像的导入

docker load < backup.tar

Docker 容器操作

运行容器

这时我们可以在刚才下载的 CentOS 镜像生成的容器内操作了。

生成一个 centos 镜像为模板的容器并使用 bash shell

docker run -it centos /bin/bash

这个时候可以看到命令行的前端已经变成了 [root@(一串 hash Id)] 的形式, 这说明我们已经成功进入了 CentOS 容器。

在容器内执行任意命令, 不会影响到宿主机, 如下

mkdir -p /data/simple_docker

可以看到 /data 目录下已经创建成功了 simple_docker 文件夹

ls /data


退出容器

exit

查看宿主机的 /data 目录, 并没有 simple_docker 文件夹, 说明容器内的操作不会影响到宿主机

ls /data

保存容器

查看所有的容器信息, 能获取容器的id

docker ps -a

然后执行如下命令[?],保存镜像:

docker commit -m="备注" 你的CONTAINER_ID 你的IMAGE

大功告成!

恭喜你结束了 Docker 的教程并学会了 Docker 的一些基本操作!

参考文档:

[1] http://www.runoob.com/docker/docker-tutorial.html

[2] https://cloud.tencent.com/developer/labs/lab/10054