Docker(一)入门

Docker入门

容器的定义

  • 运行在用户空间的独立进程
  • 与其他的用户空间程序是相互隔离
  • 一个容器运行在一个单独的用户空间
  • 底层是单独的内核空间

发展历史

  • FreeBSD jail, 2000
  • Linux VServer ,2001
    • CGroups
    • NameSpace
    • LXC
  • Docker ,2010
    • libcontainer
    • runC

docker容器编排三剑客

  • docker-mechine
  • docker-swarm
  • docker-compose

Linux NameSpace

  • 用于在一个抽象层上封装一个全局级别的可以进行切分的系统资源,主要如下的七个个级别的系统资源
NameSpaces Constant Isolates
Cgroup CLONE_NEWCGROUP Cgroup root directory,调派底层的cpu,内存,IO资源给容器
IPC CLONE_NEIPC System V IPC,POSIX message queues
NETWORK COLNE_NENET NetWork devices,stacks,ports,etc
Mount ClONE_NEWNS MountPoints
PID COLNE_NEW_PID Process PID
Users COLNE_USER User and Group IDs
UTS CLONE_NEW_UTS Hostname and NIS domain name

docker的版本

  • docker-ee
  • docker-ce

Docker的架构

image.png

docker的安装

  • 系统: centos7+

  • 初始化:配置主机名&ip略过

[root@centos7-node1 ~]# systemctl stop firewalld && systemctl disable firewalld
[root@centos7-node1 ~]# sed -i "s/SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config && reboot -f
[root@centos7-node1 ~]# yum install chrony wget curl git -y && systemctl enable chronyd && systemctl start chronyd && timedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes    #时间同步
[root@centos7-node1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos7-node1 ~]# yum -y install epel-release
[root@centos7-node1 ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf  &&  sysctl -p

  • docker的安装和配置
# step 1: 安装必要的一些系统工具
 yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装Docker-CE
yum makecache fast
yum -y install docker-ce
# 新增配置文件
 mkdir -p /etc/docker
 tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://0b8hhs68.mirror.aliyuncs.com"],
  "storage-driver": "overlay2",
  "graph":"/data/docker",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF
# 重载服务
sudo systemctl daemon-reload
sudo systemctl restart docker

docker 环境相关命令

docker info     #查看docker 信息
docker version   #查看版本信息

镜像相关命令

[root@centos7-node1 ~]# docker image -h
Flag shorthand -h has been deprecated, please use --help
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 Pull an image or a repository from a registry
  push Push an image or a repository 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

容器相关命令

[root@centos7-node1 ~]# docker container -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker container COMMAND
Manage containers
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
  exec Run a command in a running container
  export Export a container's filesystem as a tar archive
  inspect Display detailed information on one or more containers
  kill Kill one or more running containers
  logs Fetch the logs of a container
  ls List containers
  pause Pause all processes within one or more containers
  port List port mappings or a specific mapping for the container
  prune remove all stopped containers
  rename Rename a container
  restart Restart one or more containers
  rm Remove one or more containers
  run Run a command in a new container
  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
  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
image.png

实践

镜像实践

[root@centos7-node1 ~]# docker search redis    #查找redis镜像
[root@centos7-node1 ~]# docker pull redis:4-alpine    #拖镜像
[root@centos7-node1 ~]# docker images    #查看所有镜像
[root@centos7-node1 ~]# docker inspect redis:4-alpine     #查看镜像详情
[root@centos7-node1 ~]# docker rmi redis:4-alpine           #删除镜像
[root@centos7-node1 ~]# docker image save redis:4-alpine -o redis.tar     #导出镜像
[root@centos7-node1 ~]# docker image load -i redis.tar         #导入镜像

登陆自己的阿里云容器镜像控制台,创建命名空间,然后在创建一个redis的本地镜像仓库

image.png

接下来就是比较常用的自己上传镜像到阿里云redis镜像仓库

[root@centos7-node1 ~]# docker login --username=valiente0822 registry.cn-hangzhou.aliyuncs.com   #登陆我的阿里云docker镜像仓库
[root@centos7-node1 ~]# docker image tag redis:4-alpine registry.cn-hangzhou.aliyuncs.com/myimgs/redis:4-alpine   #拷贝并修改tag
[root@centos7-node1 ~]# docker push registry.cn-hangzhou.aliyuncs.com/myimgs/redis:4-alpine    #上传镜像
[root@centos7-node1 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/myimgs/redis:4-alpine      #下载镜像

注意事项:

  • 创建的如果是阿里云私有仓库,pull镜像的时候需要docker login之后才能pull

容器实践

[root@centos7-node1 ~]# docker image pull centos:7   #拉取镜像
[root@centos7-node1 ~]# docker container run -it --name c1 centos:7 /bin/bash    #交互式运行容器
[root@centos7-node1 ~]# iptables -t nat -vnL    #新开终端,发现有nat iptables规则
[root@centos7-node1 ~]# docker ps -a             #查看运行或者停止状态的容器
[root@centos7-node1 ~]# docker start c1          #启动容器
[root@centos7-node1 ~]# docker ps                  #查看运行状态的容器
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354