yaomint 发表于 2019-2-21 08:01:04

CentOS 7 上搭建 Docker 和基础管理

Docker 简介

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

Docker 容器与传统虚拟机的比较




特性
Docker
虚拟机




启动速度
秒级
分钟级


计算能力损耗
几乎无
损耗 50% 左右


性能
接近原生
弱于


系统支持量
上千个
几十个


隔离性
资源限制
完全隔离



搭建 Docker

1.配置 yum 仓库源 :

vim /etc/yum.repos.d/docker.repo

name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
2.安装 Docker :

yum install docker-engine -y
systemctl stop firewalld.service
setenforce 0
systemctl start docker.service    #开启服务
systemctl enable docker.service   #开机自启
3.查看 docker 版本 :

# docker version
Client:
Version:      17.05.0-ce
API version:1.29
Go version:   go1.7.5
Git commit:   89658be
Built:      Thu May4 22:06:25 2017
OS/Arch:      linux/amd64
Server:
Version:      17.05.0-ce
API version:1.29 (minimum version 1.12)
Go version:   go1.7.5
Git commit:   89658be
Built:      Thu May4 22:06:25 2017
OS/Arch:      linux/amd64
Experimental: false
Docker 镜像操作

1.搜索镜像 :


命令格式:docker search 关键字


# docker search httpd
NAME                                    DESCRIPTION                                     STARS   OFFICIAL   AUTOMATED
httpd                                 The Apache HTTP Server Project                  1891            
hypriot/rpi-busybox-httpd               Raspberry Pi compatible Docker Image with ...   41                  
centos/httpd                                                                            19                  
centos/httpd-24-centos7               Platform for running Apache httpd 2.4 or b...   15                  
armhf/httpd                           The Apache HTTP Server Project                  8            
......
- NAME:镜像名称
- DESCRIPTION:描述信息
- STARS:星级。表示该镜像受欢迎程度
- OFFICIAL:是否官方创建
- AUTOMATED:是否主动创建
2.下载镜像 :


命令格式:docker pull 仓库名称[:标签]


# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
d660b1f15b9b: Pull complete
aa1c79a2fa37: Pull complete
f5f6514c0aff: Pull complete
676d3dd26040: Pull complete
4fdddf845a1b: Pull complete
520c4b04fe88: Pull complete
5387b1b7893c: Pull complete
Digest: sha256:8c84e065bdf72b4909bd55a348d5e91fe265e08d6b28ed9104bfdcac9206dcc8
Status: Downloaded newer image for httpd:latest
3.查看镜像信息 :


命令格式:docker images 仓库名称[:标签]


# docker images
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
httpd               latest            11426a19f1a2      13 days ago         178MB
- REPOSITORY:镜像属于仓库
- TAG:标签信息
- IMAGE ID:镜像的唯一ID
- CREATED:镜像创建时间
- SIZE:大小
4.查看镜像详细信息 :


命令格式:docker inspect 镜像ID


# docker inspect d7e3778f14cb
[
{
"Id": "sha256:d7e3778f14cbbf17426e112e15b72eab02d2742ceabddfec3f6ffe02b761ad3e",
"RepoTags": [
"tomcat:centos"
.......
5.添加镜像标签 :


命令格式:docker tag 名称:[标签] 新名称:[新标签]


# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
centos            latest            5182e96772bf      8 days ago          200MB
# docker tag centos test:centos   #添加标签
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
centos            latest            5182e96772bf      8 days ago          200MB
test                centos            5182e96772bf      8 days ago          200MB
6.删除镜像 :


命令格式:docker rmi 仓库名称:标签   或者   docker rmi 镜像ID


# docker rmi test:centos
Untagged: test:centos

7.存出镜像和载入镜像 :


命令格式:docker save -o 存储文件名 存储镜像


# docker save -o centos centos   #默认存放在当前目录
# ls -l centos
-rw-------. 1 root root 208301056 8月15 14:02 centos

命令格式:docker load < 存出的文件   或者   docker --input 存出的文件


# docker load < centos
Docker 容器操作

1.容器的创建 :


命令格式:docker create [选项] 镜像 运行程序


# docker create -it centos /bin/bash
#-i:让容器输入保持打开-t:让 Docker 分配一个为终端
4bab9c0c5344d21ef96ad9e476958a061c06f01ea8f09f32dae14aaba3ddfd09
2.运行和停止容器 :


命令格式:docker start 容器ID/名称


# docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
4bab9c0c5344      centos            "/bin/bash"         15 seconds ago      Created                                 vibrant_stonebraker
# docker start 4bab9c0c5344   
4bab9c0c5344
# docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
4bab9c0c5344      centos            "/bin/bash"         40 seconds ago      Up 8 seconds                            vibrant_stonebraker

命令格式:docker stop 容器ID/名称


# docker ps -a
CONTAINER ID      IMAGE               COMMAND                  CREATED            STATUS            PORTS               NAMES
8ed40d081f44      centos            "/usr/bin/bash -c ..."   About a minute ago   Up About a minute                     elated_wozniak
4bab9c0c5344      centos            "/bin/bash"            13 minutes ago       Up 12 minutes                           vibrant_stonebraker
# docker stop 8ed40d081f44
8ed40d081f44
# docker ps -a
CONTAINER ID      IMAGE               COMMAND                  CREATED            STATUS                     PORTS               NAMES
8ed40d081f44      centos            "/usr/bin/bash -c ..."   About a minute ago   Exited (137) 2 seconds ago                     elated_wozniak
4bab9c0c5344      centos            "/bin/bash"            13 minutes ago       Up 13 minutes                                    vibrant_stonebraker

docker run centos /usr/bin/bash #使用run命令 镜像不存在会自动下载,自动创建容器,自动开启容器


3.持续运行容器 :

# docker run -d centos /usr/bin/bash -c "while true;do echo hello;done"
#-d 守护进程   while true;do echo hello;done 循环语句就可以保持容器一值在后台运行
4.容器的进入 :


命令格式:docker exec -it 容器ID/名称 /bin/bash


# docker start 4bab9c0c5344   #开启容器
4bab9c0c5344
# docker exec -it 4bab9c0c5344 /bin/bash
# ls
anaconda-post.logdevhomelib64mntprocrun   srvtmpvar
bin                etclib   mediaoptrootsbinsysusr
5.容器的导入导出 :


命令格式:docker export 容器ID/名称 > 文件名


# docker export 4bab9c0c5344 > test   #当前目录

命令格式:cat 文件名 | docker import - 生成的镜像名称:标签


cat tset | docker import - test01:test01


页: [1]
查看完整版本: CentOS 7 上搭建 Docker 和基础管理