当前位置:   article > 正文

[Docker]二.Docker 镜像,仓库,容器介绍以及详解_docker 镜像库

docker 镜像库

一.Docker 镜像,容器,仓库的简单介绍

通俗来讲:镜像相当于VM虚拟机中的ios文件,容器相当于虚拟机系统,仓库相当于系统中的进程或者执行文件,容器是通过镜像创建的

 1.镜像

Docker 镜像就是一个 Linux 的文件系统( Root FileSystem ),这个文件系统里面包含可以运行在 Linux 内核的程序以及相应的数据,这里要强调一下镜像的两个特征:
镜像是分层(Layer)的
        即一个镜像可以多个中间层组成,多个镜像可以共享同一中间层,也可以通过在镜像添加多一层来生成一个新的镜像
镜像是只读的(read-only
         镜像在构建完成之后,便不可以再修改,而上面所说的添加一层构建新的镜像,这中间实际是通过创建一个临时的容器,在容器上增加或删除文件,从而形成新的镜像,因为容器是可以动态改变的

 2.容器

        类似 linux 系统环境, 运行和隔离应用 ,容器从镜像启动的时候, docker 会在镜像的最上一层创建一个可写层, 镜像本身是只读的 ,保持不变,容器与镜像的关系,就如同面向编程中
对象与类之间的关系。
        因为 容器是通过镜像来创建的 ,所以必须先有镜像才能创建容器,而生成的容器是一个独立于宿主机的隔离进程,并且有属于容器自己的网络和命名空间。
        前面介绍过,镜像由多个中间层(layer )组成,生成的镜像是只读的,但容器却是可读
可写的,这是因为容器是在镜像上面添一层读写层( writer/read layer )来实现的,如下图所
示:

3.仓库(Repository 

仓库(Repository)是集中存储镜像的地方 ,这里有个概念要区分一下,那就是 仓库 仓库
服务器 (Registry) 是两回事,像上面说的 Docker Hub ,就是 Docker 官方提供的一个仓库服务器,不过其实有时候不太需要太过区分这两个概念

公共仓库

公共仓库一般是指 Docker Hub ,前面已经多次介绍如何从 Docker Hub 获取镜像,除了获取镜像外,也可以将自己构建的镜像存放到 Docker Hub ,这样,别人也可以使用我们构建的镜像,不过要将镜像上传到 Docker Hub ,必须先在 Docker 的官方网站上注册一个账号,注册界面如下,按要求填写必要的信息就可以注册了

 私有仓库

有时候自己部门内部有一些镜像要共享时,如果直接导出镜像拿给别人又比较麻烦,使用像
Docker Hub 这样的公共仓库又不是很方便,这时候可以自己搭建属于自己的私有仓库服务,用于存储和分布自己的镜像

二.Docker 镜像以及仓库 

Docker 镜像就是一个 Linux 的文件系统( Root FileSystem ),这个文件系统里面包含可以运行在 Linux 内核的程序以及相应的数据

1.docker search 搜索镜像

docker search centos

  1. [root@localhost containerd]# docker search --help
  2. Usage: docker search [OPTIONS] TERM
  3. Search the Docker Hub for images
  4. Options:
  5. -f, --filter filter Filter output based on conditions provided
  6. --format string Pretty-print search using a Go template
  7. --limit int Max number of search results (default 25)
  8. --no-trunc Don't truncate output
备注: -f STARS=3000 搜索 STARS 大于 3000 的镜像
搜索mysql  STARS 大于 3000的镜像 
  1. [root@localhost containerd]# docker search mysql -f STARS=3000
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation… 14530 [OK]
  4. mariadb MariaDB Server is a high performing open sou… 5550 [OK]

2.docker pull 下载镜像

下载docker pull centos最新的镜像

docker pull centos
  1. [root@localhost /]# docker pull centos
  2. Using default tag: latest
  3. latest: Pulling from library/centos
  4. 7a0437f04f83: Pull complete
  5. Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
  6. Status: Downloaded newer image for centos:latest
  7. docker.io/library/centos:latest
下载指定的tag的镜像:docker pull centos:8.3.2011
  1. [root@localhost /]# docker pull centos:8.3.2011
  2. 8.3.2011: Pulling from library/centos
  3. Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
  4. Status: Downloaded newer image for centos:8.3.2011
  5. docker.io/library/centos:8.3.2011

下载nginx镜像: nginx镜像中集成了一个linux操作系统,在操作系统中又集成了nginx,然后打包成一个nginx镜像,下载好nginx镜像后,然后启动该服务,就可以进行web服务操作了,实际上在下载nignx镜像的时候,其实不是一下子就下载好了的,而是一层一层地下载其他镜像,然后打包成nginx镜像

  1. root@localhost zph]# docker pull nginx
  2. Using default tag: latest
  3. latest: Pulling from library/nginx
  4. Digest: sha256:b4af4f8b6470febf45dc10f564551af682a802eda1743055a7dfc8332dffa595
  5. Status: Downloaded newer image for nginx:latest
  6. docker.io/library/nginx:latest

3.镜像结构

镜像结构:docker.io/library/centos:latest
  1. [root@localhost containerd]# docker pull centos
  2. Using default tag: latest
  3. latest: Pulling from library/centos
  4. Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
  5. Status: Downloaded newer image for centos:latest
  6. docker.io/library/centos:latest

4.docker images 查看本地镜像

  1. [root@localhost containerd]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest bc649bab30d1 10 days ago 187MB
  4. hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
  5. centos latest 5d0da3dc9764 2 years ago 231MB

5.docker tag 给镜像打标签

比如:下载了一个镜像,启动了这个容器,在这个容器中做了一些操作,然后就想把这个容器制作成自己的镜像,那么就需要给这个镜像打标签(相当于发布这个镜像,制作好这个镜像后,就可以把它发布到远程仓库了),给镜像打标签可以创建自己的镜像, 自定义镜像结构命名规则如下 : registry name/repository name/image name:tag name ,例如:docker.io/library/centos:latest
  1. [root@localhost zph]# docker tag --help
  2. Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
  3. Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

 对上述说明:

        SOURCE_IMAGE 镜像名   [:TAG] 镜像标签名

        TARGET_IMAGE 打包新的镜像名 [:TAG] 新镜像标签名

比如:把nginx镜像打包成一个新的镜像:

docker tag nginx  docker.io/zph/nginx:last 或者把nginx替换成对应的IMAGE ID,如: 

docker tag bc649bab30d1 docker.io/zph/nginx:v2

  1. root@localhost zph]# docker tag nginx docker.io/zph/nginx:last
  2. [root@localhost zph]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. nginx latest bc649bab30d1 10 days ago 187MB
  5. zph/nginx last bc649bab30d1 10 days ago 187MB

6.把本地镜像推送到 dockerHub 仓库 

(1).需要在dockerHub上面注册一个账户

把打包好的镜像发布到远程(hub.docker.com官网)自己的账号下,需要有一个docker账号

(2).使用 docker login 本地登录 dockerHub 

  1. [root@localhost ~]# docker login
  2. Login with your Docker ID to push and pull images from Docker Hub. If you don't have
  3. a Docker ID, head over to https://hub.docker.com to create one.
  4. Username: zph
  5. Password:
  6. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See
  7. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  8. Login Succeeded
登录成功后,会把账号信息保存到.docker/config.json中,查看保存的账户信息
  1. [root@localhost ~]# cat .docker/config.json
  2. {
  3. "auths": {
  4. "https://index.docker.io/v1/":
  5. {
  6. "auth": "aXR5aW5nOxxxzEyMzQ="
  7. }
  8. }
  9. }

(3).docker push 镜像名称 把本地镜像推送到远程

下载一个alpine的镜像演示,alpine是一个比较小的的 linux 镜像

  1. [root@localhost ~]# 下载alpine镜像
  2. [root@localhost ~]# docker pull alpine
  3. [root@localhost ~]# 打包alpine镜像
  4. [root@localhost ~]# docker tag d4ff818577bc docker.io/itying/alpine:v1.0.1
  5. [root@localhost ~]# docker tag d4ff818577bc docker.io/itying/alpine:latest
  6. [root@localhost ~]# 查看alpine镜像
  7. [root@localhost ~]# docker images | grep alpine
  8. itying/alpine v1.0.1 d4ff818577bc 4 weeks ago 5.6MB
  9. alpine 3.14.0 d4ff818577bc 4 weeks ago 5.6MB
  10. alpine latest d4ff818577bc 4 weeks ago 5.6MB
  11. [root@localhost ~]# 发布apline镜像到dockerHub
  12. [root@localhost ~]# docker push docker.io/itying/alpine:v1.0.1
  13. The push refers to repository [docker.io/itying/alpine]
  14. 72e830a4dff5: Mounted from library/alpine
  15. v1.0.1: digest: sha256:1775bebec23e1f3ce4869xxx26497d82f2ffca9
  16. d size: 528

7.docker rmi 删除镜像 

查看本地镜像
  1. [root@localhost zph]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest bc649bab30d1 10 days ago 187MB
  4. zph/nginx last bc649bab30d1 10 days ago 187MB
  5. zph/nginx v2 bc649bab30d1 10 days ago 187MB
  6. hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
  7. centos latest 5d0da3dc9764 2 years ago 231MB

 删除对应的标签镜像:docker rmi 镜像名:版本,如下:

  1. [root@localhost zph]# docker rmi zph/nginx:v2
  2. Untagged: zph/nginx:v2
  3. [root@localhost zph]# docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. nginx latest bc649bab30d1 10 days ago 187MB
  6. zph/nginx last bc649bab30d1 10 days ago 187MB
  7. hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
  8. centos latest 5d0da3dc9764 2 years ago 231MB

如果要删除IMAGE ID的镜像,则使用命令:docker rmi -f IMAGE ID, 如:

  1. [root@localhost zph]# docker rmi -f bc649bab30d1
  2. Untagged: nginx:latest
  3. Untagged: nginx@sha256:b4af4f8b6470febf45dc10f564551af682a802eda1743055a7dfc8332dffa595
  4. Untagged: zph/nginx:last
  5. Deleted: sha256:bc649bab30d150c10a84031a7f54c99a8c31069c7bc324a7899d7125d59cc973
  6. Deleted: sha256:c6f480996a203ed077606cce624f944b041449833e2db3f7d19fe22974fb965b
  7. Deleted: sha256:e4347a01432c5f4350b041632f5703c3dd47de2ec68547b9339d11ea44708389
  8. Deleted: sha256:9d40098fc19fdfff9c74fd3c2c0ff49bfda7d9d04b5d7806d0843d32055d769a
  9. Deleted: sha256:165ae0ef2ddd33b6d5a7f206633b9b0c30cd94ff18a4ed5c3aeb59bf28388526
  10. Deleted: sha256:06dabb44ac4d1f0b5544255e944f15a939178d77aff60a5b296e38bd8743efeb
  11. Deleted: sha256:ee220599571f649e0fb74b40db1615a4c9c1355ac912f9e70087b695617af352
  12. Deleted: sha256:cb4596cc145400fb1f2aa56d41516b39a366ecdee7bf3f9191116444aacd8c90
  13. [root@localhost zph]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
  16. centos latest 5d0da3dc9764 2 years ago 231MB
这样就会删除IMAGE ID 为 bc649bab30d1的nginx 镜像了

三.Docker 容器

类似 linux 系统环境,运行和隔离应用, 容器从镜像启动的 时候, docker 会在 镜像的最上一
创建一个 可写层 ,镜像本身是 只读的 ,保持不变,容器与镜像的关系,就如同面向编程中对象与类之间的关系。
因为容器是通过镜像来创建的,所以必须先有镜像才能创建容器,而生成的容器是一个独立
于宿主机的隔离进程,并且有属于容器自己的网络和命名空间

1.查看所的容器 

查看所有运行的容器
  1. [root@localhost zph]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 757e72f3b32e rabbitmq:3.7.16-management "docker-entrypoint.s…" 22 months ago Up 6 hours 0.0.0.0:4369->4369/tcp, :::4369->4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp, 0.0.0.0:25672->25672/tcp, :::25672->25672/tcp, 15671/tcp dockerlnmp_rabbitmq_1
  4. e526c3df36f7 dockerlnmp_mysql "docker-entrypoint.s…" 22 months ago Up 6 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp

 查看所有容器(既有运行中的容器,也有被销毁的容器)

  1. [root@localhost zph]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 8def313c4f6e hello-world "/hello" 5 hours ago Exited (0) 5 hours ago intelligent_johnson
  4. 59222227ec81 dockerlnmp_nginx "/usr/bin/supervisor…" 21 months ago Exited (0) 19 months ago dockerlnmp_nginx_1
  5. ed99ccb1576f dockerlnmp_php "/usr/bin/supervisor…" 21 months ago Exited (0) 19 months ago dockerlnmp_php_1
  6. 9299363ef447 composer:latest "/docker-entrypoint.…" 21 months ago Exited (1) 19 months ago composer
  7. b4791a6baf69 c6f915833fb6 "/bin/sh -c 'mkdir -…" 21 months ago Exited (1) 21 months ago

2.docker run 参数

  1. [root@localhost zph]# docker run --help
  2. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  3. Run a command in a new container
  4. Options:
  5. --add-host list Add a custom host-to-IP mapping (host:ip)
  6. -a, --attach list Attach to STDIN, STDOUT or STDERR
  7. --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
  8. --blkio-weight-device list Block IO weight (relative device weight) (default [])
  9. --cap-add list Add Linux capabilities
  10. --cap-drop list Drop Linux capabilities
  11. --cgroup-parent string Optional parent cgroup for the container
  12. --cgroupns string Cgroup namespace to use (host|private)
  13. 'host': Run the container in the Docker host's cgroup namespace
  14. 'private': Run the container in its own private cgroup namespace
  15. '': Use the cgroup namespace as configured by the
  16. default-cgroupns-mode option on the daemon (default)
  17. --cidfile string Write the container ID to the file
  18. --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
  19. --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
  20. --cpu-rt-period int Limit CPU real-time period in microseconds
  21. --cpu-rt-runtime int Limit CPU real-time runtime in microseconds
  22. -c, --cpu-shares int CPU shares (relative weight)
  23. --cpus decimal Number of CPUs
  24. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  25. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  26. -d, --detach Run container in background and print container ID
  27. --detach-keys string Override the key sequence for detaching a container
  28. --device list Add a host device to the container
  29. --device-cgroup-rule list Add a rule to the cgroup allowed devices list
  30. --device-read-bps list Limit read rate (bytes per second) from a device (default [])
  31. --device-read-iops list Limit read rate (IO per second) from a device (default [])
  32. --device-write-bps list Limit write rate (bytes per second) to a device (default [])
  33. --device-write-iops list Limit write rate (IO per second) to a device (default [])
  34. --disable-content-trust Skip image verification (default true)
  35. --dns list Set custom DNS servers
  36. --dns-option list Set DNS options
  37. --dns-search list Set custom DNS search domains
  38. --domainname string Container NIS domain name
  39. --entrypoint string Overwrite the default ENTRYPOINT of the image
  40. -e, --env list Set environment variables
  41. --env-file list Read in a file of environment variables
  42. --expose list Expose a port or a range of ports
  43. --gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
  44. --group-add list Add additional groups to join
  45. --health-cmd string Command to run to check health
  46. --health-interval duration Time between running the check (ms|s|m|h) (default 0s)
  47. --health-retries int Consecutive failures needed to report unhealthy
  48. --health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
  49. --health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
  50. --help Print usage
  51. -h, --hostname string Container host name
  52. --init Run an init inside the container that forwards signals and reaps processes
  53. -i, --interactive Keep STDIN open even if not attached
  54. --ip string IPv4 address (e.g., 172.30.100.104)
  55. --ip6 string IPv6 address (e.g., 2001:db8::33)
  56. --ipc string IPC mode to use
  57. --isolation string Container isolation technology
  58. --kernel-memory bytes Kernel memory limit
  59. -l, --label list Set meta data on a container
  60. --label-file list Read in a line delimited file of labels
  61. --link list Add link to another container
  62. --link-local-ip list Container IPv4/IPv6 link-local addresses
  63. --log-driver string Logging driver for the container
  64. --log-opt list Log driver options
  65. --mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
  66. -m, --memory bytes Memory limit
  67. --memory-reservation bytes Memory soft limit
  68. --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
  69. --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
  70. --mount mount Attach a filesystem mount to the container
  71. --name string Assign a name to the container
  72. --network network Connect a container to a network
  73. --network-alias list Add network-scoped alias for the container
  74. --no-healthcheck Disable any container-specified HEALTHCHECK
  75. --oom-kill-disable Disable OOM Killer
  76. --oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
  77. --pid string PID namespace to use
  78. --pids-limit int Tune container pids limit (set -1 for unlimited)
  79. --platform string Set platform if server is multi-platform capable
  80. --privileged Give extended privileges to this container
  81. -p, --publish list Publish a container's port(s) to the host
  82. -P, --publish-all Publish all exposed ports to random ports
  83. --pull string Pull image before running ("always"|"missing"|"never") (default "missing")
  84. --read-only Mount the container's root filesystem as read only
  85. --restart string Restart policy to apply when a container exits (default "no")
  86. --rm Automatically remove the container when it exits
  87. --runtime string Runtime to use for this container
  88. --security-opt list Security Options
  89. --shm-size bytes Size of /dev/shm
  90. --sig-proxy Proxy received signals to the process (default true)
  91. --stop-signal string Signal to stop a container (default "SIGTERM")
  92. --stop-timeout int Timeout (in seconds) to stop a container
  93. --storage-opt list Storage driver options for the container
  94. --sysctl map Sysctl options (default map[])
  95. --tmpfs list Mount a tmpfs directory
  96. -t, --tty Allocate a pseudo-TTY
  97. --ulimit ulimit Ulimit options (default [])
  98. -u, --user string Username or UID (format: <name|uid>[:<group|gid>])
  99. --userns string User namespace to use
  100. --uts string UTS namespace to use
  101. -v, --volume list Bind mount a volume
  102. --volume-driver string Optional volume driver for the container
  103. --volumes-from list Mount volumes from the specified container(s)
  104. -w, --workdir string Working directory inside the container
常用命令如下:
docker run:
        创建一个新的容器并运行一个命令
        docker run 是日常用的最频繁用的命令之一,同样也是较为复杂的命令之一
命令格式:
         docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS :选项
        -i :表示启动一个可交互的容器,并持续打开标准输入
        -t: 表示使用终端关联到容器的标准输入输出上
        -d :表示容器放置后台运行
        --rm:退出后即删除容器
        --name :表示定义容器唯一名称
        -p 映射端口
        -v 指定路径挂载数据卷
        -e 运行容器传递环境变量
IMAGE :
        表示要运行的镜像
COMMAND :
        表示启动容器时要运行的命令

3.-it 启动一个交互式容器

docker run 启动一个交互式容器在容器内执行/bin/bash 命令:

docker run -it --name centos7 centos /bin/bash 或者 docker run -it  --name centos7 5d0da3dc9764  /bin/bash 

5d0da3dc9764   为centos镜像的IMAGE ID, --name 给这个容器命名

  1. [root@localhost zph]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
  4. centos latest 5d0da3dc9764 2 years ago 231MB
  5. [root@localhost zph]# docker run -it --name centos7 9c7a54a9a43c /bin/bash
  6. [root@8780efa9cf48 /]# ls
  7. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var

可以在容器中执行操作命令,如:ls cat,还可以在容器中安装一些软件,相当于把容器当作一个操作系统,只不过这个操作系统是一个比较精简的操作系统而已 

  1. [root@0f0b83575aa4 /]# 进入容器后查看容器的IP地址
  2. [root@0f0b83575aa4 /]# ifconfig
  3. bash: ifconfig: command not found
  4. #s说明没有这个命令,使用yum安装命令操作
  5. [root@0f0b83575aa4 /]# yum install net-tools
  6. Loaded plugins: fastestmirror, ovl
  7. Determining fastest mirrors
  8. * base: mirrors.aliyun.com
  9. * extras: mirrors.aliyun.com
  10. * updates: mirrors.aliyun.com
  11. ...
  12. #安装成功后,使用ifconfig查看就可以了
  13. [root@0f0b83575aa4 /]# ifconfig
  14. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  15. inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
  16. ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
  17. RX packets 17779 bytes 32924504 (31.3 MiB)
  18. RX errors 0 dropped 0 overruns 0 frame 0
  19. TX packets 8464 bytes 495661 (484.0 KiB)
  20. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  21. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  22. inet 127.0.0.1 netmask 255.0.0.0
  23. loop txqueuelen 1000 (Local Loopback)
  24. RX packets 0 bytes 0 (0.0 B)
  25. RX errors 0 dropped 0 overruns 0 frame 0
  26. TX packets 0 bytes 0 (0.0 B)
  27. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 退出容器

        exit: 容器停止并退出

        ctrl+p+q容器不停止退出

4.--rm 启动一个退出即删除容器 

创建一个非交互式的容器用完就删除,也就是退出容器后就销毁
docker run --rm centos7 /bin/bash
创建一个交互式的容器用完就删
docker run -it --rm centos7 /bin/bash

exit 退出后,执行命令docker ps -a,发现上面的centos7已经被销毁了,没有这个记录

5.-d 启动一个后台容器

开启一个后台交互的容器,注意启动的时候加上-it,如果不加的话 docker 启动容器会自动停

止,也就是说,加上-d后,就是启动这个容器,然后在后台运行,不进入这个容器,eg:
  1. # 启动一个centos容器,并命名,执行/bin/bash,在后台运行,不进入容器内部
  2. [root@localhost zph]# docker run -it --name centos7-bak -d eeb6ee3f44bd /bin/bash
  3. # 该命令执行后,出现下面字符串
  4. 48830cb9e3177724a977a0ba1d6f0a1d06ceca016a148a7e9358bab9da3fe652
  5. [root@localhost zph]#
  6. #查看容器:发现存在cents7-bak这个容器,说明了上面docker run 命令的正确性
  7. [root@localhost zph]# docker ps
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. 48830cb9e317 eeb6ee3f44bd "/bin/bash" 15 seconds ago Up 13 seconds centos7-bak

 6.exec 进入置为后台已经启动的容器

进入一个容器可以使用 docker exec 或者 docker attac,用得最多的是docker exec这个命令,文档如下:    
  1. [root@localhost zph]# docker exec --help
  2. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  3. Run a command in a running container
  4. Options:
  5. -d, --detach Detached mode: run command in the background
  6. --detach-keys string Override the key sequence for detaching a container
  7. -e, --env list Set environment variables
  8. --env-file list Read in a file of environment variables
  9. -i, --interactive Keep STDIN open even if not attached
  10. --privileged Give extended privileges to the command
  11. -t, --tty Allocate a pseudo-TTY
  12. -u, --user string Username or UID (format: <name|uid>[:<group|gid>])
  13. -w, --workdir string Working directory inside the container

 docker attach文档:

  1. [root@localhost zph]# docker attach --help
  2. Usage: docker attach [OPTIONS] CONTAINER
  3. Attach local standard input, output, and error streams to a running container
  4. Options:
  5. --detach-keys string Override the key sequence for detaching a container
  6. --no-stdin Do not attach STDIN
  7. --sig-proxy Proxy all received signals to the process (default true)

 两个命令的区别:

        docker exec: 进入容器开启一个新的终端(常用) 执行 exit 退出的时候不会停止容器,使用docker ps 查看时,容器存在,不会被销毁
        docker attach: 进入容器正在执行的终端 exit 退出会停止容器,docker ps查看时,容器不存在,已经被销毁了
  1. #查看正在运行的容器
  2. []root@localhost zph]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 48830cb9e317 eeb6ee3f44bd "/bin/bash" 12 minutes ago Up 12 minutes centos7-bak
  5. #进入容器,开启一个终端
  6. [root@localhost zph]# docker exec -it 48830cb9e317 /bin/bash

7.start 启动 stop 停止 restart 重启容器 exit 退出容器 

  1. #docker ps -a: 查看所有容器(包括正在运行的以及被销毁的)
  2. #docker start: 容器名/容器id 启动容器
  3. #docker stop: 容器名/容器id 停止容器
  4. #docker restart: 容器名/容器id 重启容器
  5. #退出容器
  6. # exit: 容器停止并退出
  7. # ctrl+p+q:容器不停止退出

8.删除容器

  1. #docker rm 容器名/容器id :删除一个已经停止的容器,如果删除的容器在运行中,会弹出一个警告
  2. #docker rm -f 容器名/容器id :强制删除一个容器,不管是否在运行
  3. #docker rm -f $(docker ps -q) :删除所有容器()docker ps -q: 获取容器id)
  4. #docker rm $(docker ps -qf status=exited) :根据容器的状态,删除 Exited 状态的容器(docker ps -qf status=exited:筛选获取status=exited的容器id)

9.docker ps讲解

  1. [root@localhost zph]# docker ps --help
  2. Usage: docker ps [OPTIONS]
  3. List containers
  4. Options:
  5. -a, --all Show all containers (default shows just running):展示所有容器
  6. -f, --filter filter Filter output based on conditions provided:筛选
  7. --format string Pretty-print containers using a Go template
  8. -n, --last int Show n last created containers (includes all states) (default -1)
  9. -l, --latest Show the latest created container (includes all states)
  10. --no-trunc Don't truncate output
  11. -q, --quiet Only display container IDs:展示容器id
  12. -s, --size Display total file sizes

10查看容器操作日志

  1. #docker logs 帮助文档
  2. [root@localhost zph]# docker logs --help
  3. Usage: docker logs [OPTIONS] CONTAINER
  4. Fetch the logs of a container
  5. Options:
  6. --details Show extra details provided to logs
  7. -f, --follow Follow log output: 跟踪日志输出
  8. --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) :显示某个开始时间的所有日志
  9. -n, --tail string Number of lines to show from the end of the logs (default "all"):仅列出最新 N 条容器日
  10. -t, --timestamps Show timestamps:显示时间戳
  11. --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  12. #运行容器,给容器取一个别名,并进入容器的bash
  13. [root@localhost zph]# docker run -it --name centos7 eeb6ee3f44bd /bin/bash
  14. [root@63b354d40a1a /]#
  15. [root@63b354d40a1a /]# cd /home/
  16. [root@63b354d40a1a home]# ll
  17. total 0
  18. [root@63b354d40a1a home]# mkdir git
  19. [root@63b354d40a1a home]# cd git/
  20. [root@63b354d40a1a git]# touch text.txt
  21. [root@63b354d40a1a git]# ll
  22. total 0
  23. -rw-r--r-- 1 root root 0 Oct 24 15:26 text.txt
  24. #执行ctrl+q+p:退出不停止容器操作
  25. [root@63b354d40a1a git]# ^P[root@localhost zph]#
  26. #查看正在运行的容器
  27. [root@localhost zph]# docker ps
  28. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  29. 63b354d40a1a eeb6ee3f44bd "/bin/bash" About a minute ago Up About a minute centos7
  30. #查看进入容器后的操作日志
  31. [root@localhost zph]# docker logs 63b354d40a1a
  32. [root@63b354d40a1a /]#
  33. [root@63b354d40a1a /]# cd /home/
  34. [root@63b354d40a1a home]# ll
  35. total 0
  36. [root@63b354d40a1a home]# mkdir git
  37. [root@63b354d40a1a home]# cd git/
  38. [root@63b354d40a1a git]# touch text.txt
  39. [root@63b354d40a1a git]# ll
  40. total 0
  41. -rw-r--r-- 1 root root 0 Oct 24 15:26 text.txt
  42. [root@localhost zph]#

11.docker commit 容器转换为镜像

镜像是没有写入权限的,但是可以修改容器把容器制作为镜像,通过镜像启动一个容器 给容器写入内容,安装相关的软件,然后把这个容器转换为镜像,然后打包上传到hub,别人就可以使用该镜像了,以alpine为例:

  1. #拉取alpine镜像
  2. [root@localhost zph]# docker pull alpine
  3. Using default tag: latest
  4. latest: Pulling from library/alpine
  5. 96526aa774ef: Pull complete
  6. Digest: sha256:eece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978
  7. Status: Downloaded newer image for alpine:latest
  8. docker.io/library/alpine:latest
  9. #查看是否存在alpine镜像
  10. [root@localhost zph]# docker images
  11. REPOSITORY TAG IMAGE ID CREATED SIZE
  12. alpine latest 8ca4688f4f35 3 weeks ago 7.34MB
  13. #运行alpine镜像,启动容器,并给容器命名,执行/bin/bash命令
  14. [root@localhost zph]# docker run -it --name myalpine 8ca4688f4f35 /bin/bash
  15. docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown.
  16. #上面报错了: 提示没有/bin/bash这个命令
  17. #重新启动容器,执行/bin/sh命令
  18. [root@localhost zph]# docker run -it --name myalpine 8ca4688f4f35 /bin/sh
  19. docker: Error response from daemon: Conflict. The container name "/myalpine" is already in use by container "959ece20098efb44b3c1744bb5df49a4d4caa78c362fd0eacc22fc53494f9f65". You have to remove (or rename) that container to be able to reuse that name.
  20. #还是报错了:提示容器已经存在,容器名称存在了
  21. #通过docker ps -a 查看,果然刚才启动的容器存在,解决办法:删除这个容器
  22. root@localhost zph]# docker ps -a
  23. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  24. 959ece20098e 8ca4688f4f35 "/bin/bash" 40 seconds ago Created myalpine
  25. #删除容器
  26. [root@localhost zph]# docker rm 959ece20098e
  27. 959ece20098e
  28. #重新执行镜像,启动容器,并执行/bin/sh 命令,然后在容器中写入内容,退出
  29. [root@localhost zph]# docker run -it --name myalpine 8ca4688f4f35 /bin/sh
  30. / # cd root/
  31. ~ # ls
  32. ~ # echo test docker commit > test.txt
  33. ~ # ls
  34. test.txt
  35. ~ # exit
  36. #查看容器:发现myalpine已经退出,因为执行了exit,没问题
  37. [root@localhost zph]# docker ps -a
  38. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  39. c37dce69c265 8ca4688f4f35 "/bin/sh" About a minute ago Exited (0) 22 seconds ago myalpine
  40. #把myalpine制作成镜像,使用docker commit 命令
  41. #查看docker commit命令文档: docker commit [参数:-a,-c,...] 容器名/容器id 镜像名[:版本]
  42. [root@localhost zph]# docker commit --help
  43. Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
  44. Create a new image from a container's changes
  45. Options:
  46. -a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  47. -c, --change list Apply Dockerfile instruction to the created image
  48. -m, --message string Commit message
  49. -p, --pause Pause container during commit (default true)
  50. #制作镜像
  51. [root@localhost zph]# docker commit c37dce69c265 docker.io/library/myalpine:v1
  52. sha256:dd14c18efe091413581205f414dacca80d954e85cfb28208e7e610e1fb3c4f6e
  53. #查看是否制作镜像成功
  54. [root@localhost zph]# docker images
  55. REPOSITORY TAG IMAGE ID CREATED SIZE
  56. myalpine v1 dd14c18efe09 14 seconds ago 7.34MB
  57. #上面存在myalpine:v1,说明制作镜像成功了
  58. #验证镜像: 运行镜像,开启这个容器
  59. [root@localhost zph]# docker run -it -d --name myalpine.v1 dd14c18efe09 /bin/sh
  60. cde912eb360c67b8becd8d70562929b08205440f8ba552b6eea80e51ebb20f78
  61. #查看这个容器是否启动
  62. [root@localhost zph]# docker ps
  63. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  64. cde912eb360c dd14c18efe09 "/bin/sh" 9 seconds ago Up 5 seconds myalpine.v1
  65. #进入容器验证
  66. [root@localhost zph]# docker exec -it cde912eb360c /bin/sh
  67. / # cd root/
  68. ~ # ls
  69. test.txt
  70. ~ # cat test.txt
  71. test docker commit
  72. ~ # exit
  73. #里面有创建的text.txt,说明镜像创建成功

12.镜像的导入导出

可以把制作好的镜像发布到 dockerHub上,也可以把制作好的镜像导出发给别人

  1. #查看所有镜像
  2. [root@localhost zph]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. myalpine v1 dd14c18efe09 14 hours ago 7.34MB
  5. #导出镜像
  6. [root@localhost zph]# docker save myalpine > myalpine.tar
  7. [root@localhost zph]# ll
  8. 总用量 7464
  9. drwxr-xr-x. 2 zph zph 6 1223 2021 公共
  10. drwxr-xr-x. 2 zph zph 6 1223 2021 模板
  11. drwxr-xr-x. 2 zph zph 6 1223 2021 视频
  12. drwxr-xr-x. 2 zph zph 6 1223 2021 图片
  13. drwxr-xr-x. 2 zph zph 6 1223 2021 文档
  14. drwxr-xr-x. 2 zph zph 6 1223 2021 下载
  15. drwxr-xr-x. 2 zph zph 6 1223 2021 音乐
  16. drwxr-xr-x. 2 zph zph 6 1223 2021 桌面
  17. -rw-r--r-- 1 root root 7641600 1025 07:26 myalpine.tar
  18. #删除镜像
  19. [root@localhost zph]# docker rmi dd14c18efe09
  20. #查看所有镜像
  21. [root@localhost zph]# docker images
  22. REPOSITORY TAG IMAGE ID CREATED SIZE
  23. #导入镜像
  24. [root@localhost zph]# docker load < myalpine.tar
  25. Loaded image: myalpine:v1
  26. #查看所有镜像
  27. [root@localhost zph]# docker images
  28. REPOSITORY TAG IMAGE ID CREATED SIZE
  29. myalpine v1 dd14c18efe09 14 hours ago 7.34MB
  30. #校验镜像是否成功
  31. [root@localhost zph]# docker run -it --name myalpine.v1 dd14c18efe09 /bin/bash
  32. #不报错就成功了

13.docker cp 实现数据拷贝

docker cp :用于容器与主机之间的数据拷贝

  1. [root@localhost zph]# docker cp --help
  2. Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
  3. docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
  4. Copy files/folders between a container and the local filesystem
  5. Use '-' as the source to read a tar archive from stdin
  6. and extract it to a directory destination in a container.
  7. Use '-' as the destination to stream a tar archive of a
  8. container source to stdout.
  9. Options:
  10. -a, --archive Archive mode (copy all uid/gid information)
  11. -L, --follow-link Always follow symbol link in SRC_PATH

  1. #查看目录
  2. [root@localhost zph]# pwd
  3. /home/zph
  4. #创建文件夹
  5. [root@localhost zph]# mkdir www
  6. [root@localhost zph]# cd www/
  7. [root@localhost www]# ll
  8. 总用量 0
  9. #创建文件并写入数据
  10. [root@localhost www]# echo test.docker > test.txt
  11. [root@localhost www]# ll
  12. 总用量 4
  13. -rw-r--r-- 1 root root 12 10月 25 07:51 test.txt
  14. [root@localhost www]# cd ..
  15. #查看images
  16. [root@localhost zph]# docker images
  17. REPOSITORY TAG IMAGE ID CREATED SIZE
  18. centos 7 eeb6ee3f44bd 2 years ago 204MB
  19. #查看运行的容器
  20. [root@localhost zph]# docker ps
  21. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  22. 63b354d40a1a eeb6ee3f44bd "/bin/bash" 23 hours ago Up 23 hours centos7
  23. #进入容器
  24. [root@localhost zph]# docker exec -it 63b354d40a1a /bin/bash
  25. #查看/下是否存在www目录:发现不存在该目录
  26. [root@63b354d40a1a /]# ll
  27. total 12
  28. -rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
  29. lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
  30. drwxr-xr-x 5 root root 360 Oct 24 15:25 dev
  31. drwxr-xr-x 1 root root 66 Oct 24 15:24 etc
  32. drwxr-xr-x 1 root root 17 Oct 24 15:26 home
  33. lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
  34. lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
  35. drwxr-xr-x 2 root root 6 Apr 11 2018 media
  36. drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
  37. drwxr-xr-x 2 root root 6 Apr 11 2018 opt
  38. dr-xr-xr-x 255 root root 0 Oct 24 15:24 proc
  39. dr-xr-x--- 2 root root 114 Nov 13 2020 root
  40. drwxr-xr-x 11 root root 148 Nov 13 2020 run
  41. lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
  42. drwxr-xr-x 2 root root 6 Apr 11 2018 srv
  43. dr-xr-xr-x 13 root root 0 Oct 24 15:24 sys
  44. drwxrwxrwt 7 root root 132 Nov 13 2020 tmp
  45. drwxr-xr-x 13 root root 155 Nov 13 2020 usr
  46. drwxr-xr-x 18 root root 238 Nov 13 2020 var
  47. #退出
  48. [root@63b354d40a1a /]# exit
  49. exit
  50. #cp /home/zph/www/目录下的所有文件到容器的/下
  51. [root@localhost zph]# docker cp /home/zph/www/ 63b354d40a1a:/
  52. #进入容器查看是否cp成功
  53. [root@localhost zph]# docker exec -it 63b354d40a1a /bin/bash
  54. #ll查看:发现存在www,说明cp成功了
  55. [root@63b354d40a1a /]# ll
  56. total 12
  57. -rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
  58. lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
  59. drwxr-xr-x 5 root root 360 Oct 24 15:25 dev
  60. drwxr-xr-x 1 root root 66 Oct 24 15:24 etc
  61. drwxr-xr-x 1 root root 17 Oct 24 15:26 home
  62. lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
  63. lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
  64. drwxr-xr-x 2 root root 6 Apr 11 2018 media
  65. drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
  66. drwxr-xr-x 2 root root 6 Apr 11 2018 opt
  67. dr-xr-xr-x 255 root root 0 Oct 24 15:24 proc
  68. dr-xr-x--- 1 root root 27 Oct 25 14:53 root
  69. drwxr-xr-x 11 root root 148 Nov 13 2020 run
  70. lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
  71. drwxr-xr-x 2 root root 6 Apr 11 2018 srv
  72. dr-xr-xr-x 13 root root 0 Oct 24 15:24 sys
  73. drwxrwxrwt 7 root root 132 Nov 13 2020 tmp
  74. drwxr-xr-x 13 root root 155 Nov 13 2020 usr
  75. drwxr-xr-x 18 root root 238 Nov 13 2020 var
  76. drwxr-xr-x 2 root root 22 Oct 25 14:51 www
  77. [root@63b354d40a1a /]# cd www/
  78. [root@63b354d40a1a www]# ll
  79. total 4
  80. -rw-r--r-- 1 root root 12 Oct 25 14:51 test.txt
  81. #在www下新建文件并写入数据,然后退出
  82. [root@63b354d40a1a www]# echo test2.com > test2.txt
  83. [root@63b354d40a1a www]# exit
  84. exit
  85. #cp容器中的www下的所有文件到系统的/var下
  86. [root@localhost zph]# docker cp 63b354d40a1a:www/ /var
  87. #查看是否cp成功:发现成功了
  88. [root@localhost zph]# ll /var/www/
  89. 总用量 8
  90. -rw-r--r-- 1 root root 10 10月 25 07:54 test2.txt
  91. -rw-r--r-- 1 root root 12 10月 25 07:51 test.txt
  92. [root@localhost zph]#

[上一节][Docker]一.Docker 简介与安装

[下一节][Docker]三.Docker 部署nginx,以及映射端口,挂载数据卷 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/80409
推荐阅读
相关标签
  

闽ICP备14008679号