01-04尚硅谷周阳老师Docker课程笔记

学习方法论

概念 -- 这个东西是什么?

作用 -- 这个东西什么作用?  好处是啥

怎么下载  -- 官网

怎么玩 -- 使用 实践 命令

实践 -- 永远hello world

1.AB法则

Before

1.配置环境、测试环境、开发环境等不同,所以运维部署的时候就会发现版本、配置不同,造成问题
开发 : 这里能跑   运维:我这跑不起来
开发 :只提供src源代码,list清单
运维 : 根据清单进行环境安装(mysql、redis等版本),扩容、缩容(3主3从等),很麻烦,参数配错一台就跑不起来

熟悉
    优点: 一次镜像,处处运行,从搬家到搬楼
    假设没有VM,直接装centos7 linux系统,有的是win10,win7,还有电脑不一样,可能会导致各种问题,在同一个VM上装同一个镜像文件
 

After

Docker : 把源代码+配置+环境+版本进行打包成镜像镜像,使用docker进行打包。 
docker就是轻型的VMware

陌生:
    项目-redis、mysql、mq打包成一个镜像[相当于把centos7打包成一个镜像],将上述代码+运行环境+配置文件+特殊的第三方包...打包成一个镜像运行到docker引擎上

2.Docker简介

Docker是什么

系统平滑移植,容器虚拟化技术

Docker的出现是为了解决代码在本机可以正常运行,而部署到其他机器不能运行的问题。这是因为代码运行所需要的环境、系统、配置、数据等不同,Docker透过镜像将程序运行所需要的系统环境由下而上打包,达到应用程序跨平台间的无缝接轨运行。

Linux 容器技术的出现就解决了这样一个问题,而 Docker 就是在它的基础上发展过来的。将应用运行在 Docker 容器上面,而 Docker 容器在任何操作系统上都是一致的,这就实现了跨平台、跨服务器。只需要一次配置好环境,换到别的机子上就可以一键部署好,大大简化了操作

Docker理念

docker是基于Go语言实现的云开源项目

docker主要目标“ Build, ship and run any app, anywhere”,也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,使用户的APP以及运行环境能够做到**一次封装,到处运行**

Docker解决了运行环境和配置问题软件容器,方便做持续集成并有助于整体发布的容器虚拟化技术

容器和虚拟机的区别

虚拟机 : 占用资源多、冗余动作多,启动慢,把整个操作系统都安装
Docker容器是在操作系统层面实现虚拟化,直接复用本地主机的操作系统,而传统的虚拟机则是在硬件层面实现虚拟化。与传统的虚拟机相比,docker优势体现在启动速度快、占用体积小
Docker容器只需要最小的linux内核[就是需要启动的最小环境]

Docker能干嘛

虚拟机就是带环境安装的一种解决方案。它可以在一种操作系统里面运行另一种操作系统,如windows上运行Linux系统。应用程序无感知。
 

开发和运维

更快速的应用交付和部署
更便捷的升级和扩缩容
更简单的系统运维
更高效的计算资源利用

Docker优势

轻量,秒级的快速启动速度
简单、易用、活跃的社区
标准统一的打包/部署/运行方案
镜像支持增量分发,易于部署
易于构建,良好的REST API,也很适合自动化测试和持续集成
性能,尤其是内存和IO开销

Docker官网

Docker官网: docker.com
Docker中文网站:docker-cn.com/
Docker Hub 官网: hub.docker.com/

Docker安装

1.centOS安装docker

yum install -y epel-release
yum install -y docker-io
# 安装后的配置文件:/etc/sysconfig/docker
service docker start # 启动docker后台服务
docker version # 查看版本,验证是否安装成功
 

2.ubuntu安装docker

curl -sSL https://get.daocloud.io/docker | sh

安装教程可查看:https://docs.docker.com/engine/install/ubuntu/

3.配置阿里云镜像加速器:

查看是否是阿里云加速器

ps -ef |grep docker

解决docker权限问题:

sudo gpasswd -a username docker #将普通用户username加入到docker组
newgrp docker #更新docker组

4.测试

docker run hello-world

run命令

底层原理

Docker是怎么工作的

Docker是一个 Client-Server 结构的系统,Docker守护进程运行在主机上,然后通过Socket连接从客户端访问,守护进程从客户端接收命令并管理运行在主机上的容器。
容器,是一个运行时环境,就是我们前面说到的集装箱。

为什么Dcoker比VM快

Docker比VM抽象层更少,Docker不需要Hypervisor实现硬件资源虚拟化,运行在Docker容器上的程序直接使用的都是实际物理机资源。因此在CPU、内存利用率上Docker将会在效率上有明显的优势
Docker利用的是宿主机内核,而不需要Guest OS。因此,当新建一个容器时,Docker不需要和虚拟机一样重新加载一个操作系统内核。
新建虚拟机时,虚拟机软件需要加载Guest OS,返回新建过程是分钟级的。Docker直接利用宿主机的操作系统,新建一个Docker只需要几秒钟
 

3.Docker常用命令

帮助命令

查看 docker版本 :docker version

查看docker帮助文档 : docker --help
查看docker具体命令帮助文档 : docker 具体命令--help
启动docker : systemctl start docker
停止docker : systemctl stop docker
重启docker : systemctl restart docker
查看docker状态 : systemctl status docker
开机启动 : systemctl enable docker
查看docker概要信息 :  docker info
 

[root@node001 docker]# docker status docker
docker: 'status' is not a docker command.
See 'docker --help'
[root@node001 docker]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 五 2023-05-26 15:14:55 CST; 17min ago
     Docs: https://docs.docker.com
 Main PID: 4160 (dockerd)
    Tasks: 11
   Memory: 30.2M
   CGroup: /system.slice/docker.service
           └─4160 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
 
5月 26 15:14:54 node001 dockerd[4160]: time="2023-05-26T15:14:54.528872384+08:00" level=info msg="Starting up"
5月 26 15:14:54 node001 dockerd[4160]: time="2023-05-26T15:14:54.612128512+08:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
5月 26 15:14:54 node001 dockerd[4160]: time="2023-05-26T15:14:54.613619570+08:00" level=info msg="Loading containers: start."
5月 26 15:14:55 node001 dockerd[4160]: time="2023-05-26T15:14:55.000236197+08:00" level=info msg="Default bridge (docker0) is assigned with an IP ... address"
5月 26 15:14:55 node001 dockerd[4160]: time="2023-05-26T15:14:55.128860025+08:00" level=info msg="Loading containers: done."
5月 26 15:14:55 node001 dockerd[4160]: time="2023-05-26T15:14:55.180968994+08:00" level=info msg="Docker daemon" commit=463850e graphdriver=overla...on=24.0.1
5月 26 15:14:55 node001 dockerd[4160]: time="2023-05-26T15:14:55.181125988+08:00" level=info msg="Daemon has completed initialization"
5月 26 15:14:55 node001 dockerd[4160]: time="2023-05-26T15:14:55.270751801+08:00" level=info msg="API listen on /run/docker.sock"
5月 26 15:14:55 node001 systemd[1]: Started Docker Application Container Engine.
5月 26 15:15:19 node001 dockerd[4160]: time="2023-05-26T15:15:19.315721290+08:00" level=info msg="ignoring event" container=3ff6498fa0a7d34b5995ac...skDelete"
Hint: Some lines were ellipsized, use -l to show in full.
[root@node001 docker]# docker --help
Usage:  docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information
Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.10.4)
  compose*    Docker Compose (Docker Inc., v2.18.1)
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes
Swarm Commands:
  swarm       Manage Swarm
Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  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
  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
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  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
  wait        Block until one or more containers stop, then print their exit codes
Global 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 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
Run 'docker COMMAND --help' for more information on a command.
For more help on how to use Docker, head to https://docs.docker.com/go/guides/
[root@node001 docker]# 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.
Aliases:
  docker container cp, docker cp
Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH
  -q, --quiet         Suppress progress output during copy. Progress output is automatically suppressed if no terminal is attached
[root@node001 docker]# 
[root@node001 atguigu]# man docker-run
[root@node001 atguigu]# 

镜像命令

docker image   列出本地主机上的镜像
  OPTIONS说明:
    -a:列出本地所有的镜像(含中间映像层)
    -q:只显示镜像ID
    --digests:显示镜像的摘要信息
    --no-trunc:显示完整的镜像信息
docker search 镜像名称
docker search [OPTIONS] 镜像名字
  OPTIONS说明
    --no-trunc:显示完整的镜像描述
    -s:列出收藏数不小于指定值的镜像
    --automated:只列出 automated build 类型的镜像
docker pull 镜像名称   下载镜像

        docker pull mysql:5.7
docker system df 查看镜像/容器/数据卷所占的空间
docker rmi 镜像名称或ID
    + 删除镜像
    + 删除单个
      docker rmi -f 镜像ID
    + 删除多个
      docker rmi -f 镜像名1:TAG 镜像名2:TAG
    + 删除全部
     docker rmi -f $(docker images -qa)

[root@node001 atguigu]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    9c7a54a9a43c   7 weeks ago   13.3kB
[root@node001 atguigu]# docker image is
 
Usage:  docker image COMMAND
 
Manage images
 
Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Download an image from a registry
  push        Upload an image to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
 
Run 'docker image COMMAND --help' for more information on a command.
[root@node001 atguigu]# docker search hello-world
NAME                                       DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   2051      [OK]       
rancher/hello-world                                                                         4                    
okteto/hello-world                                                                          0                    
golift/hello-world                         Hello World Go-App built by Go Lift Applicat…   0                    
tacc/hello-world                                                                            0                    
armswdev/c-hello-world                     Simple hello-world C program on Alpine Linux…   0                    
tutum/hello-world                          Image to test docker deployments. Has Apache…   90                   [OK]
thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   2                    
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   152                  
dockercloud/hello-world                    Hello World!                                     20                   [OK]
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK]
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   2                    [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB           17                   [OK]
strimzi/hello-world-consumer                                                                0                    
strimzi/hello-world-producer                                                                0                    
businessgeeks00/hello-world-nodejs                                                          0                    
koudaiii/hello-world                                                                        0                    
freddiedevops/hello-world-spring-boot                                                       0                    
strimzi/hello-world-streams                                                                 0                    
garystafford/hello-world                   Simple hello-world Spring Boot service for t…   0                    [OK]
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                    
tsepotesting123/hello-world                                                                 0                    
kevindockercompany/hello-world                                                              0                    
dandando/hello-world-dotnet                                                                 0                    
vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   7                    [OK]
[root@node001 atguigu]# docker search mysql
NAME                            DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14263     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5453      [OK]       
percona                         Percona Server is a fork of the MySQL relati…   616       [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   829       [OK]       
bitnami/mysql                   Bitnami MySQL Docker Image                       90                   [OK]
circleci/mysql                  MySQL is a widely used, open-source relation…   29                   
bitnami/mysqld-exporter                                                          5                    
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   51                   
cimg/mysql                                                                       0                    
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL    23                   
rapidfort/mysql8-ib             RapidFort optimized, hardened image for MySQ…   9                    
google/mysql                    MySQL server for Google Compute Engine           23                   [OK]
hashicorp/mysql-portworx-demo                                                    0                    
rapidfort/mysql-official        RapidFort optimized, hardened image for MySQ…   7                    
newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL databa…   1                    [OK]
databack/mysql-backup           Back up mysql databases to... anywhere!          86                   
bitnamicharts/mysql                                                              0                    
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   38                   
mirantis/mysql                                                                   0                    
docksal/mysql                   MySQL service images for Docksal - https://d…   0                    
linuxserver/mysql-workbench                                                      50                   
vitess/mysqlctld                vitess/mysqlctld                                 1                    [OK]
eclipse/mysql                   Mysql 5.7, curl, rsync                           0                    [OK]
drupalci/mysql-5.5              https://www.drupal.org/project/drupalci          3                    [OK]
drupalci/mysql-5.7              https://www.drupal.org/project/drupalci          0                    
[root@node001 atguigu]# docker search minio
NAME                           DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
bitnami/minio                  Bitnami MinIO Docker Image                       95                   
bitnami/minio-client           Bitnami MinIO Client Docker Image                11                   
rancher/minio-minio                                                             1                    
bitnamicharts/minio                                                             0                    
corpusops/minio                https://github.com/corpusops/docker-images/      0                    
longhornio/minio                                                                0                    
drud/minio                                                                      0                    
hephy/minio                                                                     0                    
silintl/minio-default-server                                                    0                    
corpusops/minio-k8s-operator   https://github.com/corpusops/docker-images/      0                    
corpusops/minio-mint           https://github.com/corpusops/docker-images/      0                    
newrelic/synthetics-minion     Synthetics Containerized Private Minion          5                    
corpusops/minio-doctor         https://github.com/corpusops/docker-images/      0                    
drud/minio-exporter                                                             0                    
vulhub/minio                                                                    0                    
jumpserver/minio               Multi-Cloud Object Storage                       0                    
minio/minio                    Multi-Cloud Object Storage                       723                  [OK]
minio/mc                       Minio Client (mc) provides a modern alternat…   41                   [OK]
minio/console                  A graphical user interface for MinIO server      15                   
jumpserver/mc                  Minio Client (mc) provides a modern alternat…   0                    
minio/k8s-operator             Minio Operator for k8s https://kubernetes.io/    1                    
minio/operator                                                                  3                    
minio/kes                      Description  Kes is a tool for managing and …   2                    
minio/direct-csi               Container Storage Interface (CSI) driver for…   0                    
minio/logsearchapi                                                              2                    
[root@node001 atguigu]# docker search redis
NAME                                DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
redis                               Redis is an open source key-value store that…   12181     [OK]       
redislabs/redisearch                Redis With the RedisSearch module pre-loaded…   56                   
redislabs/redisinsight              RedisInsight - The GUI for Redis                 88                   
redislabs/rebloom                   A probablistic datatypes module for Redis        23                   [OK]
redislabs/redis                     Clustered in-memory database engine compatib…   38                   
redis/redis-stack-server            redis-stack-server installs a Redis server w…   45                   
redislabs/rejson                    RedisJSON - Enhanced JSON data type processi…   53                   
redis/redis-stack                   redis-stack installs a Redis server with add…   54                   
redislabs/redisgraph                A graph database module for Redis                26                   [OK]
redislabs/redismod                  An automated build of redismod - latest Redi…   41                   [OK]
redislabs/redistimeseries           A time series database module for Redis          12                   
redislabs/operator                                                                   7                    
redislabs/operator-internal         This repository contains pre-released versio…   1                    
redislabs/redis-py                                                                   5                    
redislabs/redis-webcli              A tiny Flask app to provide access to Redis …   3                    [OK]
redislabs/redisgears                An automated build of RedisGears                 4                    
redislabs/k8s-controller-internal                                                    0                    
redislabs/memtier_benchmark         Docker image to run memtier_benchmark            0                    
redislabs/ng-redis-raft             Redis with redis raft module                     0                    
redislabs/k8s-controller                                                             2                    
redislabs/redisai                                                                    5                    
redislabs/olmtest                   Test artefact for OLM CSV                        0                    
bitnami/redis                       Bitnami Redis Docker Image                       259                  [OK]
redislabs/olm-bundle                                                                 0                    
redislabs/redisml                   A Redis module that implements several machi…   3                    [OK]
[root@node001 atguigu]# docker search --limit  redis
invalid argument "redis" for "--limit" flag: strconv.ParseInt: parsing "redis": invalid syntax
See 'docker search --help'.
[root@node001 atguigu]# docker search --limit 5 redis
NAME                     DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
redis                    Redis is an open source key-value store that…   12181     [OK]       
redislabs/redisearch     Redis With the RedisSearch module pre-loaded…   56                   
redislabs/redisinsight   RedisInsight - The GUI for Redis                 88                   
redislabs/rebloom        A probablistic datatypes module for Redis        23                   [OK]
redislabs/redis          Clustered in-memory database engine compatib…   38                   
[root@node001 atguigu]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@node001 atguigu]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   <none>    9c7a54a9a43c   7 weeks ago     13.3kB
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
[root@node001 atguigu]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
[root@node001 atguigu]# docker pull redis:6.8.0
Error response from daemon: manifest for redis:6.8.0 not found: manifest unknown: manifest unknown
[root@node001 atguigu]# docker pull redis:6.0.8
6.0.8: Pulling from library/redis
bb79b6b2107f: Pull complete 
1ed3521a5dcb: Pull complete 
5999b99cee8f: Pull complete 
3f806f5245c9: Pull complete 
f8a4497572b2: Pull complete 
eafe3b6b8d06: Pull complete 
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Downloaded newer image for redis:6.0.8
docker.io/library/redis:6.0.8
[root@node001 atguigu]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   <none>    9c7a54a9a43c   7 weeks ago     13.3kB
ubuntu        latest    ba6acccedd29   20 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
redis         6.0.8     16ecd2772934   2 years ago     104MB
[root@node001 atguigu]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   <none>    9c7a54a9a43c   7 weeks ago     13.3kB
ubuntu        latest    ba6acccedd29   20 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
redis         6.0.8     16ecd2772934   2 years ago     104MB
[root@node001 atguigu]# docker images -q
9c7a54a9a43c
ba6acccedd29
feb5d9fea6a5
16ecd2772934
[root@node001 atguigu]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   45G   26G   17G   62% /
devtmpfs                 2.0G     0  2.0G    0% /dev
tmpfs                    2.0G     0  2.0G    0% /dev/shm
tmpfs                    2.0G   13M  2.0G    1% /run
tmpfs                    2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/sda1                976M  127M  782M   14% /boot
tmpfs                    394M   12K  394M    1% /run/user/42
tmpfs                    394M     0  394M    0% /run/user/1001
tmpfs                    394M     0  394M    0% /run/user/0
[root@node001 atguigu]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          4         1         177MB     177MB (99%)
Containers      2         0         0B        0B
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B
[root@node001 atguigu]# docker rmi 16ecd2772934
Untagged: redis:6.0.8
Untagged: redis@sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Deleted: sha256:16ecd277293476392b71021cdd585c40ad68f4a7488752eede95928735e39df4
Deleted: sha256:3746030fff867eb26a0338ad9d3ab832e6c19c7dc008090bcfa95c7b9f16f505
Deleted: sha256:1274ec54ad17d15ec95d2180cb1f791057e86dfcdfcc18cd58610a920e145945
Deleted: sha256:18d156147e54edec9a927080fdc0a53c4a8814b0c717b36dc62e637363c1a98d
Deleted: sha256:a8f09c4919857128b1466cc26381de0f9d39a94171534f63859a662d50c396ca
Deleted: sha256:2ae5fa95c0fce5ef33fbb87a7e2f49f2a56064566a37a83b97d3f668c10b43d6
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
[root@node001 atguigu]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   <none>    9c7a54a9a43c   7 weeks ago     13.3kB
ubuntu        latest    ba6acccedd29   20 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
[root@node001 atguigu]# docker pull redis:6.0.8
6.0.8: Pulling from library/redis
bb79b6b2107f: Pull complete 
1ed3521a5dcb: Pull complete 
5999b99cee8f: Pull complete 
3f806f5245c9: Pull complete 
f8a4497572b2: Pull complete 
eafe3b6b8d06: Pull complete 
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Downloaded newer image for redis:6.0.8
docker.io/library/redis:6.0.8
[root@node001 atguigu]# docker images -qa
9c7a54a9a43c
ba6acccedd29
feb5d9fea6a5
16ecd2772934
[root@node001 atguigu]# history 

面试题--虚悬镜像

定义 : 仓库名、标签都是<none>的镜像,俗称虚悬镜像

ubuntu容器说明

[root@node001 atguigu]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   <none>    9c7a54a9a43c   7 weeks ago     13.3kB
ubuntu        latest    ba6acccedd29   20 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
redis         6.0.8     16ecd2772934   2 years ago     104MB
[root@node001 atguigu]# 

容器命令

docker pull centos  拉取镜像


# 新建 + 启动
docker run [OPTIONS] IMAGE [COMMAND][ARG...]
    OPTIONS 说明(常用):
      --name="容器新名字"  :为容器指定一个名称
      -d : 后台运行容器,并返回容器ID,也即启动守护式容器
      -i : 以交互模式运行容器,通常与-t同时使用
      -t : 为容器重新分配一个伪输入终端,通常与-i同时使用
      -P(大写):随机端口映射
      -p: 指定端口映射,有以下四种格式
          ip:hostPort:containerPort
          ip::containerPort
          hostPort:containerPort
          containerPort
docker run -it centos /bin/bash  使用镜像 centos:latest 以交互模式启动一个容器,在容器内执行 /bin/bash 命令  

列出当前所有正在运行的容器

docker ps [OPTIONS]
    OPTIONS说明:
        -a: 列出当前所有正在运行的容器+历史上运行过的
        -I:显示最近创建的容器
        -n:显示最近n个创建的容器
        -q:静默模式,只显示容器编号
        --no-trunc:不截断输出
        
docker ps -n 2 显示最近创建的2个容器
        

[root@node001 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   <none>    9c7a54a9a43c   7 weeks ago     13.3kB
ubuntu        latest    ba6acccedd29   20 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
redis         6.0.8     16ecd2772934   2 years ago     104MB
[root@node001 ~]# docker run hello-world
 
Hello from Docker!
This message shows that your installation appears to be working correctly.
 
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
 
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
 
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
 
For more examples and ideas, visit:
 https://docs.docker.com/get-started/
 
[root@node001 ~]# docker run ubuntu
[root@node001 ~]# docker run -it ubuntu /bin/bash
root@a37594fd6c7e:/# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  1 11:40 pts/0    00:00:00 /bin/bash
root          9      1  0 11:40 pts/0    00:00:00 ps -ef
root@a37594fd6c7e:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@a37594fd6c7e:/# exit
exit
[root@node001 ~]# docker run -it ubuntu /bin/bash
root@8342de29884a:/# 
[root@node001 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
a37594fd6c7e   ubuntu    "/bin/bash"   2 minutes ago   Up 2 minutes             zealous_swirles
[root@node001 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@node001 ~]# docker run -it --name=myu1 ubuntu bash
root@6c0111214702:/# 
[root@node001 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@node001 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                       PORTS     NAMES
6c0111214702   ubuntu         "bash"        15 minutes ago   Exited (129) 7 minutes ago             myu1
8342de29884a   ubuntu         "/bin/bash"   15 minutes ago   Exited (0) 7 minutes ago               determined_keldysh
a37594fd6c7e   ubuntu         "/bin/bash"   24 minutes ago   Exited (0) 16 minutes ago              zealous_swirles
cccf6112cf2f   ubuntu         "bash"        24 minutes ago   Exited (0) 24 minutes ago              eager_grothendieck
b09066c13913   hello-world    "/hello"      40 minutes ago   Exited (0) 40 minutes ago              unruffled_sinoussi
3ff6498fa0a7   9c7a54a9a43c   "/hello"      4 weeks ago      Exited (0) 4 weeks ago                 vigorous_mcnulty
7b023b885d8c   9c7a54a9a43c   "/hello"      4 weeks ago      Exited (0) 4 weeks ago                 gallant_lehmann
[root@node001 ~]# docker ps -l
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                       PORTS     NAMES
6c0111214702   ubuntu    "bash"    15 minutes ago   Exited (129) 8 minutes ago             myu1
[root@node001 ~]# 

exit:容器停止退出
Ctrl + P + Q: 容器不停止退出

attach    Attach to a running container                 # 当前 shell 下 attach 连接指定运行镜像
build     Build an image from a Dockerfile              # 通过 Dockerfile 定制镜像
commit    Create a new image from a container changes   # 提交当前容器为新的镜像
cp        Copy files/folders from the containers filesystem to the host path   #从容器中拷贝指定文件或者目录到宿主机中
create    Create a new container                        # 创建一个新的容器,同 run,但不启动容器
diff      Inspect changes on a container's filesystem   # 查看 docker 容器变化
events    Get real time events from the server          # 从 docker 服务获取容器实时事件
exec      Run a command in an existing container        # 在已存在的容器上运行命令
export    Stream the contents of a container as a tar archive   # 导出容器的内容流作为一个 tar 归档文件[对应 import ]
history   Show the history of an image                  # 展示一个镜像形成历史
images    List images                                   # 列出系统当前镜像
import    Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]
info      Display system-wide information               # 显示系统相关信息
inspect   Return low-level information on a container   # 查看容器详细信息
kill      Kill a running container                      # kill 指定 docker 容器
load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]
login     Register or Login to the docker registry server    # 注册或者登陆一个 docker 源服务器
logout    Log out from a Docker registry server          # 从当前 Docker registry 退出
logs      Fetch the logs of a container                 # 输出当前容器日志信息
port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT    # 查看映射端口对应的容器内部源端口
pause     Pause all processes within a container        # 暂停容器
ps        List containers                               # 列出容器列表
pull      Pull an image or a repository from the docker registry server   # 从docker镜像源服务器拉取指定镜像或者库镜像
push      Push an image or a repository to the docker registry server    # 推送指定镜像或者库镜像至docker源服务器
restart   Restart a running container                   # 重启运行的容器
rm        Remove one or more containers                 # 移除一个或者多个容器
rmi       Remove one or more images       # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run       Run a command in a new container              # 创建一个新的容器并运行一个命令
save      Save an image to a tar archive                # 保存一个镜像为一个 tar 包[对应 load]
search    Search for an image on the Docker Hub         # 在 docker hub 中搜索镜像
start     Start a stopped containers                    # 启动容器
stop      Stop a running containers                     # 停止容器
tag       Tag an image into a repository                # 给源中镜像打标签
top       Lookup the running processes of a container   # 查看容器中运行的进程信息
unpause   Unpause a paused container                    # 取消暂停容器
version   Show the docker version information           # 查看 docker 版本号
wait      Block until a container stops, then print its exit code   # 截取容器停止时的退出状态值


4.Docker镜像

镜像分层概念

当容器启动时,一个新的可写层被加载到镜像的顶部。这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。

所有对容器的改动 - 无论添加、删除、还是修改文件都只会发生在容器层中。只有容器层是可写的,容器层下面的所有镜像层都是只读的。

[root@node001 ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
atguigu/ubuntu   3.7       7b40f602c178   13 hours ago    72.8MB
hello-world      <none>    9c7a54a9a43c   8 weeks ago     13.3kB
ubuntu           latest    ba6acccedd29   20 months ago   72.8MB
hello-world      latest    feb5d9fea6a5   21 months ago   13.3kB
redis            6.0.8     16ecd2772934   2 years ago     104MB
[root@node001 ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Pull complete 
9b829c73b52b: Pull complete 
cb5b7ae36172: Pull complete 
6494e4811622: Pull complete 
668f6fcc5fa5: Pull complete 
dc120c3e0290: Pull complete 
8f7c0eebb7b1: Pull complete 
77b694f83996: Pull complete 
0f611256ec3a: Pull complete 
4f25def12f23: Pull complete 
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@node001 ~]# docker run -it ubuntu /bin/bash
root@d5d7bf226e1e:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@d5d7bf226e1e:/# 

commit命令上集

root@d5d7bf226e1e:/# apt-get update
root@d5d7bf226e1e:/# apt-get -y install vim
root@d5d7bf226e1e:/# pwd
/
root@d5d7bf226e1e:/# vim a.txt
root@d5d7bf226e1e:/# cat a.txt
Hello, apex !
root@d5d7bf226e1e:/# 

commit命令下集

docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:[标签名]

docker commit -m="vim cmd add ok" -a="yh" d5d7bf222djd yh/myubuntu:1.3

[root@node001 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
d5d7bf226e1e   ubuntu    "/bin/bash"   21 minutes ago   Up 21 minutes             sharp_rosalind
[root@node001 ~]# docker commit -m="vim cmd add ok" -a="vlu" d5d7bf226e1e vlu/mybuntu:1.3
sha256:079bf1275a8a2e7aedd35132d2f4fafc407d799d1f9a3204ef97aed35da20a34
[root@node001 ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
vlu/mybuntu      1.3       079bf1275a8a   13 seconds ago   185MB
atguigu/ubuntu   3.7       7b40f602c178   13 hours ago     72.8MB
hello-world      <none>    9c7a54a9a43c   8 weeks ago      13.3kB
tomcat           latest    fb5657adc892   18 months ago    680MB
ubuntu           latest    ba6acccedd29   20 months ago    72.8MB
hello-world      latest    feb5d9fea6a5   21 months ago    13.3kB
redis            6.0.8     16ecd2772934   2 years ago      104MB
[root@node001 ~]# 
[root@node001 ~]# docker stop d5d7bf226e1e
d5d7bf226e1e
[root@node001 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@node001 ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
vlu/mybuntu      1.3       079bf1275a8a   3 minutes ago   185MB
atguigu/ubuntu   3.7       7b40f602c178   13 hours ago    72.8MB
hello-world      <none>    9c7a54a9a43c   8 weeks ago     13.3kB
tomcat           latest    fb5657adc892   18 months ago   680MB
ubuntu           latest    ba6acccedd29   20 months ago   72.8MB
hello-world      latest    feb5d9fea6a5   21 months ago   13.3kB
redis            6.0.8     16ecd2772934   2 years ago     104MB
[root@node001 ~]# docker run -it 079bf1275a8a /bin/bash
root@ddd323dc5d61:/# vim a.txt
root@ddd323dc5d61:/# cat a.txt 
Hello, apex !
root@ddd323dc5d61:/# 

Docker中的镜像分层,支持通过扩展现有镜像,创建新的镜像。类似Java继承于一个Base基础类,自己再按需扩展。

新镜像是从 base 镜像一层一层叠加生成的。每安装一个软件,就在现有镜像的基础上增加一层。

  • 26
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
尚硅谷周阳老师Docker教程的相关学习笔记包括以下内容:首先是Docker的简介,介绍了Docker的基本概念和特点。其次是构建Docker镜像的步骤,包括编写Dockerfile文件、使用docker build命令构建镜像、以及使用docker run命令运行镜像。最后是Dockerfile构建过程的基础知识。 具体来说,Dockerfile是用来定义Docker镜像的构建过程的文件,其中包含了一系列的指令,例如选择基础镜像、安装软件、配置环境等。通过使用docker build命令,我们可以根据Dockerfile构建出一个新的镜像。而使用docker run命令则可以运行该镜像。 以上是尚硅谷周阳老师Docker教程的相关学习笔记的简要内容。如果需要更详细的信息,建议查阅原教程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【学习笔记尚硅谷周阳老师Docker教程学习笔记](https://blog.csdn.net/zuzhiang/article/details/117339172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [docker笔记-基础篇](https://blog.csdn.net/xuanxxxxxx/article/details/108511352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值