docker 常用容器命令汇总

目录

新建 + 启动容器

列出所有正在运行的容器

退出容器

启动已经停止运行的容器

重启容器

停止容器

强制停止容器

删除已停止的容器

强制删除容器

容器作为启动守护进程

docker run -d hello-world

docker run -d redis:6.0.8

查看容器的日志

查看容器内运行的进程

查看容器内部细节

进入正在运行的容器,并以命令行交互

从容器内拷贝文件到主机

导入和到出容器


新建 + 启动容器

  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

# 给新启动的容器分配一个名字        

--name string                    Assign a name to the container

-d 后台启动容器并返回容器ID,也即启动守护式容器

-i 以交互模式运行容器,通常与 -t 同时使用

-t 为容器分配一个伪输入终端,通常与 -i 配合使用

也即启动交互式容器(前台有伪虚拟终端,等待交互)

-P 随机端口映射,大写P

-p 指定端口映射,小写p 

-p hostport:containerPort ,如 -p 8080:80

# 执行了容器名称
[root@localhost ~]# docker run --name ubt_conid -it ubuntu /bin/bash
root@0d498db1e759:/# ip addr
bash: ip: command not found
root@0d498db1e759:/# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 00:32 pts/0    00:00:00 /bin/bash
root         10      1  0 00:33 pts/0    00:00:00 ps -ef
root@0d498db1e759:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@0d498db1e759:/# route -n
bash: route: command not found
root@0d498db1e759:/# exit


# 未指定容器名字
[root@localhost ~]# docker run  -it ubuntu /bin/bash
root@4515a6c81b3f:/#

docker run --name ubt_conid -it ubuntu /bin/bash

--name :ubt_conid 指定容器名称,如果不指定容器的名字,系统会随机分配一个

-it :交换模式

ubuntu :镜像名称

/bin/bash:交互使用的shell语言

0d498db1e759 :返回的容器ID

查看效果:

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
4515a6c81b3f   ubuntu    "/bin/bash"   6 seconds ago    Up 4 seconds              vigilant_pike
2faefecdb857   ubuntu    "/bin/bash"   48 seconds ago   Up 47 seconds             ubt_conid1
[root@localhost ~]#

列出所有正在运行的容器

docker ps [OPTIONS]

[root@localhost ~]# 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 container IDs
  -s, --size            Display total file sizes
[root@localhost ~]#


[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
4515a6c81b3f   ubuntu    "/bin/bash"   14 minutes ago   Up 14 minutes             vigilant_pike
2faefecdb857   ubuntu    "/bin/bash"   14 minutes ago   Up 14 minutes             ubt_conid1
[root@localhost ~]#

退出容器

方式1: run进入容器后,使用 exit 退出,此方式会导致该容器停止运行

方式2:run进去容器后,使用ctrl + p + q 退出,此方式退出后,容器不停止

启动已经停止运行的容器

docker start 容器ID 或者 容器名称

重启容器

docker restart 容器ID 或者 容器名称

停止容器

docker stop 容器ID 或者 容器名称

强制停止容器

docker kill 容器ID 或者 容器名称

删除已停止的容器

docker rm 容器ID 或者 容器名称

强制删除容器

docker rm  -f 容器ID 或者 容器名称

容器作为启动守护进程

docker run -d hello-world

docker run -d 并不是能保证容器一直在后台,要确保执行的命令能够在后台一直挂着。 如下图运行hello-world,虽然使用了-d,但是hello-world 很快就运行完,并退出了,通过docker ps 并没有看到有容器在运行

[root@localhost ~]# docker run -d hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Already exists
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Status: Downloaded newer image for hello-world:latest
a3f6a524566976cefe8436317c387cafd86e72491112d3b8e3040376ba4cef39
[root@localhost ~]#

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]#

docker run -d redis:6.0.8

这个用-d就能够使redis一直在后台运行

[root@localhost ~]# docker run -d redis:6.0.8
36bd9053b51b7959e68b2f43b345fc75aaf3369a685a54dad793da4f30af7925
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS      NAMES
36bd9053b51b   redis:6.0.8   "docker-entrypoint.s…"   7 seconds ago   Up 5 seconds   6379/tcp   objective_elgamal
[root@localhost ~]#

查看容器的日志

docker logs 容器ID 或者 容器名称

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS      NAMES
36bd9053b51b   redis:6.0.8   "docker-entrypoint.s…"   7 seconds ago   Up 5 seconds   6379/tcp   objective_elgamal
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker logs objective_elgamal
1:C 10 Jul 2022 01:35:44.521 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 10 Jul 2022 01:35:44.521 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 10 Jul 2022 01:35:44.521 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 10 Jul 2022 01:35:44.531 * Running mode=standalone, port=6379.
1:M 10 Jul 2022 01:35:44.533 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 10 Jul 2022 01:35:44.533 # Server initialized
1:M 10 Jul 2022 01:35:44.533 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 10 Jul 2022 01:35:44.533 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 10 Jul 2022 01:35:44.534 * Ready to accept connections
[root@localhost ~]#

查看容器内运行的进程

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS      NAMES
36bd9053b51b   redis:6.0.8   "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   6379/tcp   objective_elgamal
[root@localhost ~]# docker top 36bd9053b51b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
polkitd             13408               13389               0                   09:35               ?                   00:00:01            redis-server *:6379
[root@localhost ~]#

查看容器内部细节

 docker inspect 容器ID 或者 容器名称

inspect : 检查; 查看; 审视; 视察;

进入正在运行的容器,并以命令行交互

方式1:

docker exec -it  容器ID 或者 容器名称  /bin/bash

方式2:

docker attach  容器ID 或者 容器名称

区别:

  1. docker exec 是在容器中启动的新终端,并且可以启动新的进程用exit退出,不会导致容器停止
  2. docker attach直接进入容器启动命令的终端,不会启用新的进程,用exit退出,会导致容器的停止

从容器内拷贝文件到主机

docker cp 容器ID:路径  目的主机的路径

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS      NAMES
36bd9053b51b   redis:6.0.8   "docker-entrypoint.s…"   34 minutes ago   Up 34 minutes   6379/tcp   objective_elgamal
[root@localhost ~]# docker exec -it 36bd9053b51b /bin/bash
root@36bd9053b51b:/data# pwd
/data
root@36bd9053b51b:/data# touchu 123.txt
bash: touchu: command not found
root@36bd9053b51b:/data# touch 123.txt
root@36bd9053b51b:/data# ls
123.txt
root@36bd9053b51b:/data# echo "12345" > 123.txt
root@36bd9053b51b:/data# cat 123.txt
12345
root@36bd9053b51b:/data# exit
exit
[root@localhost ~]# docker cp 36bd9053b51b:/data/123.txt /root/
[root@localhost ~]# ls
123.txt  anaconda-ks.cfg  original-ks.cfg
[root@localhost ~]# cat 123.txt
12345
[root@localhost ~]#

导入和到出容器

export 导出容器的内容流作为一个tar归档文件

import 从tar包中的内容创建一个新的文件系统再导入为镜像

docker export 容器ID > XXX.tar

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS      NAMES
36bd9053b51b   redis:6.0.8   "docker-entrypoint.s…"   41 minutes ago   Up 41 minutes   6379/tcp   objective_elgamal
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker export 36bd9053b51b > /root/redis_bak.tar
[root@localhost ~]# ls
123.txt  anaconda-ks.cfg  original-ks.cfg  redis_bak.tar
[root@localhost ~]#

cat XXX.tar | docker import  - 镜像名或者镜像ID:镜像版本

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS      NAMES
36bd9053b51b   redis:6.0.8   "docker-entrypoint.s…"   47 minutes ago   Up 47 minutes   6379/tcp   objective_elgamal
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
redis         latest    2e50d70ba706   2 weeks ago     117MB
ubuntu        latest    27941809078c   4 weeks ago     77.8MB
hello-world   latest    feb5d9fea6a5   9 months ago    13.3kB
centos        latest    5d0da3dc9764   9 months ago    231MB
redis         6.0.8     16ecd2772934   20 months ago   104MB
[root@localhost ~]# cat redis_bak.tar | docker import - redisbak:7.0.7
sha256:faa679f39e42c5d45de65bb84bb3b534e874660dcef0486908e5db96b10956f1
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
redisbak      7.0.7     faa679f39e42   4 seconds ago   101MB
redis         latest    2e50d70ba706   2 weeks ago     117MB
ubuntu        latest    27941809078c   4 weeks ago     77.8MB
hello-world   latest    feb5d9fea6a5   9 months ago    13.3kB
centos        latest    5d0da3dc9764   9 months ago    231MB
redis         6.0.8     16ecd2772934   20 months ago   104MB
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS      NAMES
36bd9053b51b   redis:6.0.8   "docker-entrypoint.s…"   47 minutes ago   Up 47 minutes   6379/tcp   objective_elgamal
[root@localhost ~]#

你可能感兴趣的:(docker,大数据,docker,容器,运维,linux)