本文简要介绍Docker的基础命令,目的在于快速入门Dokcer,Docker的完整命令可以参考Docker官方手册。
0. 安装Docker
docker-ee 和 docker-ce
移除旧版的docker
yum remove docker*
配置docker-ce YUM仓库
# 方法1:
curl -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
# 方法2:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装docker-ce
安装最新版的docker-ce
yum -y install docker-ce
安装指定版本的docker-ce
# 查看所有版本的docker-ce
yum list docker-ce --showduplicates
# 安装指定版本的docker-ce
yum install docker-ce-<VERSION>
启动docker
# 启动|停止|查看状态
systemctl start|stop|status dokcer
docker [help|--help|-H|-h]
$ docker
Usage: docker COMMAND
...
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
docker COMMAND --help
$ docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
下图是Docker的命令结构图,可以帮助我们快速理解Docker基础命令的使用。
1. Docker环境信息
docker info 命令
docker info 命令用于查看Docker是否正确安装,正确安装则返回Docker的配置信息。
$ docker info
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 3
...
docker version 命令
docker version命令用于显示docker版本信息
# docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:20:16 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:23:58 2018
OS/Arch: linux/amd64
Experimental: false
2. 从Docker Registry中下载、上传镜像
Docker官方仓库Docker Hub中有大量的Docker镜像可供我们下载使用,对于国内的用户访问Docker Hub速度比较慢,可通过配置阿里云Docker镜像加速器来提高速度,配置方法如下:
/etc/docker/daemon.json
$ vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://13sxamgp.mirror.aliyuncs.com"]
}
docker pull命令
从Docker Rgeistry仓库中下载Images到本地主机
docker pull [OPTIONS] NAME[:TAG]
#从配置的默认仓库中拉取最新的busybox镜像
$ docker pull busybox
#从配置的默认仓库中拉取版本为7.4.1708的CentOS镜像
# docker pull centos:7.4.1708
#从指定的仓库中拉取镜像
$ docker pull registry.cn-hangzhou.aliyuncs.com/zhubiao/busybox:latest
docker images命令
从仓库拉取下来的镜像,我们可以使用 docker images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 8c811b4aec35 3 weeks ago 1.15MB
registry.cn-hangzhou.aliyuncs.com/zhubiao/busybox latest 8c811b4aec35 3 weeks ago 1.15MB
centos
docker push命令
将本地主机上的docker镜像上传到Docker Registry仓库中
3. 给镜像创建标签、删除镜像
docker tag命令
同一个镜像的不同版本,通常使用Tag标签来区分,创建镜像的标签语法是:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
#以busybox:latest为源,创建busybox:1.28.4
$ docker images busybox
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 8c811b4aec35 3 weeks ago 1.15MB
$
$ docker tag busybox:latest busybox:1.28.4
$
$ docker images busybox
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox 1.28.4 8c811b4aec35 3 weeks ago 1.15MB
busybox latest 8c811b4aec35 3 weeks ago 1.15MB
docker rmi命令
删除本地主机的镜像文件
docker rmi [options] IMAGE...
-f
# 删除镜像busybox:latest
$ docker rmi busybox:latest
4. 镜像的导出、导入
docker save 命令将镜像导出为tar文件,也可以使用 docker load
docker save命令
将Images文件导出为tar格式的打包文件
docker save REPOSITORY[:TAG] -o IMAGE.tar
将busybox导出为busybox.tar文件
$ docker save busybox:1.28.4 -o busybox.tar
$ ls busybox.tar
busybox.tar
docker load命令
将打包的镜像文件导入为Image
docker load -i IMAGE.tar
# 将现有的镜像文件busybox:1.28.4删除,然后将上面所导出的busybox.tar文件导入
$ docker rmi busybox:1.28.4
$ docker load -i ./busybox.tar
Loaded image: busybox:1.28.4
5. 容器生命周期管理
docker run 命令
当本地主机有镜像以后,我们就可以是用 docker run
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
-i
-t :给启动的容器分配一个伪终端, -it
--name
-v
COMMAND:启动容器后,在容器中执行的命令
#使用镜像centos:7.4.1708创建容器,并命名为test,由于使用了-it选项,并在容器中执行了bash命令。所以容器启动就可也与容器进行交互式操作。
$ docker run -it --name test centos:7.4.1708 /bin/bash
[root@c11fc849b0a4 /]#
#同时按下Ctrl + p + q快捷键退出容器,同时保持容器的运行
[root@c11fc849b0a4 /]# $
$
docker ps 命令
用于查看容器信息,比如容器的ID、名字、运行状态、依赖的镜像等,默认不加选项只列出正在运行的容器。
docker ps
-a
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bdc004a443cc busybox "echo 'Hello World!'" 17 seconds ago Exited (0) 15 seconds ago hardcore_noyce
c11fc849b0a4 centos:7.4.1708 "/bin/bash" 16 minutes ago Up 16 minutes test
docker attach 命令
docker attach
$ docker attach test
[root@c11fc849b0a4 /]#
docker start | stop | restart 命令
对容器进行启动、停止、重启操作
#停止test容器,这里我们用容器名test,也可以使用CONTAINER ID
$ docker stop test
test
#启动容器
$ docker start test
test
#重启容器
$ docker restart test
docker commit 命令
对正在运行的容器,我们可能已经在里面安装了一些程序,修改了配置文件,可以使用 docker commit 命令将其固化下来,生成镜像文件。但制作镜像,通常不使用该方法,建议使用 docker build
docker commit [options] CONTAINER [REPOSITORY[:TAG]]
#将test容器制作成镜像
$ docker commit test
#查看制作好镜像
$ docker images centos
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.4.1708 3afd47092a0e 7 months ago 197MB
docker inspect 命令
-f
docker inspect [options] CONTAINER|IMAGE
-f STRING
#查看容器的信息
$ docker inspect test
[
{
"Id": "c11fc849b0a4d4c5f8f64d3a821b401bf90ea4d664f3ca840a6c093b27717d05",
"Created": "2018-06-16T03:27:24.713206283Z",
"Path": "/bin/bash",
...
},
...
]
# 查看容器的地址
$ docker inspect -f {{.NetworkSettings.IPAddress}} test
172.17.0.2
# 查看镜像的指定信息
$ docker inspect -f {{.RepoTags}} centos:7.4.1708
[centos:7.4.1708]
docker rm 命令
用于删除1个或多个容器,
docker rm
-f
$ docker rm -f test
test
Andraw|朱标