### 命令操作
#### 启动/关闭
```shell
#启动docker
systemctl start docker
#关闭docker
systemctl stop docker
#重启守护进程
systemctl daemon-reload
#重启docker
systemctl restart docker
```
#### 检索镜像
```shell
#搜索镜像
docker search 镜像名:tag
```
#### image命令
```shell
docker image --help
#可使用的命令
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
-----------------------------------------------------------------------------------------
#拉入(下载)镜像 注:若tag只填写大版本(比如2)则会下载大版本中最新的镜像
docker image pull 镜像名:tag = docker pull 镜像名:tag
#列出镜像
docker image ls = docker images
#删除镜像
docker image rm 镜像Id = docker rmi 镜像Id
```
#### container命令
```shell
docker container --help
#可使用的命令
attach Attach local standard input, output, and error streams to a running container
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
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
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
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
wait Block until one or more containers stop, then print their exit codes
------------------------------------------------------------------------------------
#查看容器列表(正在运行)
docker container ls = docker ps
#查看所有容器列表(包括已停止运行)
docker container ls -a = docker ps -a
#删除容器
docker container rm 容器Id = docker rm 容器Id
#运行容器 参数(--name 命名容器名称 -it 交互式运行并分配伪终端 -p 端口 -d 后台运行 -v 数据卷)
docker container run --name 容器名称 -d -p 宿主机端口:容器端口 -v 宿主机目录:容器目录 镜像名
#从交互式运行容器中退出
1. exit (容器停止并退出)
2. ctrl+P+Q (容器不停止并退出)
#启动容器
docker container start 容器ID = docker start 容器ID
#重启容器
docker restart 容器ID
#停止容器(优雅)
docker container stop 容器ID = docker stop 容器ID
#停止容器(立即)
docker container kill 容器ID = docker kill 容器ID
#查看容器日志
docker container logs -f 容器ID = docker logs -f 容器Id
#查看容器详细信息
docker container inspect 容器名称 = docker inspect 容器名称
#进入容器
docker container exec -it 容器ID = docker exec -it 容器Id
#拷贝文件
docker container cp 容器Id:文件路径 宿主机路径 = docker cp 容器Id:文件路径 宿主机路径
#提交新镜像
docker container commit -m="描述" -a="提交者" 容器ID 要创建新的镜像名:tag
```
#### DockerFile
```dockerfile
FROM 继承文件名
MAINTAINER 作者信息
ENV 设置环境变量例如:JAVA_HOME /usr/local/jdk
VOLUME ["数据卷1","数据卷2","数据卷3",......] 或 数据卷1
WORKDIR 指定创建容器后,终端默认登录进来的工作目录
ADD 将宿主机目录下文件拷贝到镜像中,并自动处理URL和解压tar压缩包
COPY 宿主机目录 镜像目录 或 ["宿主机目录","镜像目录"]
RUN 构建时所需命令(shell)
EXPOSE 暴露端口
CMD 容器启动时执行的命令 多个CMD时只执行最后一个,并且会被docker run 指定的参数替换
ENTRYPOINT 与CMD不同在于 命令可追加
ONBUILD 写在父DockerFile文件中,当子DockerFile构建时会出发此命令
#构建镜像
docker image build -f dockerFile文件路径 -t 镜像名称:tag .
```