Docker(一)

这篇博客详细介绍了Docker的基础操作,包括镜像的查找、查看、拉取、删除和创建,以及容器的创建、启动、运行、暂停、终止等管理。还讲解了数据卷的创建、绑定和多容器挂载,端口映射、互联以及Dockerfile创建镜像的方法。此外,文章涵盖了网络设置,如默认网络和自定义网络的使用。
摘要由CSDN通过智能技术生成

Docker

docker版本

ajune@ubuntu:~$ docker -v
Docker version 20.10.7, build f0df350

安装后需要执行的操作

将当前用户添加到docker用户组 docker 用户组,在安装docker后会自动创建

sudo usermod -aG docker USER NAME 然后退出重新登陆即可。
1、将当前用户加入docker用户组(该用户组在安装docker时会默认安装)

sudo gpasswd -a $USER docker
2、然后重启docker(有些系统不需要重启,可以先试试执行docker,如果系统报错,再重启docker即可)

sudo systemctl restart docker

3、添加权限

sudo chmod a+rw /var/run/docker.sock

镜像

查找

  • 查找, 通过docker search 可以进行对镜像的查找。查找是通过https://hub.docker.com/进行查找的。

    docker search 镜像名称

    ajune@ubuntu:~$ 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
    
    # 根据条件过滤
    ajune@ubuntu:~$ docker search nginx -f STARS=3000
    NAME      DESCRIPTION                STARS     OFFICIAL   AUTOMATED
    nginx     Official build of Nginx.   15317     [OK] 
    
    

查看

  • 查看

    docker images

    ajune@ubuntu:~$ 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
      -q, --quiet           Only show image IDs
    
    

拉取

  • 拉取,通过docker pull进行镜像的拉取,可以对拉取的镜像版本进行指定拉取。

    docker pull [选项] 镜像名称[:标签|@hash值]

    ajune@ubuntu:~$ 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 
    
    # 拉取不同版本的镜像
    # 拉取python3的镜像
    ajune@ubuntu:~$ docker pull python:3
    3: Pulling from library/python
    ...
    Digest: sha256:ec3fd3c3585160da037d2b01908ee7837a802be8984f449e65d3d80065d6d23a
    Status: Downloaded newer image for python:3
    docker.io/library/python:3
    # 拉取python2的镜像
    ajune@ubuntu:~$ docker pull python:2
    2: Pulling from library/python
    ...
    Digest: sha256:cfa62318c459b1fde9e0841c619906d15ada5910d625176e24bf692cf8a2601d
    Status: Downloaded newer image for python:2
    docker.io/library/python:2
    
    # 查看python的镜像
    ajune@ubuntu:~$ docker images python
    REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    python       3         b2278d5ae327   3 days ago      886MB
    python       2         68e7be49c28c   16 months ago   902MB
    
    

删除

  • 删除

    docker rmi python

    ajune@ubuntu:~$ 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
    # 删除tag为2 的python
    ajune@ubuntu:~$ docker rmi python:2
    

清理

docker image prune清理长时间未使用的镜像

ajune@ubuntu:~$ docker image prune --help

Usage:  docker image prune [OPTIONS]

Remove unused images

Options:
  -a, --all             Remove all unused images, not just dangling ones
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation

创建

commit
ajune@ubuntu:~$ docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
ajune@ubuntu:~$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                                   NAMES
c6d7d2387c2f   nginx     "/docker-entrypoint.…"   2 hours ago   Up 2 hours   0.0.0.0:8081->80/tcp, :::8081->80/tcp   nginxVolumeTest
fcff774abdc3   nginx     "/docker-entrypoint.…"   2 hours ago   Up 2 hours   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx01
ajune@ubuntu:~$ docker commit  -m "修改了index.html" -a "ajune" nginx01 nginxowner:v1
sha256:8e0d4807304c1bb96211c6e877df6430bb0d00377a31558c952208f76466dfde
ajune@ubuntu:~$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginxowner   v1        8e0d4807304c   5 seconds ago   133MB
nginx        latest    08b152afcfae   3 weeks ago     133MB

import
ajune@ubuntu:~$ docker import --help

Usage:  docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string    Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable

基于Dockerfile创建

导出与导入

导出
ajune@ubuntu:~$ docker save --help

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

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:
  -o, --output string   Write to a file, instead of STDOUT

ajune@ubuntu:~$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
dtest               v1        683cb0154877   16 hours ago   141MB
ajune@ubuntu:~$ docker save -o dtest.tar dtest:v1
ajune@ubuntu:~$ ll dtest*
-rw------- 1 ajune docker 147870208 Aug 17 09:47 dtest.tar

导入
ajune@ubuntu:~$ docker load --help

Usage:  docker load [OPTIONS]

Load an image from a tar archive or STDIN

Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

ajune@ubuntu:~$ docker load -i dtest.tar

容器

创建

docker create -it ubuntu
仅仅是创建了一个容器

ajune@ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS       PORTS                                   NAMES
8111753f3fc2   ubuntu    "bash"                   5 minutes ago   Created                                              dazzling_bardeen

启动

docker start

ajune@ubuntu:~$ docker start --help

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

Start one or more stopped containers

Options:
  -a, --attach               Attach STDOUT/STDERR and forward signals
      --detach-keys string   Override the key sequence for detaching a container
  -i, --interactive          Attach container's STDIN
ajune@ubuntu:~$ docker start 8111753f3fc2
ajune@ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS                                   NAMES
8111753f3fc2   ubuntu    "bash"                   5 minutes ago   Exited (0) 2 seconds ago                                           dazzling_bardeen

运行

  • 运行

    docker run

    docker run命令首先creates在指定映像上创建一个可写容器层,然后starts使用指定的命令。也就是说, docker run相当于 API /containers/createthen /containers/(id)/start。一个停止的容器可以使用docker start. 查看docker ps -a以查看所有容器的列表。
    Docker在后台运行的标准操作:

    • 检查本地是否存在指定的镜像,如果不存在,从公共仓库下下载

    • 利用镜像创建一个容器,并启动该容器

    • 分配一个文件系统给容器,并在只读的镜像层外面怪在一层可读写层

    • 从宿主机配置的网桥接一个虚拟接口到容器中

    • 从网桥的地址池配置一个IP地址给容器

    • 执行用户指定的应用程序

    • 执行完毕后容器被自动终止

    # 使用交互终端的方式启动名字为python3的容器,并且使用的镜像为python镜像tag为3.
    ajune@ubuntu:~$ docker run -it --name python3 python:3 /bin/bash 
    root@8665c5fb049e:/# python
    Python 3.9.6 (default, Jul 22 2021, 15:16:20) 
    [GCC 8.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit()
    # 退出容器
    root@8665c5fb049e:/# exit
    exit
    # 此时对应的容器也停止了
    ajune@ubuntu:~$ docker ps -a
    CONTAINER ID   IMAGE      COMMAND       CREATED              STATUS                     PORTS     NAMES
    8665c5fb049e   python:3   "/bin/bash"   About a minute ago   Exited (0) 4 seconds ago             python3
    
    # 运行一个nginx容器, 启动一个名字为nginx01的容器,将宿主机的8080端口映射到容器的80端口。
    ajune@ubuntu:~$ docker run -d --name nginx01 -p 8080:80 nginx
    fcff774abdc3f80685212e27372f3a2c710f535aaebc4d5bb31e6a8dfbbea73d
    ajune@ubuntu:~$ docker ps --filter name=nginx
    CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
    fcff774abdc3   nginx     "/docker-entrypoint.…"   8 seconds ago   Up 7 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx01
    ajune@ubuntu:~$ 
    
    
    参数 默认值 含义
    --add-host 添加自定义主机到 IP 映射 (host:ip)
    --attach , -a 附加到 STDIN、STDOUT 或 STDERR
    --blkio-weight Block IO(相对权重),在 10 到 1000 之间,或 0 表示禁用(默认为 0)
    --blkio-weight-device 块IO权重(相对设备权重)
    --cap-add 添加 Linux 功能
    --cap-drop 删除 Linux 功能
    --cgroup-parent 容器的可选父 cgroup
    --cgroupns API 1.41+ 要使用的 Cgroup 命名空间 (host|private) ‘host’:在 Docker 主机的 cgroup 命名空间中运行容器 ‘private’:在其自己的私有 cgroup 命名空间中运行容器 ‘’:使用由 default-cgroupns-配置的 cgroup 命名空间守护进程的模式选项(默认)
    --cidfile 将容器 ID 写入文件
    --cpu-count CPU 计数(仅限 Windows)
    --cpu-percent CPU 百分比(仅限 Windows)
    --cpu-period 限制 CPU CFS(完全公平调度程序)周期
    --cpu-quota 限制 CPU CFS(完全公平调度程序)配额
    --cpu-rt-period API 1.25+ 以微秒为单位限制 CPU 实时周期
    --cpu-rt-runtime API 1.25+ 以微秒为单位限制 CPU 实时运行时间
    --cpu-shares , -c CPU份额(相对权重)
    --cpus API 1.25+ CPU数量
    --cpuset-cpus 允许执行的 CPU (0-3, 0,1)
    --cpuset-mems 允许执行的 MEM (0-3, 0,1)
    --detach , -d 在后台运行容器并打印容器 ID
    --detach-keys 覆盖用于分离容器的键序列
    --device 将主机设备添加到容器
    --device-cgroup-rule 向 cgroup 允许的设备列表添加规则
    --device-read-bps 限制设备的读取速率(每秒字节数)
    --device-read-iops
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值