Docker常用命令操作

1.Docker基本信息查看命令

1.1查看docker帮助命令

[root@host152 ~]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker
                           context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.8.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  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
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  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
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.
 

1.2查看docker详细信息

[root@host152 ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1

1.3查看docker版本

[root@host152 ~]# docker --version
Docker version 20.10.7, build f0df350

2.Docker常用操作命令

2.1列出镜镜

[root@host152 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   4 months ago   13.3kB

2.2查找镜像

[root@host152 ~]# docker search redis
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                            Redis is an open source key-value store that…   9734      [OK]       
sameersbn/redis                                                                  83                   [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2      78                   
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   63                   [OK]

2.3拉取镜像

docker pull 镜像名称[:version],不加版本写默认拉取最新镜像。

[root@host152 ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
33847f680f63: Pull complete 
26a746039521: Pull complete 
18d87da94363: Pull complete 
5e118a708802: Pull complete 
ecf0dbe7c357: Pull complete 
46f280ba52da: Pull complete 
Digest: sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

2.4删除镜像

2.4.1删除一个镜像 

docker rmi 镜像名称/id 

[root@host152 ~]# docker rmi hello-world

2.4.2删除多个镜像

docker rmi 镜像名称1/id1 镜像名称2/id2 ... 

[root@host152 ~]#docker rmi hello-world redis

2.4.3删除所有镜像 

[root@host152 ~]# docker rmi `docker images ‐q`

2.5创建容器

[root@host152 ~]# docker run --help
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

后台运行redis容器

[root@host152 ~]# docker run -itd --name redis-docker -p 6379:6379 redis

参数说明
-i: 交互式操作。
-t: 终端。
-d:后台运行
-name:创建容器名称
-p:端口映射,映射容器服务的 6379 端口到宿主机的 6379 端口。外部可以直接通过宿主机ip:6379 访问到 Redis 的服务

2.6查看容器运行信息

2.6.1查看正在运行的容器

[root@host152 ~]#  docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                       NAMES
756fe98d843c   redis     "docker-entrypoint.s…"   8 minutes ago   Up 7 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis-docker

2.6.2查看历史容器

[root@host152 ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS                         PORTS                                       NAMES
756fe98d843c   redis         "docker-entrypoint.s…"   16 minutes ago      Up 16 minutes                  0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis-docker
fd5b96fbee6e   redis         "docker-entrypoint.s…"   18 minutes ago      Exited (0) 17 minutes ago                                                  redis-test
6923169afc51   hello-world   "/hello"                 About an hour ago   Exited (0) About an hour ago                                               elegant_feynman

2.6.3查看最后一次运行的容器

[root@host152 ~]# docker ps -l
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                       NAMES
756fe98d843c   redis     "docker-entrypoint.s…"   18 minutes ago   Up 18 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis-docker

2.7登陆容器

命令格式提示

[root@host152 ~]# docker exec --help

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
      --env-file list        Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container

登陆容器,开启终端连接redis

[root@host152 ~]# docker exec -it redis-docker /bin/bash

[root@host152 ~]# docker exec -it redis-docker /bin/bash
root@756fe98d843c:/data# redis-cli 
127.0.0.1:6379> set age 10
OK
127.0.0.1:6379> get age
"10"

2.8容器的停启

2.8.1容器的停止

[root@host152 ~]# docker stop redis-docker
redis-docker
[root@host152 ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

2.8.2容器的启动

[root@host152 ~]# docker start redis-docker
redis-docker
[root@host152 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                                       NAMES
756fe98d843c   redis     "docker-entrypoint.s…"   22 minutes ago   Up 3 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis-docker

2.9查看容器的详细信息

[root@host152 ~]# docker inspect redis-docker

2.10查看容器的日志

[root@host152 ~]# docker logs --help
Usage:  docker logs [OPTIONS] CONTAINER

[root@host152 ~]# docker logs redis-docker
1:C 01 Aug 2021 09:22:40.449 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 01 Aug 2021 09:22:40.449 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 01 Aug 2021 09:22:40.449 # 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 01 Aug 2021 09:22:40.450 * monotonic clock: POSIX clock_gettime

2.11容器间文件的拷贝

[root@host152 myfile]# 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

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

2.11.1启动centos

[root@host152 ~]# docker run -itd --name centos-docker centos /bin/bash

登陆创建的centos容器

[root@host152 ~]# docker exec -it centos-docker /bin/bash

2.11.2复制宿主机文件到运行的docker容器

[root@host152 myfile]# docker cp a.txt centos-docker:/root/dockerfile

登陆容器查看文件是否复制成功
[root@host152 myfile]# docker exec -it centos-docker /bin/bash
[root@0966db9df33d /]# cd /root/dockerfile
[root@0966db9df33d dockerfile]# ll       
total 4
-rw-r--r-- 1 root root 24 Aug  1 10:24 a.txt

2.11.3把docker文件复制出来到宿主查

[root@host152 myfile]# docker cp centos-docker:/root/dockerfile/dockerfile.txt /root/myfile/
[root@host152 myfile]# ll
total 8
-rw-r--r-- 1 root root 24 Aug  1 18:24 a.txt
-rw-r--r-- 1 root root 22 Aug  1 18:40 dockerfile.txt

1.12启动挂载目录文件

        我们可以在创建容器的时候,将宿主机的目录与容器内的目录进行映射,这样我们就可 以通过修改宿主机某个目录的文件从而去影响容器。 
        创建容器 添加-v参数 后边为 宿主机目录:容器目录

[root@host152 myfile]# docker run -id --name=centos-load -v /root/myfileload:/root/dockerfile centos

        如果有权限问题,是因为CentOS7中的安全模块selinux把权限禁掉了,我们需要添加参数 -- privileged=true 来解决挂载的目录没有权限的问题

[root@host152 myfile]# docker run -id --privileged=true --name=centos-load -v /root/myfileload:/root/dockerfile centos

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值