Docker的容器、镜像、镜像仓库中⼼操作命令⼤全
1, docker images
2, docker run -it image_name /bin/bash
3, docker ps -a
4, docker start/restart/stop container_id
5, docker rmi image_name
6, docker rm container_id
7, docker pull image_name:tag
8, docker export container_id > xxx.tar
9, docker import xxx.tar new_image_name:new_tag
10, docker attach container_id
11, docker tag image_name:tag new_image_name:new_tag
12, docker login
13, docker push ⽤户名/image_name
本篇⽂章介绍 Docker 的操作参数,包括针对容器(container)、镜像(image)和镜像仓库中⼼(registry)的操作命令。
Tips:本篇⽂章来⾃于专栏。
1. 容器操作
Docker 和容器相关的常⽤的操作命令如下:
run
docker run ⽤来通过镜像启动⼀个容器。这个可以算是操作 docker 容器的核⼼命令了,参数及其丰富,多达 91 个参数,使⽤⼿册如下:
[root@emr-header-1 ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] []
Run a command in a new container
⽐如我们启动⼀个 centos 的镜像
[root@xxx ~]# docker run -ti centos:latest /bin/bash
[root@b16716790a3f /]#
其中 centos:latest 就是 Usage 中的 IMAGE,COMMAND 就是 /bin/bash,然后 -ti 分配⼀个终端⽤于交互式输⼊。
我们下⾯主要介绍⼏个重要的参数。
--interactive 等同于 -i ,接受 stdin 的输⼊;
--tty 等同于 -t,分配⼀个 tty,⼀般和 i ⼀起使⽤;
--name 给容器设置⼀个名字;
--add-host 给容器设置 hosts ⽂件,格式 host:ip;
--env 环境变量设置;
--expose 暴露端⼝;
--hostname 设置容器的主机名;
--link 容器⽹络相关,和其他的 container 连接;
--cpu-quota 设置 CPU 限制;
--memory 设置容器可以使⽤的内存限制。
attach
docker attach 让我们可以进⼊到⼀个运⾏着的容器的内部,这个命令的原理是给⼀个正在运⾏的容器分配⼀个 stdin、stdout 和stderr。
[root@xxx ~]# docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string  Override the key sequence for detaching a container
busybox安装
--no-stdin            Do not attach STDIN
--sig-proxy            Proxy all received signals to the process (default true)
需要注意的是,如果 docker attach 之后要退出的话,不能使⽤ exit,使⽤ exit 原容器也会退出。我们可以使⽤ Ctrl + C 的⽅式退出。exec
docker exec 命令也可以达到 attach 的⽬的。exec 命令的⽤处是在⼀个运⾏着的容器⾥⾯执⾏⼀个命令。关于这个命令的原理其实很简单,在 Linux 内核层⾯,相当于 fork 了⼀个进程,然后这个进程设置和容器相同的 NameSpace。如果我们 OPTIONS 指定 -ti ,那么我们就可以进⼊到⼀个运⾏着的容器⾥⾯执⾏命令了。因为这个是⼀个 fork 出来的进程,所以可以 exit。
[root@emr-header-1 ~]# docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND []
Run a command in a running container
Options:
-d, --detach              Detached mode: run command in the background
--detach-keys string  Override the key sequence for detaching a container
-e, --env list            Set environment variables
-i, --interactive          Keep STDIN open even if not attached
--privileged          Give extended privileges to the command
-t, --tty                  Allocate a pseudo-TTY
-u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string      Working directory inside the container
ps
docker ps 可以⽤来列出所有在运⾏的容器的信息,同时⽀持⼀些类似 filter 的参数。
[root@xxx ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all            Show all containers (default shows just running)
-f, --filter filter  Filter output based on conditions provided
--format string  Pretty-print containers using a Go template
-n, --last int        Show n last created containers (includes all states) (default -1)
-l, --latest          Show the latest created container (includes all states)
--no-trunc        Don't truncate output
-q, --quiet          Only display numeric IDs
-
s, --size            Display total file sizes
kill
docker kill ⽤来 kill ⼀个或者⼀组 container。
[root@xxx ~]# docker kill --help
Usage: docker kill [OPTIONS] CONTAINER []
Kill one or more running containers
Options:
-s, --signal string  Signal to send to the container (default "KILL")
logs
docker logs ⽤来获取 docker 的 log。
[root@xxx ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details        Show extra details provided to logs
-f, --follow        Follow log output
--since string  Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
--tail string    Number of lines to show from the end of the logs (default "all")
-t, --timestamps    Show timestamps
--until string  Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
top
docker top 这个命令有时候⽐较有⽤,我们想看⼀下运⾏这个容器在宿主机上⾯是那个进程就可以使⽤这个命令,毕竟 container 的本质就是⼀个进程,这个我们后⾯会细说。
[root@xxx ~]# docker top --help
Usage: docker top CONTAINER [ps OPTIONS]
Display the running processes of a container
2. 镜像操作
和镜像相关的常⽤的操作命令如下:
images:列出本地所有的镜像;
build:通过 Dockerfile build 出镜像;
commit:将容器中的所有改动⽣成新的镜像;
history:查看镜像的历史;
save:将镜像保存成 tar 包;
import:通过 tar 包导⼊新的镜像;
load:通过 tar 包或者标志输⼊导⼊镜像;
rmi:删除本地镜像;
tag:给镜像打 tag。
images
docker images 会显⽰本地所有的⾮隐藏镜像,默认会将中间依赖镜像进⾏隐藏。
[root@xxx ~]# docker images
REPOSITORY          TAG                IMAGE ID            CREATED            SIZE
busybox            1-musl              ff04c2bddacb        3 days ago          1.44MB
busybox            1-glibc            ad06ec8ab37b        3 days ago          5.2MB
busybox            1-uclibc            b534869c81f0        3 days ago          1.22MB
busybox            latest              b534869c81f0        3 days ago          1.22MB
busybox            1.24-glibc          54df49495ae4        3 years ago        4.18MB
busybox            1-ubuntu            d34ea343a882        3 years ago        4.35MB
busybox            1.21-ubuntu        d34ea343a882        3 years ago        4.35MB
busybox            1.21.0-ubuntu      d34ea343a882        3 years ago        4.35MB
busybox            1.23                a84c36ecc374        4 years ago        1.1MB
busybox            1.23.2              a84c36ecc374        4 years ago        1.1MB
这是 docker images 的默认显⽰,但是有时候我们需要显⽰更多的信息⽐如 Digest,或者过滤掉⼀些镜像,那么我们可以通过添加参数来实现。我们看⼀下 docker images 的完整的功能。
[root@xxx ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all            Show all images (default hides intermediate images)
--digests        Show digests
-f, --filter filter  Filter output based on conditions provided
--format string  Pretty-print images using a Go template
--no-trunc        Don't truncate output
-q, --quiet          Only show numeric IDs
all 参数显⽰所有镜像,包括中间依赖镜像;
digest 显⽰ digest;
filter 对镜像进⾏过滤;
no-trunc 默认会对某些输出列进⾏截断展⽰,该参数将全量展⽰。
quiet 只展⽰镜像的数字 ID。
format 参数使⽤⼀种 Go 模板的形式输出,简单来说就是指定输出的列。这么描述不太直观,我们可以看⼀个简单的例⼦如下,其中的 ID 和 Repository 就是指定的输出的列。
[root@xxx ~]# docker images --format "{{.ID}}: {{.Repository}}"
ff04c2bddacb: busybox
ad06ec8ab37b: busybox
b534869c81f0: busybox
b534869c81f0: busybox
除了 ID 和 Repository,Docker ⽀持的全部的列如下:
Placeholder Description
.ID镜像 ID
.Repository镜像 repo,其实就是镜像名称
.Tag镜像 tag
.Digest镜像 digest
.CreatedSince镜像创建之后多长时间
.CreatedAt镜像的创建时间
.Size镜像的磁盘⼤⼩
build
在 Docker 中我们可以通过⼀个 Dockerfile,使⽤ docker build 构建出镜像。这⼀块我们后⾯将会有专门的⼩节来展开,这⾥暂时先略过。
commit
上⾯说到我们可以通过⼀个 Dockerfile 构建出镜像,但是有时候我们在使⽤过程中对容器(container)做了⼀些改动,⽐如安装了⼀些依赖包。我们想把这些改动保存下来形成新的镜像,同时不想再去编写 Dockerfile,或者之前的 Dockerfile 我们没有。那么这时候我们就可以通过 docker commit 将⼀个 container 的环境持久成镜像。我们⾸先看⼀下 docker commit 的使⽤规范说明。
[root@xxx ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team>")
-c, --change list      Apply Dockerfile instruction to the created image
-m, --message string  Commit message
-p, --pause            Pause container during commit (default true)
如果要对现在的⼀个 container 进⾏ commit 操作,我们⾸先可以通过 docker ps 到我们要执⾏ commit 操作的 container 的 id。
[root@xxx ~]# docker ps
CONTAINER ID        IMAGE              COMMAND            CREATED              STATUS              PORTS              NAMES
d7bb841d9811        legendtkl:v2.1  "/bin/bash"        About a minute ago  Up About a minute  22/tcp              vigorous_chaplygin
然后执⾏ commit 。
[root@xxx ~]# docker commit d7bb841d9811 legendtkl:v2.2
然后再执⾏ docker images 就可以看到我们新 commit 出来的镜像了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。