测试的环境为Ubuntu1804。
1. search命令搜索镜像
sudo docker search centos
搜索centos相关的镜像,可以看到第一个最多星的的centos是官方的镜像,而其它的是用户自己创建并分享的。
2. pull命令下载镜像
sudo docker pull ubuntu:latest
下载Ubuntu最新版的镜像,latest默认选择是最新的稳定版本。
3. images命令列出镜像
sudo docker images
4. run命令创建容器
sudo docker run -i -t --name hello ubuntu /bin/bash
上面的命令是使用ubuntu镜像创建一个名称是hello容器,命令/bin/bash是运行在创建的hello容器,其中-i(interative)、-t(Pseudo-tty)选项可以在bash中进行输入输出。使用sudo docker container ls -a可以查看已创建的容器,可以看到刚才创建的name为hello的容器。
5. ps命令查看容器列表
sudo docker ps -a
sudo docker ps -a和sudo docker container ls -a的都是查看所有已创建的容器列表。
6. start/restart/stop命令启动/重启/停止容器
sudo docker start hello sudo docker restart hello sudo docker stop 4be81a62488d
命令格式为docker start/restart/stop <名称/id>。如sudo docker stop 4be81a62488d,可以使用名称,也可以使用id指定的容器。
7. attach命令连接容器
sudo docker attach hello
命令格式为docker attach <名称/id>,注意连接的容器必须是已经启动的容器。
8. exec命令从外部运行容器内命令
sudo docker exec hello ls -a
命令格式为docker <名称/id> <命令> <参数>,如docker exec hello apt install git命令在hello容器上运行apt install git命令安装git软件,不过有一个安装的确认Y为不知道怎么输入到容器中,故这里不列出运行截图。
9. rm/rmi命令删除容器/镜像
sudo docker rm hello
sudo docker rmi 3517f071388e
命令格式为docker rm/rmi <名称/id>删除制定容器名称或id的容器/镜像。 如docker rm hello命令删除容器名称为hello的容器。