Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Docker速学(一) 镜像和容器

Docker速学(一) 镜像和容器

原创
作者头像
w9
修改于 2021-08-25 06:14:37
修改于 2021-08-25 06:14:37
69000
代码可运行
举报
运行总次数:0
代码可运行

了解Docker基本原理和要掌握的内容之后,我们就正式开始Docker的学习了。本篇内容主要介绍Docker的镜像和容器

Docker安装:可视化Docker快速安装部署教程

上篇回顾:Docker小白入门建议及基本原理介绍。下一篇预告:环境变量、数据卷等。

下面,一起来开始学习吧!

镜像

原理

如果只考虑 Docker 容器的操作系统属性,那么镜像=轻量级操作系统安装包。

如果需考虑 Docker 容器的应用软件属性,那么镜像=(轻量级操作系统+应用)安装包

例如:MySQL 镜像= 虚拟的 Linux 操作系统 + MySQL

镜像是怎么产生的?

用户编写镜像编排 Dockerfile,对这种文件进行 build 操作,就生成了一个镜像。

镜像是一个文件?

镜像从逻辑上可以简单理解是一个文件,但实际上是多层文件的组合。

所以,镜像虽然不是一个单独的文件,但可以被导出成为一个压缩文件:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 镜像导出成一个tarball文件
docker save image
​
# 加载一个 tarball 镜像文件
docker load image
镜像存放在哪里?

运行容器时,Docker 会从 /var/lib/docker/image 目录下寻找是否镜像文件。

如果没有镜像文件,Docker 会尝试从 Dockerhub 镜像仓库 (opens new window)中下载到本地,然后运行。

仓库

众所周知,DockerHub 是由 Docker 官方运营的全球最大的镜像仓库

实际上,除了 DockerHub 之外,还有多种构建仓库的方式:

自建仓库

支持自建仓库。一般云提供商均提供了镜像仓库服务,供客户存放自己的私有镜像。

加速仓库

如果从 Dockerhub 下载镜像镜像非常慢的话,就需要通过如下的方式修改仓库地址:

  1. 选择或获取你喜欢的国内镜像仓库(加速地址) #1 Docker 中文社区 https://registry.docker-cn.com ​ #2 网易仓库 http://hub-mirror.c.163.com ​ #3 腾讯仓库 https://mirror.ccs.tencentyun.com ​ #4 阿里云仓库 https://f53jxx8r.mirror.aliyuncs.com 上述阿里云仓库加速地址仅供参考,建议登录控制台后,从后台获取 (opens new window)获取
  2. 修改 /etc/docker/daemon.json 文件(如果没有可以增加),插入下值 { "registry-mirrors": ["https://f53jxx8r.mirror.aliyuncs.com"] }
  3. 重启服务后生效 sudo systemctl daemon-reload sudo systemctl restart docker
  4. Docker 支持配置多个仓库地址,类似: { "registry-mirrors": ["https://registry.docker-cn.com","https://f53jxx8r.mirror.aliyuncs.com","https://docker.mirrors.ustc.edu.cn","http://hub-mirror.c.163.com"] }

容器

容器是 Docker 最重要的组件,上面已经多次提到容器就是一个轻量级虚拟机

运行容器

通过 docker run 命令运行容器,它的用法和参数如下(详情 (opens new window))。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
​
Run a command in a new containerOptions:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup namespace
                                       'private': Run the container in its own private cgroup namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
  -d, --detach                         Run container in background and print container ID
      --detach-keys string             Override the key sequence for detaching a container
      --device list                    Add a host device to the container
      --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])
      --device-read-iops list          Limit read rate (IO per second) from a device (default [])
      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])
      --device-write-iops list         Limit write rate (IO per second) to a device (default [])
      --disable-content-trust          Skip image verification (default true)
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains
      --domainname string              Container NIS domain name
      --entrypoint string              Overwrite the default ENTRYPOINT of the image
  -e, --env list                       Set environment variables
      --env-file list                  Read in a file of environment variables
      --expose list                    Expose a port or a range of ports
      --gpus gpu-request               GPU devices to add to the container ('all' to pass all GPUs)
      --group-add list                 Add additional groups to join
      --health-cmd string              Command to run to check health
      --health-interval duration       Time between running the check (ms|s|m|h) (default 0s)
      --health-retries int             Consecutive failures needed to report unhealthy
      --health-start-period duration   Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
      --health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)
      --help                           Print usage
  -h, --hostname string                Container host name
      --init                           Run an init inside the container that forwards signals and reaps processes
  -i, --interactive                    Keep STDIN open even if not attached
      --ip string                      IPv4 address (e.g., 172.30.100.104)
      --ip6 string                     IPv6 address (e.g., 2001:db8::33)
      --ipc string                     IPC mode to use
      --isolation string               Container isolation technology
      --kernel-memory bytes            Kernel memory limit
  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --platform string                Set platform if server is multi-platform capable
      --privileged                     Give extended privileges to this container
  -p, --publish list                   Publish a container's port(s) to the host
  -P, --publish-all                    Publish all exposed ports to random ports
      --pull string                    Pull image before running ("always"|"missing"|"never") (default "missing")
      --read-only                      Mount the container's root filesystem as read only
      --restart string                 Restart policy to apply when a container exits (default "no")
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --sig-proxy                      Proxy received signals to the process (default true)
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -t, --tty                            Allocate a pseudo-TTY
      --ulimit ulimit                  Ulimit options (default [])
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container

下面我们通过一个简单的示例,介绍如何运行一个容器:

  1. 找到一个 Docker 镜像,例如:MySQL(opens new window)
  2. 运行如下的命令启动一个 MySQL 容器 docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:tag
  3. 容器运行成功后,运行如下命令即可开始使用 MySQL 的客户端命令 docker exec -it mysql mysql -uroot -p123456

上述示例我们完成如下几个工作:

  • 通过镜像页面找到运行容器的方案
  • 运行一个容器
  • 进入一个容器

创建镜像

Docker 的原理表明,容器的内核有一部分共享的 Docker 镜像的不变文件,另外一部分是可变文件。

所以,容器也可以很方便的转换成镜像。具体操作如下:

  1. 运行 docker ps 命令获取容器的 ID 号
  2. 将容器导出为压缩文件 # 容器导出成 tarball 文件 docker export -o mysql-`date +%Y%m%d`.tar f9fc8627b7fe ​ # 查看文件 ls mysql-`date +%Y%m%d`.tar
  3. 将压缩文件转换成镜像 docker import mysql-20210416.tar mysql-test
  4. 运行 docker image ls 命令,查看刚转换成功的镜像 $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mysql-test latest 05cb947f5572 5 seconds ago 209MB

从功能上讲,docker export相当于commit +save,先将容器commit成镜像,再save成文件。

下篇内容:境变量、数据卷等。期待您和小九一起继续学习~

这个可视化Docker运行环境值得一试:https://market.cloud.tencent.com/products/24508

云市场开源软件中心,从Wordpress、宝塔、SqlServer数据库到Gitlab、AWX等,一键部署,欢迎试用:https://market.cloud.tencent.com/stores/1252192180。本地安装部署方案:https://github.com/websoft9

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Docker容器的使用
执行命令docker run即可启动容器,也就是创建某个镜像的实例。docker run命令非常复杂,可以先执行一个docker run --help来查看帮助:
人不走空
2024/02/21
1900
Docker容器的使用
docker基本操作
容器中可以运行网络应用,但是要让外部也可以访问这些应用,可以通过-p或-P参数指定端口映射。
CoderJia
2024/10/18
1500
【Docker】Docker常见命令汇总
命令帮助文档:Reference documentation | Docker Documentation
宝耶需努力
2022/12/13
1.1K0
docker-05
前几章节的回顾: docker-01 docker-02 docker-03 docker-04 今天我们介绍关于docker容器的命令,没错就是container 一.docker容器命令 要跑通容器首先我们需要有基础镜像 所以我们先拉取一个Ubuntu系统 服务器系统为centos,所以我这里拉取Ubuntu系统 指令: docker pull ubuntu 结果: Using default tag: latest latest: Pulling from library/ubuntu 08
Tom2Code
2022/04/15
1970
docker-05
Docker selenium自动化 - Python调用容器实例跑自动化查天气实例演示,docker selenium自动化环境部署过程
[系列文章篇] 篇章一:Docker selenium 自动化 - windows 版 docker 的安装与运行环境检测 [问题处理篇] 篇章一:Docker selenium 自动化 - 修改 /dev/shm 路径大小实例演示,“session deleted because of page crash” 问题解决
小蓝枣
2021/12/01
4830
Docker selenium自动化 - Python调用容器实例跑自动化查天气实例演示,docker selenium自动化环境部署过程
Docker镜像创建
创建镜像有两种方式: 1、从已经创建的容器中更新镜像,并且提交这个镜像 2、使用 Dockerfile 指令来创建一个新的镜像
李小白是一只喵
2020/04/23
1.3K0
Docker镜像创建
​Docker:从入门到实战过程全记录
想要真正理解Docker,就不得不从虚拟化技术的发展历程说起。普遍认为虚拟化技术经历了物理机时代、虚拟机时代,目前已经进入到了容器化时代。可以说,Docker是虚拟化技术不断发展的必然结果。
用户8949263
2022/11/07
7800
浅析Docker运行安全
AppArmor 主要的作用是设置某个可执行程序的访问控制权限,可以限制程序 读/写某个目录/文件,打开/读/写网络端口等等。
FB客服
2020/04/18
3K0
【知识分享】docker run命令详解
(1)Docker run 命令参数及使用 https://blog.csdn.net/luolianxi/article/details/107169954 (2)Docker run 命令[简洁清爽版] https://www.runoob.com/docker/docker-run-command.html (3)docker run官网说明 https://docs.docker.com/engine/reference/commandline/run/ (4)【知识分享】Docker 命令大全 https://www.jianshu.com/p/7fa8e918768e
辉哥
2021/04/01
1.1K0
【知识分享】docker run命令详解
docker安装与启动
通过docker run来启动镜像,同时会创建一个容器,看下docker run的启动命令:
全栈程序员站长
2022/08/05
5030
性能环境之docker操作指南3(全网最全)
如果在执行run命令时没有指定-a,那么docker默认会挂载所有标准数据流,包括输入输出和错误。你可以特别指定挂载哪个标准流。
高楼Zee
2019/07/17
1.6K0
性能环境之docker操作指南2(全网最全)
通过容器创建本地镜像. 可自定义tag,如docker commit dc806ee0bf86 web:v1 不指定tag的情况下默认tag为latest
高楼Zee
2019/07/17
6370
Docker
与其他介绍Docker的文章不同,由本文开启的系列文章将专注于Docker安全研究,一共分为6部分。
扬起
2022/06/24
1.2K0
Docker
Docker-client for python使用指南
Docker-client for python使用指南: 客户端初始化的三种方法 import docker docker.api() docker.APIClient() docker.client() docker.DockerClient() 其实也是docker.client()的一个子集 docker.from_env() 其实就是docker.client()的一个子集 一、初始化客户端 1.Docker客户端的初始化工作 >>> import docker >>> client = dock
BGBiao
2018/02/26
6.1K0
Docker容器学习梳理--日常操作总结
使用Docker已有一段时间了,今天正好有空梳理下自己平时操作Docker时的一些命令和注意细节: Docker 命令帮助 $ sudo docker Commands: attach Attach to a running container --将终端依附到容器上 1> 运行一个交互型容器 [root@localhost ~]# docker run -i -t centos /bin/bash
洗尽了浮华
2018/01/22
2.3K0
Docker Review - dockerfile 实战_给基础镜像增加功能
Dockerfile 分为四部分:基础镜像信息、维护者信息、镜像操作指令、容器启动执行指令。
小小工匠
2022/11/30
6360
Docker Review - dockerfile 实战_给基础镜像增加功能
n1.Docker命令参数一览表
描述:利用 docker info 命令 Docker Client && Docker Server 信息一览:
全栈工程师修炼指南
2020/10/23
2.3K0
n1.Docker命令参数一览表
Docker入门
每个虚拟化应用程序不仅包括应用程序(可能只有数十MB)以及必要的二进制文件和库,还包括整个客户机操作系统(可能重数十GB)。
星宇大前端
2020/07/07
6320
Docker入门
Docker安全入门与实战(四)
众所周知,Docker使用namespace进行环境隔离、使用cgroup进行资源限制。但是在实际应用中,还是有很多企业或者组织没有使用namespace或者cgroup对容器加以限制,从而埋下安全隐患。本文定位于简单介绍namespace和cgroup的基本原理之后,通过具体配置和应用向读者展示如何应用这些技术保护docker容器安全,不过namespace和cgroup并不是万能的,他们只是保障Docker容器安全的多种方案中的一类而已。
0xtuhao
2022/06/21
3720
5.Docker底层实现原理了解
通过前面的学习,我们基本掌握了Docker的配置使用,现在我们以 Docker 基础架构来探究Docke底层的核心技术,简单的包括:
全栈工程师修炼指南
2022/09/28
1.3K0
5.Docker底层实现原理了解
相关推荐
Docker容器的使用
更多 >
LV.3
公众号:猫头虎技术团队职业: 全栈软件工程师
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验