docker学习笔记(2)

时间:2022-10-02 21:34:02

docker镜像及容器常用命令

一、docker镜像

  docker pull

# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
2a72cbf407d6: Already exists
eccc107d7abd: Pull complete
76aa3935d77c: Pull complete
Digest: sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Status: Downloaded newer image for nginx:latest
$ docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
bf5d46315322: Pull complete
9f13e0ac480c: Pull complete
e8988b5b3097: Pull complete
40af181810e7: Pull complete
e6f7c7e5c03e: Pull complete
Digest: sha256:147913621d9cdea08853f6ba9116c2e27a3ceffecf3b492983ae97c3d643fbbe
Status: Downloaded newer image for ubuntu:16.04

  运行容器 docker run

docker run -it --rm \
> ubuntu:16.04 \
> bash
Unable to find image 'ubuntu:16.04' locally
16.04: Pulling from library/ubuntu
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:16.04
root@d89a89ec3c03:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
root@d89a89ec3c03:/#

-it :这是两个参数,一个是 -i :交互式操作,一个是 -t 终端。我们这里打算进入
bash 执行一些命令并查看返回结果,因此我们需要交互式终端。

--rm :这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容
器并不会立即删除,除非手动 docker rm 。我们这里只是随便执行个命令,看看结果,
不需要排障和保留结果,因此使用 --rm 可以避免浪费空间。

ubuntu:16.04 :这是指用 ubuntu:16.04 镜像为基础来启动容器。

bash :放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 bash 。

进入容器后,我们可以在 Shell 下操作,执行任何所需的命令。这里,我们执行了 cat
/etc/os-release ,这是 Linux 常用的查看当前系统版本的命令,从返回的结果可以看到容器
内是 Ubuntu 16.04.4 LTS 系统。

最后我们通过 exit 退出了这个容器。

  

  列出镜像:docker image ls

# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 0849f1309c28 About an hour ago 109MB
its-xnetd 1.0 2087299be7b8 hours ago 198MB
fandf-image-base 1.0 26d35143484b hours ago 762MB
nginx latest 73acd1f0cfad hours ago 109MB
ubuntu 16.04 f975c5035748 days ago 112MB
ubuntu latest f975c5035748 days ago 112MB
ubuntu 17.10 1af812152d85 days ago .4MB
nginx <none> e548f1a579cf weeks ago 109MB
registry d1fd7d86a825 months ago .3MB
mysql 5.6. 15a5ee56ec55 months ago 299MB
127.0.0.1:/hello-world latest f2a91732366c months ago .85kB
hello-world latest f2a91732366c months ago .85kB
centos 7.4. 3afd47092a0e months ago 197MB
java latest d23bdf5b1b1b months ago 643MB

列表包含了 仓库名 、 标签 、 镜像 ID 、 创建时间 以及 所占用的空间 。

  删除镜像 docker rmi

# docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Deleted: sha256:73acd1f0cfadf6f56d30351ac633056a4fb50d455fd95b229f564ff0a7adecda
Deleted: sha256:660d894d7e1779b260ce69426dced9f1750deb8a6505f052f61a9876991e73e6
Deleted: sha256:97e86b3c85516c6f3c848ee0df11bebe95154a567046225f1cd3a690fd22687e

  docker commit 使用

注意: docker commit 命令除了学习之外,还有一些特殊的应用场合,比如被入侵后保存现
场等。但是,不要使用 docker commit 定制镜像,定制镜像应该使用 Dockerfile 来完成。

  启动一个nginx

# docker run --name webserver -d -p : nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
2a72cbf407d6: Already exists
eccc107d7abd: Pull complete
76aa3935d77c: Pull complete
Digest: sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Status: Downloaded newer image for nginx:latest
bc4c06e17114b3c21658d8f89edb37fc11627704e42a3efcb0654e170d9e94b3

docker run 命令如果本地仓库没有,会去远程仓库pull到本地

登陆ip:80

docker学习笔记(2)

这条命令会用 nginx 镜像启动一个容器,命名为 webserver ,并且映射了 80 端口,这样我
们可以用浏览器去访问这个 nginx 服务器。

  

docker exec -it webserver bash
x.htmlc4c06e17114:/# echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
root@bc4c06e17114:/# exit
exit

通过docker exec -it --name bash 获得一个可操作的shell

刷新浏览器

docker学习笔记(2)

二、容器

  新建并启动容器 docker run

例如,下面的命令输出一个 “Hello World”,之后终止容器。

  

# docker run ubuntu:14.04 /bin/echo 'Hello world'
Hello world

下面的命令则启动一个 bash 终端,允许用户进行交互。

# docker run -t -i ubuntu:14.04 /bin/bash
root@a7cf4956678d:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@a7cf4956678d:/# pwd
/
root@a7cf4956678d:/#

-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上, -i 则让容器的标准输入保持打开

当利用 docker run 来创建容器时,Docker 在后台运行的标准操作包括:
  1.检查本地是否存在指定的镜像,不存在就从公有仓库下载
  2.利用镜像创建并启动一个容器
  3.分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
  4.从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  5.从地址池配置一个 ip 地址给容器
  6.执行用户指定的应用程序
  7.执行完毕后容器被终止

查看容器 docker container ls

# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc4c06e17114 nginx "nginx -g 'daemon of…" 18 minutes ago Up 17 minutes 0.0.0.0:80->80/tcp webserver
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2

终止容器 docker container stop

# docker container stop bc4c06e17114
bc4c06e17114

查看所有容器(包括已停止的) docker container ls -a

# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7cf4956678d ubuntu:14.04 "/bin/bash" 2 minutes ago Exited (0) 28 seconds ago dreamy_dubinsky
eabc9ee2a377 ubuntu:14.04 "/bin/echo 'Hello wo…" 7 minutes ago Exited (0) 7 minutes ago jolly_shockley
ee73354b5ad9 ubuntu:14.04 "/bin/echo 'Hello wo…" 7 minutes ago Exited (0) 7 minutes ago cranky_varahamihira
bc4c06e17114 nginx "nginx -g 'daemon of…" 18 minutes ago Up 18 minutes 0.0.0.0:80->80/tcp webserver
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2

处于终止状态的容器,可以通过 docker container start 命令来重新启动。
此外, docker container restart 命令会将一个运行态的容器终止,然后再重新启动它。

进入容器

  某些时候需要进入容器进行操作,包括使用 docker attach 命令或 docker exec 命令,推荐
  大家使用 docker exec 命令

# docker run -dit ubuntu
be530e7c563cb33035ce3cc9e00d2e64f48adf319a1fe977c5d871d03f044eb5
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be530e7c563c ubuntu "/bin/bash" 9 seconds ago Up 7 seconds compassionate_pare
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2
[root@localhost ~]# docker exec -it be530e7c563c bash
root@be530e7c563c:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@be530e7c563c:/# pwd
/
root@be530e7c563c:/#

如果从这个 stdin 中 exit,不会导致容器的停止。这就是为什么推荐大家使用 docker exec 的原因。

  导出和导入容器

  docker export

# ls
anaconda-ks.cfg etcd-v3.2.10-linux-amd64.tar.gz
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be530e7c563c ubuntu "/bin/bash" 4 minutes ago Exited (0) 20 seconds ago compassionate_pare
a7cf4956678d ubuntu:14.04 "/bin/bash" 9 minutes ago Exited (0) 7 minutes ago dreamy_dubinsky
eabc9ee2a377 ubuntu:14.04 "/bin/echo 'Hello wo…" 14 minutes ago Exited (0) 14 minutes ago jolly_shockley
ee73354b5ad9 ubuntu:14.04 "/bin/echo 'Hello wo…" 14 minutes ago Exited (0) 14 minutes ago cranky_varahamihira
bc4c06e17114 nginx "nginx -g 'daemon of…" 25 minutes ago Exited (0) 5 minutes ago webserver
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2
[root@localhost ~]# docker export bc4c06e17114 > nginx.tar
[root@localhost ~]# ls
anaconda-ks.cfg etcd-v3.2.10-linux-amd64.tar.gz nginx.tar

docker import 导入容器

# cat nginx.tar | docker import - test/nginx:v1.0
sha256:777d7a2b2bf5b2ce8c66c0b344a453114cfb1bd1f75f1c3e5625d00f8b8cc35f
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/nginx v1.0 777d7a2b2bf5 15 seconds ago 107MB
nginx v2 0849f1309c28 2 hours ago 109MB
its-xnetd 1.0 2087299be7b8 15 hours ago 198MB
fandf-image-base 1.0 26d35143484b 15 hours ago 762MB
nginx latest 73acd1f0cfad 41 hours ago 109MB
ubuntu 16.04 f975c5035748 9 days ago 112MB
ubuntu latest f975c5035748 9 days ago 112MB
ubuntu 14.04 a35e70164dfb 9 days ago 222MB
ubuntu 17.10 1af812152d85 9 days ago 98.4MB

可以使用 docker container rm 来删除一个处于终止状态的容器

# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be530e7c563c ubuntu "/bin/bash" 9 minutes ago Exited (0) 6 minutes ago compassionate_pare
a7cf4956678d ubuntu:14.04 "/bin/bash" 15 minutes ago Exited (0) 13 minutes ago dreamy_dubinsky
eabc9ee2a377 ubuntu:14.04 "/bin/echo 'Hello wo…" 20 minutes ago Exited (0) 20 minutes ago jolly_shockley
ee73354b5ad9 ubuntu:14.04 "/bin/echo 'Hello wo…" 20 minutes ago Exited (0) 20 minutes ago cranky_varahamihira
bc4c06e17114 nginx "nginx -g 'daemon of…" 31 minutes ago Exited (0) 11 minutes ago webserver
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2
[root@localhost ~]# docker container rm be530e7c563c
be530e7c563c
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7cf4956678d ubuntu:14.04 "/bin/bash" 15 minutes ago Exited (0) 13 minutes ago dreamy_dubinsky
eabc9ee2a377 ubuntu:14.04 "/bin/echo 'Hello wo…" 21 minutes ago Exited (0) 20 minutes ago jolly_shockley
ee73354b5ad9 ubuntu:14.04 "/bin/echo 'Hello wo…" 21 minutes ago Exited (0) 21 minutes ago cranky_varahamihira
bc4c06e17114 nginx "nginx -g 'daemon of…" 31 minutes ago Exited (0) 11 minutes ago webserver
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2
[root@localhost ~]#

用 docker container ls -a 命令可以查看所有已经创建的包括终止状态的容器

docker container prune 可以删除所有停止的容器

# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7cf4956678d ubuntu:14.04 "/bin/bash" 15 minutes ago Exited (0) 13 minutes ago dreamy_dubinsky
eabc9ee2a377 ubuntu:14.04 "/bin/echo 'Hello wo…" 21 minutes ago Exited (0) 20 minutes ago jolly_shockley
ee73354b5ad9 ubuntu:14.04 "/bin/echo 'Hello wo…" 21 minutes ago Exited (0) 21 minutes ago cranky_varahamihira
bc4c06e17114 nginx "nginx -g 'daemon of…" 31 minutes ago Exited (0) 11 minutes ago webserver
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2
[root@localhost ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
a7cf4956678db31ee82e9adb0b88ad3a8771a5b0a4de7718bfe72d0688d9e2e8
eabc9ee2a37785f754b4571dc4cff286743f7d777d504ff133c6ae1404b64dd9
ee73354b5ad9ee137873537a1beae4e1988c0fe8fdfd19f7cd60eb7c4fdfd68a
bc4c06e17114b3c21658d8f89edb37fc11627704e42a3efcb0654e170d9e94b3 Total reclaimed space: 107B
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68d9a1146e82 nginx:v2 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp web2
[root@localhost ~]#