Docker入门笔记
Docker
获取镜像
docker image pull
$ docker pull ubuntu:18.04
查看镜像
docker image ls
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
openzipkin/zipkin latest 9b4acc3eb019 2 months ago 150MB
ubuntu 18.04 2c047404e52d 3 months ago 63.3MB
# 或者
$ docker images
# 过滤出ubuntu的镜像
$ docker images ubuntu
给镜像打标签 docker tag
# 打标签
$ docker tag ubuntu:18.04 myubuntu:18.04
# 再查出来
# 注意:myubuntu 和 ubuntu 镜像id是一样的!!!myubuntu镜像只是在2c047404e52d镜像上打个标签而已
$ docker images *ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 18.04 2c047404e52d 3 months ago 63.3MB
ubuntu 18.04 2c047404e52d 3 months ago 63.3MB
查看详细信息 docker inspect
$ docker image inspect ubuntu:18.04
[
{
"Id": "sha256:2c047404e52d7f17bdac4121a13cd844447b74e13063f8cb8f8b314467feed06",
"RepoTags": [
"myubuntu:18.04",
"ubuntu:18.04"
],
"RepoDigests": [
"ubuntu@sha256:fd25e706f3dea2a5ff705dbc3353cf37f08307798f3e360a13e9385840f73fb3"
],
"Parent": "",
"Comment": "",
"Created": "2020-11-25T22:25:17.102901455Z",
"Container": "360f37005f8aa084aa4a0de8a51b991a359599a40637783b3ceb0ffbe3a78d71",
// ........
}
]
查看镜像历史 docker history
$ docker history ubuntu:18.04
IMAGE CREATED CREATED BY SIZE COMMENT
2c047404e52d 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 3 months ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 3 months ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:6ef542de9959c3061… 63.3MB
搜寻镜像
docker search
$ docker search --filter=is-official=true nginx
$ docker search --filter=stars=4 tensorflow
删除镜像
根据标签删除镜像 docker image rm
# 删除 myubuntu,
# ubuntu 标签的镜像还引用相同镜像id的镜像
# 所以只会移除 myubuntu 标签
$ docker image rm myubuntu:18.04
Untagged: myubuntu:18.04
# 删除 ubuntu
# 此时已经没有其他标签引用了
# 所以会彻底删除
$ docker image rm ubuntu:18.04
Untagged: ubuntu:18.04
Untagged: ubuntu@sha256:fd25e706f3dea2a5ff705dbc3353cf37f08307798f3e360a13e9385840f73fb3
Deleted: sha256:2c047404e52d7f17bdac4121a13cd844447b74e13063f8cb8f8b314467feed06
Deleted: sha256:9459b6a89846db0723e467610b841e6833dbb2aae6133319a91f2f70c388afac
Deleted: sha256:9a9311f7fcddf94f7476ce89f9703e8360e8cf347ef486a280735f5cf98888cd
Deleted: sha256:b43408d5f11b7b2faf048ae4eb25c296536c571fb2f937b4f1c3883386e93d64
根据镜像id删除镜像 docker image rm
# 查看一下,目前 myubuntu 和 ubuntu 都是相同的镜像id
$ docker image ls *ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 2c047404e52d 3 months ago 63.3MB
myubuntu 18.04 2c047404e52d 3 months ago 63.3MB
# 尝试用镜像id删除
$ docker image rm 2c047404e52d
Error response from daemon: conflict: unable to delete 2c047404e52d (must be forced) - image is referenced in multiple repositories
# 先根据镜像标签删一个
$ docker image rm myubuntu:18.04
Untagged: myubuntu:18.04
# 再用镜像id删,这次就成功了
$ docker image rm 2c047404e52d
Untagged: ubuntu:18.04
Untagged: ubuntu@sha256:fd25e706f3dea2a5ff705dbc3353cf37f08307798f3e360a13e9385840f73fb3
Deleted: sha256:2c047404e52d7f17bdac4121a13cd844447b74e13063f8cb8f8b314467feed06
Deleted: sha256:9459b6a89846db0723e467610b841e6833dbb2aae6133319a91f2f70c388afac
Deleted: sha256:9a9311f7fcddf94f7476ce89f9703e8360e8cf347ef486a280735f5cf98888cd
Deleted: sha256:b43408d5f11b7b2faf048ae4eb25c296536c571fb2f937b4f1c3883386e93d64
删除虚悬镜像 docker image prune
$ dokcer image prune
创建镜像
docker commit
# 基于ubuntu镜像启动一个容器,并进入到容器的bash
$ docker run -it ubuntu:18.04 /bin/bash
# 在容器中创建一个测试文件,然后退出容器,记住容器的 id 5f470c986c61
root@5f470c986c61:/# touch test.txt
root@5f470c986c61:/# exit
exit
# 通过提交容器创建镜像
$ docker container commit -m "add test file" -a "some info" 5f470c986c61
sha256:ed81f7211c7f6e22a02f2f7a8425537bfef3359db2fad870cdc62240fe146f69
docker import
注:了解即可
# 基于本地模板导入
# 到 https://wiki.openvz.org/Download/template/precreated 下载OpenVZ提供的模板
# 下面 “import - ubuntu:18:04” 中的-是表示从标准输入流读取文件信息
$ cat ubuntu-18.04-x86-minimal.tar.gz | docker container import - ubuntu:18:04
docker build
新建一个mypython3
目录,进入目录然后创建一个Dockerfile
$ mkdir mypython3
$ cd mypython3
$ vim Dockerfile
然后写一个简单的Dockerfile文件:
FROM debian:stretch-slim
LABEL version="1.0" maintainer="caibh"
RUN apt-get update && \
apt-get install -y python3 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
然后构建镜像:
caibh@book:~/mypython3$ docker build -t mypython3:1.0 .
导入导出
docker save
# 导出镜像到本地文件系统
$ docker save -o ubuntu_18.04.tar ubuntu:18.04
$ ll
总用量 64120
-rw------- 1 caibh caibh 65652224 2月 25 17:59 ubuntu_18.04.tar
docker load
# 删除镜像
$ docker image rm ubuntu:18.04
# 从本地导入镜像
$ docker load -i ubuntu_18.04.tar
b43408d5f11b: Loading layer [==================================================>] 65.62MB/65.62MB
23135df75b44: Loading layer [==================================================>] 15.87kB/15.87kB
fe6d8881187d: Loading layer [==================================================>] 3.072kB/3.072kB
Loaded image: ubuntu:18.04
# 查看导出的镜像
$ docker image ls ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 2c047404e52d 3 months ago 63.3MB