docker容器操作命令

docker容器命令

一、前台交互式启动

[root@localhost etc]# docker run -it  ubuntu /bin/bash
root@d9c81eb09ef1:/# 
  • 指定名称启动–name
[root@localhost etc]# docker run -it --name=u1 ubuntu /bin/bash

查看启动的docker容器实例

[root@localhost etc]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
d9c81eb09ef1   ubuntu    "/bin/bash"   9 hours ago   Up 9 hours             focused_proskuriakova
[root@localhost etc]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
3499e2cf3d1c   ubuntu    "/bin/bash"   19 seconds ago   Up 18 seconds             u1
d9c81eb09ef1   ubuntu    "/bin/bash"   10 hours ago     Up 10 hours               focused_proskuriakova
  • 查看历史记录
[root@localhost etc]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                      PORTS     NAMES
3499e2cf3d1c   ubuntu         "/bin/bash"   9 minutes ago   Up 9 minutes                          u1
d9c81eb09ef1   ubuntu         "/bin/bash"   10 hours ago    Up 10 hours                           focused_proskuriakova
ac67d6ae8d61   ubuntu         "/bin/bash"   10 hours ago    Exited (0) 10 hours ago               serene_johnson
1f7b023b8a12   ubuntu         "bash"        10 hours ago    Exited (127) 10 hours ago             amazing_carver
d4cad94c2b5b   feb5d9fea6a5   "/hello"      26 hours ago    Exited (0) 26 hours ago               infallible_payne
906747c232cb   feb5d9fea6a5   "/hello"      27 hours ago    Exited (0) 27 hours ago               relaxed_mendel
327aa17a0154   feb5d9fea6a5   "/hello"      27 hours ago    Exited (0) 27 hours ago               cranky_diffie
59870e025231   feb5d9fea6a5   "/hello"      5 days ago      Exited (0) 5 days ago                 hungry_mestorf
e07f9af080a4   feb5d9fea6a5   "/hello"      5 days ago      Exited (0) 5 days ago                 epic_elgamal
  • 查看最近启动实例
[root@localhost etc]# docker ps -l
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
3499e2cf3d1c   ubuntu    "/bin/bash"   11 minutes ago   Up 11 minutes             u1
  • 查看最近n条记录
[root@localhost etc]# docker ps -n 1
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
3499e2cf3d1c   ubuntu    "/bin/bash"   17 minutes ago   Up 17 minutes             u1

二、后台守护式启动

[root@localhost ~]# docker run -d --name=r1 redis
dc55ae800635501bae6011916e335d34faa725a074a0e05daa60791f81f419ab
[root@localhost ~]# 
  • 查看启动是否成功
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS      NAMES
dc55ae800635   redis     "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   6379/tcp   r1
dd1b2c4e684f   ubuntu    "bash"                   18 hours ago    Up 7 minutes              u1
9ea72b383053   ubuntu    "bash"                   18 hours ago    Up 7 minutes              u2
  • 查看进程日志
[root@localhost ~]# docker logs dc55ae800635
1:C 07 Aug 2022 09:44:29.412 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 07 Aug 2022 09:44:29.413 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 07 Aug 2022 09:44:29.413 # 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 07 Aug 2022 09:44:29.414 * monotonic clock: POSIX clock_gettime
1:M 07 Aug 2022 09:44:29.415 * Running mode=standalone, port=6379.
1:M 07 Aug 2022 09:44:29.415 # 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 07 Aug 2022 09:44:29.415 # Server initialized
1:M 07 Aug 2022 09:44:29.415 # 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 07 Aug 2022 09:44:29.416 * Ready to accept connections
  • 查看容器内进程
  • 此时仅运行了root
[root@localhost ~]# docker top dd1b2c4e684f 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                72739               72718               0                   18:12               pts/0               00:00:00            bash
  • 查看容器内部细节
[root@localhost ~]# docker inspect dc55ae800635
[
    {
        "Id": "dc55ae800635501bae6011916e335d34faa725a074a0e05daa60791f81f419ab",
        "Created": "2022-08-07T09:44:28.397855411Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "redis-server"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 73469,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-08-07T09:44:29.390079276Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631",
.
.
.
  • 重新进入容器终端
[root@localhost ~]# docker exec -it dc55ae800635 /bin/bash
root@dc55ae800635:/data# 
  • 一般用-d后台启动程序,然后用exec进入对应容器实例
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值