Docker的基础命令
注意,在docker的命令中,如果需要输入整个的英文单词,就是--
,单独的一个字母就是-
Docker帮助命令
shell
docker version #显示docker的版本信息
docker info #显示docker系统信息,包括镜像和容器的信息
docker command --help #帮助命令
#例如:docker images --help
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# 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
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
帮助文档:docker | Docker Docs
Docker 镜像命令
当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。
- docker images 的命令
shell
#例如:docker images --help
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# 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
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
- 查询全部镜像
shell
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/portainer/portainer latest 5f11582196a4 15 months ago 287 MB
#REPOSITORY 镜像的仓库源
#TAG 标签【版本】
#IMAGE ID 镜像的id
#CREATED 创建的时间
#SIZE 大小
- 搜素镜像
shell
# docker search [镜像名]
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--help Print usage
--limit int Max number of search results (default 25)
--no-index Don't truncate output
--no-trunc Don't truncate output
举例
shell
docker search mysql #相当于从docker hub上去搜索,与百度去docker相同
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 14899 [OK]
docker.io docker.io/mariadb MariaDB Server is a high performing open s... 5689 [OK]
docker.io docker.io/phpmyadmin phpMyAdmin - A web interface for MySQL and... 953 [OK]
docker.io docker.io/percona Percona Server is a fork of the MySQL rela... 625 [OK]
- 拉取镜像
shell
# docker pull [镜像名]
docker pull mysql # 拉取镜像【下载】,默认是最新版本[latest],也可以指定版本
- 删除惊醒
shell
docker rmi [镜像名|镜像id]
Docker 容器命令
- 运行容器【以镜像名运行容器】
shell
# docker run [可选参数] image
#参数说明
--name="Name" 容器的名字 例如tomcat01...
--d 后台的方式运行
-it 交互的方式运行
-p 指定容器的端口 -p 8080:8080
-p 主机端口:容器端口---------------------------------常用
-p 容器端口
-P 随机指定端口
#举例
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker run -it centos /bin/bash # 测试启动并进入容器
[root@e0ccea9f0acf /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@e0ccea9f0acf /]# exit # 停止并退出容器
- 列出正在运行的所有容器
shell
# docker ps
[root@iZf8zhsqf64x47n1tpdy6oZ /]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all #Show all containers (default shows just running)
-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
- exit退出
shell
exit 停止退出
快捷按键Ctrl + P + Q 不停止退出
- 删除容器
shell
docker rm 容器id # 不能删除正在运行的,docker rm -f 强制删除
- 启动停止容器
shell
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前的容器
docker kill 容器id # 强制停止当前的容器
常用其他的命令
- 后台启动容器
shell
# docker run -d 镜像名
# docker ps 发现centos停止了
# 常见的坑,docker容器使用后台运行,就必须要存在一个前台进程 ,如果docker 发现没有应用,就会自动停止
# 例如,如果只安装了一个nginx,发现自己没有提供服务,就会立刻停止,就是因为没有一个前台的程序
- 查看日志命令
shell
docker logs --help
docker logs -tf --tail [number] [容器id]
# tf显示日志
# --tail要显示日志的条数
- 查看容器中进程信息ps
shell
# docker top 容器id
- 查看镜像的元数据
shell
docker inspect 容器id
- 进入当前正在运行的容器
shell
#通过后台方式运行容器时,需要进入容器,修改配置
方式一:
docker exec -it 容器id #进入容器开启一个新的终端
方式二:
docker attach 容器id #进入容器正在执行的终端,不会启动新的进程
- 从容器内拷贝文件到主机上
shell
docker cp 容器id:容器内路径
# 测试
#查看当前正在运行的容器
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfb7cef7d1e4 centos "/bin/bash" 10 minutes ago Up 10 minutes naughty_meninsky
af3bc99f4a47 portainer/portainer "/portainer" 2 days ago Up 26 hours 8000/tcp, 9443/tcp, 0.0.0.0:9000->9000/tcp portainer
#进入容器内
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker attach bfb7cef7d1e4
[root@bfb7cef7d1e4 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@bfb7cef7d1e4 /]# cd /home
[root@bfb7cef7d1e4 home]# ls
#创建一个文件
[root@bfb7cef7d1e4 home]# touch test.java
[root@bfb7cef7d1e4 home]# ls
test.java
[root@bfb7cef7d1e4 home]# exit
exit
#复制容器内的文件到主机上
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker cp bfb7cef7d1e4:/home/test.java /home
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# ls
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# cd /home
[root@iZf8zhsqf64x47n1tpdy6oZ home]# ls
test.java
[root@iZf8zhsqf64x47n1tpdy6oZ home]#