Linux-ubuntu20.04 docker 镜像操作(一)
1、什么是镜像?什么是容器?
镜像相当于iso光盘,而容器相当于通过ISO光盘安装的系统
2、镜像的相关操作
sudo 配置过docker免sudo操作后可以省去,具体免sudo操作见文章https://blog.csdn.net/weixin_44939106/article/details/124698298?spm=1001.2014.3001.5502
(1)查看本地镜像并运行
#1、查看镜像命令,没有镜像
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
#2、 使用pull命令从官方hub.docker.com接取一个hello-world镜像案例
$ sudo docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest:
sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29
c51
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
# 3、再次查看镜像,含有hello-world镜像
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
#4、运行镜像建立容器命令(案例运行hello-world镜像)
$ sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working
correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker
Hub.
(amd64)
3. The Docker daemon created a new container from that image which
runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client,
which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container
with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
(2)删除镜像操作
#1、删除镜像
docker rmi -f 镜像ID #删除镜像
docker rmi -f 镜像ID 镜像ID 镜像ID #删除多个镜像
docker rmi $(docker images -aq) #删除全部镜像
webrx@us:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest ecac195d15af 3 days ago 516MB
hello-world latest feb5d9fea6a5 4 weeks ago 13.3kB
#2、删除镜像,如果容器正在运行,不能删除
webrx@us:~$ docker rmi feb5d9fea6a5
Error response from daemon: conflict: unable to delete feb5d9fea6a5
(must be forced) - image is being used by stopped container
1b3fc44c7409
3、容器的相关操作
# 1、查看所有容器,我这里是建立了两个容器
$sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f3928c14489 f "/hello" 32 seconds ago Exited (0) 32 seconds ago relaxed_murdock
34d9d4b87b63 f "/hello" About an hour ago Exited (0) About an hour ago ecstatic_shaw
#2、删除容器操作
#注意34d9d4b87b63是我的容器ID 上面查询容器时显示出来的容器ID
$sudo docker rm 34d9d4b87b63
#删除之后再次查看所有容器
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f3928c14489 f "/hello" 4 minutes ago Exited (0) 4 minutes ago relaxed_murdock
#可以看到容器已经被删除