Docker学习笔记

官网

https://www.docker.com/

镜像下载URL

https://hub.docker.com/u/arm64v8

docker架构图

![image-20220205210426512](/Users/wangdl/Library/Application Support/typora-user-images/image-20220205210426512.png)

基础命令

查看版本

# 查看版本
docker version
# 查看情报
docker info
# 查看帮助
docker --help

查看本地所有镜像

docker images

# 帮助文档
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
      --no-trunc        Don't truncate output
  # 静默模式,只显示镜像的ID
  -q, --quiet           Only show image IDs

查找镜像

docker search 镜像名称

#帮助文档
docker search --help

Usage:  docker search [OPTIONS] TERM

Search the 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 (default 25)
      --no-trunc        Don't truncate output
# 过滤搜索结果
docker search wget --filter=STARS=3
NAME            DESCRIPTION             STARS     OFFICIAL   AUTOMATED
mwendler/wget   a tiny wget setup       7                    [OK]
inutano/wget    simple wget container   3                    [OK]

下载镜像

docker pull 镜像名称

# 帮助文档
docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  # 静默模式
  -q, --quiet                   Suppress verbose output

docker pull inutano/wget
Using default tag: latest
latest: Pulling from inutano/wget
df20fa9351a1: Pull complete 				#分层下载
2940cbfbf56c: Pull complete 				#分层下载
Digest: sha256:4717ff7329d48f925e6f09bb8b499059b349448d946a36b9b8c630c88ff7db49
Status: Downloaded newer image for inutano/wget:latest
docker.io/inutano/wget:latest

删除镜像

# 删除单个镜像
docker rmi 镜像名称/镜像ID

# 删除多个镜像
docker rmi 镜像ID1 镜像ID2 镜像ID3 

docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

# 删除所有镜像
docker rmi -f $(docker images -aq)

启动容器

docker run 镜像ID

# 可选参数
      # 给容器起一个好记忆的名字
      --name string                    Assign a name to the container
      # 后台运行
      -d, --detach                         Run container in background and print container ID
      # 以交互模式运行
      -it
        -i, --interactive                    Keep STDIN open even if not attached
        -t, --tty                            Allocate a pseudo-TTY
      # 指定端口,端口映射
      -p, --publish list                   Publish a container's port(s) to the host
      # 主机端口:容器端口
      -p 8080:8080
      
      # 指定随机端口
      -P, --publish-all                    Publish all exposed ports to random ports

docker run -it --name=nginx arm64v8/nginx /bin/bash
root@980b498991ed:/#

查看运行中的容器

docker ps 

# 帮助文档
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
  # 静默模式,只显示容器的ID
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes

查看所有容器

docker ps -a

docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                            PORTS     NAMES
980b498991ed   arm64v8/nginx   "/docker-entrypoint.…"   3 minutes ago    Exited (127) About a minute ago             nginx
ac7b2f0c1fee   502962e4a113    "catalina.sh run"        9 months ago     Exited (143) 6 minutes ago                  tomcat9

容器操作

# 启动
docker start 容器ID
# 停止
docker stop 容器ID
# 重启
docker restart 容器ID
# 强制停止
docker kill 容器ID

退出容器

# 关闭退出
exit

# ~~未检证~~
# 不关闭退出
Cmd+P+Q

删除容器

# 删除单个
docker rm 容器ID 

# 删除多个
docker rm 容器ID1 容器ID2 容器ID3 

# 删除所有
docker rm $(docker ps -aq)
docker ps -aq | xargs docker rm

# 帮助文档
docker rm --help

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container

查看日志

docker logs

# 帮助文档
docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

查看容器中的进程

docker top 容器ID

查看容器的元数据

docker inspect --help

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type

进入运行中的容器

# 以新开终端的方式进入到运行中的容器
docker exec -it 容器id /bin/bash

# 帮助文档
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

未检证

# 进入到当前运行中的容器的进程里,不新开终端
docker attach 容器id

# 帮助文档
docker attach --help

Usage:  docker attach [OPTIONS] CONTAINER

Attach local standard input, output, and error streams to a running container

Options:
      --detach-keys string   Override the key sequence for detaching a container
      --no-stdin             Do not attach STDIN
      --sig-proxy            Proxy all received signals to the process (default true)

互相拷贝文件

# 帮助文档
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
# 从镜像拷贝文件到本地
docker cp 容器名:拷贝元路径加文件 本地路径

docker cp ac7b2f0c1fee:/usr/local/games/Centos-8.repo /Users/Movies

# 从本地拷贝文件到镜像
docker cp 本地文件 容器名:容器路径

docker cp /Users/Movies/Centos-8.repo ac7b2f0c1fee:/usr/local/games/
上一篇:docker入门教程


下一篇:文本框、密码框、文本域