我没有使用高权限,所以后边都要加sudo,你如果和我一起练习的话,注意一下。
1.查看状态
sudo systemctl status docker
2.查看镜像
kt@kt-SYS-4028GR-TR2:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kt@kt-SYS-4028GR-TR2:~$ sudo docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker 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 Format output using a custom template:
'table': Print output in table format with column headers
(default)
'table TEMPLATE': Print output in table format using the given Go
template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more information
about formatting output with templates
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
--tree List multi-platform images as a tree (EXPERIMENTAL)
3.search
kt@kt-SYS-4028GR-TR2:~$ sudo docker search mongo
NAME DESCRIPTION STARS OFFICIAL
mongo MongoDB document databases provide high avai… 10469 [OK]
circleci/mongo CircleCI images for MongoDB 13
litmuschaos/mongo 1
corpusops/mongo https://github.com/corpusops/docker-images/ 0
uselagoon/mongo 0
kt@kt-SYS-4028GR-TR2:~$ docker search --help
Usage: docker search [OPTIONS] TERM
Search Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results
--no-trunc Don't truncate output
4.pull,最好指定版本,我认为的。注意系统的对应。打开
docker pull mongo:latest
5删除
docker rmi -f ID
6.运行
docker run 镜像名或ID
重要参数:
--name='' 容器名,自定义。
-d 后台。
-it 交互方式
-p 指定端口或映射
主机:容器
容器
-P 随机端口
测试:
kt@kt-SYS-4028GR-TR2:~$ sudo docker run -it contos /bin/bash
Unable to find image 'contos:latest' locally
docker: Error response from daemon: pull access denied for contos, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
kt@kt-SYS-4028GR-TR2:~$ sudo docker run -it centos /bin/bash
[root@5ba6a94f87fa /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@5ba6a94f87fa /]# exit
exit
kt@kt-SYS-4028GR-TR2:~$
7.查看 仓库
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103ec0f1f3cb mongo "docker-entrypoint.s…" 2 minutes ago Exited (0) About a minute ago stupefied_clarke
5ba6a94f87fa centos "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago pedantic_newton
326205a8bc00 5d0d "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago goofy_pike
kt@kt-SYS-4028GR-TR2:~$ docker ps --help
Usage: docker ps [OPTIONS]
List containers
Aliases:
docker container ls, docker container list, docker container ps, docker ps
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Format output using a custom template:
'table': Print output in table format with column headers
(default)
'table TEMPLATE': Print output in table format using the given Go
template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more information
about formatting output with templates
-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 container IDs
-s, --size Display total file sizes
8.容器操作
docker start ID.
restart ID.
stop ID.
kill ID.
kt@kt-SYS-4028GR-TR2:~$ sudo docker run -it centos /bin/bash
[root@5d0d29900adb /]# exit
exit
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d0d29900adb centos "/bin/bash" 14 seconds ago Exited (0) 10 seconds ago practical_galois
1f7192baecc2 centos "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago wonderful_poincare
1f97f4632d9f centos "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago eloquent_shtern
kt@kt-SYS-4028GR-TR2:~$ sudo docker start 5d0d
5d0d
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d0d29900adb centos "/bin/bash" About a minute ago Up 10 seconds practical_galois
1f7192baecc2 centos "/bin/bash" 5 minutes ago Exited (0) 4 minutes ago wonderful_poincare
1f97f4632d9f centos "/bin/bash" 7 minutes ago Exited (0) 6 minutes ago eloquent_shtern
kt@kt-SYS-4028GR-TR2:~$ sudo docker stop 5dod
Error response from daemon: No such container: 5dod
kt@kt-SYS-4028GR-TR2:~$ sudo docker stop 5d0d
5d0d
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d0d29900adb centos "/bin/bash" About a minute ago Exited (0) 3 seconds ago practical_galois
1f7192baecc2 centos "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago wonderful_poincare
1f97f4632d9f centos "/bin/bash" 7 minutes ago Exited (0) 7 minutes ago eloquent_shtern
kt@kt-SYS-4028GR-TR2:~$
删除容器
docker rm ID
docker rm -f $(docker ps -aq)
9.其他操作:
kt@kt-SYS-4028GR-TR2:~$ sudo docker run -d centos
e31697546e141e3642cbdeeba977c3a789ffe5728a5ef5bf775f93a7ac74d1bf
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e31697546e14 centos "/bin/bash" 9 seconds ago Exited (0) 8 seconds ago clever_feynman
5d0d29900adb centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago practical_galois
1f7192baecc2 centos "/bin/bash" 8 minutes ago Exited (0) 7 minutes ago wonderful_poincare
1f97f4632d9f centos "/bin/bash" 9 minutes ago Exited (0) 9 minutes ago eloquent_shtern
kt@kt-SYS-4028GR-TR2:~$ sudo docker restart e31
e31
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e31697546e14 centos "/bin/bash" 39 seconds ago Exited (0) 2 seconds ago clever_feynman
5d0d29900adb centos "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago practical_galois
1f7192baecc2 centos "/bin/bash" 8 minutes ago Exited (0) 7 minutes ago wonderful_poincare
1f97f4632d9f centos "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago eloquent_shtern
kt@kt-SYS-4028GR-TR2:~$
容器在启动后,必须要有一个前台进程,如果没有,就自动退出。
如:nginx,如果没有提供服务就会退出。
10.查看容器的进程信息
docker top ID
11.查看容器的信息
docker inspect ID
kt@kt-SYS-4028GR-TR2:~$ sudo docker top e31
Error response from daemon: container e31697546e141e3642cbdeeba977c3a789ffe5728a5ef5bf775f93a7ac74d1bf is not running
kt@kt-SYS-4028GR-TR2:~$ sudo docker inspect e31
[
{
"Id": "e31697546e141e3642cbdeeba977c3a789ffe5728a5ef5bf775f93a7ac74d1bf",
"Created": "2025-01-06T01:49:41.490204538Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
12.进入正在运行的容器
方式一:进入后开启一个新的终端。
docker exec -it ID /bin/bash
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e31697546e14 centos "/bin/bash" 17 minutes ago Exited (0) 17 minutes ago clever_feynman
5d0d29900adb centos "/bin/bash" 21 minutes ago Exited (0) 19 minutes ago practical_galois
1f7192baecc2 centos "/bin/bash" 25 minutes ago Exited (0) 24 minutes ago wonderful_poincare
1f97f4632d9f centos "/bin/bash" 27 minutes ago Exited (0) 27 minutes ago eloquent_shtern
kt@kt-SYS-4028GR-TR2:~$ sudo docker rm -f $(sudo docker ps -aq)
e31697546e14
5d0d29900adb
1f7192baecc2
1f97f4632d9f
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
kt@kt-SYS-4028GR-TR2:~$ sudo docker run -it centos /bin/bash
[root@1320d092cdf4 /]# exit
exit
kt@kt-SYS-4028GR-TR2:~$ sudo docker restart 132
132
kt@kt-SYS-4028GR-TR2:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1320d092cdf4 centos "/bin/bash" 37 seconds ago Up 8 seconds suspicious_swirles
kt@kt-SYS-4028GR-TR2:~$ sudo docker exec -it 132 /bin/bash
[root@1320d092cdf4 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@1320d092cdf4 /]#
方式2:使用前查看命令帮助。进入容器正在执行的。
docker attach ID
kt@kt-SYS-4028GR-TR2:~$ sudo docker attach 132
[root@1320d092cdf4 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@1320d092cdf4 /]#
13.文件的cp.内外互考。注意。当前目录下,用.代替。别忘记了。
kt@kt-SYS-4028GR-TR2:~$ cd Desktop
kt@kt-SYS-4028GR-TR2:~/Desktop$ touch kt.txt
kt@kt-SYS-4028GR-TR2:~/Desktop$ ls
kt.txt ls
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker cp kt.txt 132:/home
Successfully copied 1.54kB to 132:/home
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker attach 132
You cannot attach to a stopped container, start it first
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker attach 132
You cannot attach to a stopped container, start it first
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1320d092cdf4 centos "/bin/bash" 17 minutes ago Exited (0) 3 minutes ago suspicious_swirles
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker restart 132
132
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker attach 132
[root@1320d092cdf4 /]# cd home
[root@1320d092cdf4 home]# ls
aa.txt kt.txt
[root@1320d092cdf4 home]# exit
exit
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker cp 132:/home/aa.txt
"docker cp" requires exactly 2 arguments.
See 'docker cp --help'.
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
kt@kt-SYS-4028GR-TR2:~/Desktop$ sudo docker cp 132:/home/aa.txt .
Successfully copied 1.54kB to /home/kt/Desktop/.
kt@kt-SYS-4028GR-TR2:~/Desktop$ ls
aa.txt kt.txt ls
kt@kt-SYS-4028GR-TR2:~/Desktop$
标签:ago,SYS,记录,4028GR,学习,kt,docker,TR2
From: https://blog.csdn.net/weixin_42771529/article/details/144955018