docker容器引擎

Docker容器引擎

一:Docker的安装(以1809版本为例)

1:修改内核参数

[ dockerfile]# vim /etc/sysctl.conf

在末尾添加:

net.ipv4.ip_forward=1

2:安装方法1(不能上网)

1)先将Docker的仓库文件拷贝到Linux系统中

2)修改yum配置文件,在末尾添加

[ ~]# vim /etc/yum.repos.d/CentOS-Media.repo

[docker]

name=docker1809

baseurl=file:///opt/yum-docker1809

gpgcheck=0

[ ~]# yum clean all

3)安装

[ ~]# yum -y install docker-ce

4)启动docker服务

[ ~]# systemctl start docker

[ ~]# systemctl enable docker

5)查看docker版本

[ ~]# docker version

Client:

 Version:           18.09.3

 API version:       1.39

 Go version:        go1.10.8

 Git commit:        774a1f4

 Built:             Thu Feb 28 06:33:21 2019

 OS/Arch:           linux/amd64

 Experimental:      false

Server: Docker Engine - Community

 Engine:

  Version:          18.09.3

  API version:      1.39 (minimum version 1.12)

  Go version:       go1.10.8

  Git commit:       774a1f4

  Built:            Thu Feb 28 06:02:24 2019

  OS/Arch:          linux/amd64

  Experimental:     false

方法二:能上网

docker版本为1809

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/CentOS-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

# yum install -y yum-utils device-mapper-persistent-data lvm2

# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

注意:上述两个语句是为了在linux中下载dockerrpm仓库文件,如果已经下载的有,这两部步可以不要

[ ~]# yum install -y docker-ce-18.09.7

注意:如果要安装最新版可以:yum install -y docker-ce

[ ~]# systemctl enable docker

[ ~]# systemctl start docker

二:Docker镜像操作

1:搜索镜像

[ ~]# docker search dhcp

[ ~]# docker search apache

[ ~]# docker search httpd

2:获取镜像

[ ~]# docker pull httpd

Using default tag: latest

latest: Pulling from library/httpd

743f2d6c1f65: Pull complete

c92eb69846a6: Pull complete

2211b052800a: Pull complete

aed180197314: Pull complete

7c472a4980a7: Pull complete

Digest: sha256:680657b49788fc643cda8cbdcf0564474cb78c0d5ed53a3ea9cb3d56f9aacfc8

Status: Downloaded newer image for httpd:latest

注意:如果使用docker官方的docker仓库,下载会很慢,可以使用国内的镜像

对于大于1.9docker版本可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

此文件原本不存在,需要创建

[ ~]# vim /etc/docker/daemon.json

添加如下代码:

{

"registry-mirrors": ["https://v10sz3o0.mirror.aliyuncs.com"],

"insecure-registries": ["192.168.1.149:8000"]

}

[ ~]# systemctl daemon-reload

[ ~]# systemctl restart docker

注释:

https://v10sz3o0.mirror.aliyuncs.com为申请的阿里专属加速器

如果没有专属加速器,可以使用其他国内仓库:https://registry.docker-cn.com

没有此文件表示使用默认的docker官方仓库

Docker镜像加速地址可以在阿里上申请

https://cr.console.aliyun.com/cn-hangzhou/mirrors

3:查看镜像信息

1)查看本地所有镜像

[ ~]# docker images

REPOSITORY    TAG                 IMAGE ID            CREATED             SIZE

httpd          latest              b7cc370ac278        2 days ago          132MB

2)获取镜像详细信息

[ ~]# docker inspect httpd

3)为镜像重命名并设置新的标签

[ ~]# docker tag httpd http:myhttpd

[ ~]# docker images

REPOSITORY   TAG                 IMAGE ID            CREATED             SIZE

http          myhttpd             b7cc370ac278        2 days ago          132MB

httpd         latest                b7cc370ac278        2 days ago          132MB

4)删除镜像

[ ~]# docker rmi httpd

Untagged: httpd:latest

Untagged: :680657b49788fc643cda8cbdcf0564474cb78c0d5ed53a3ea9cb3d56f9aacfc8

[ ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

http                myhttpd             b7cc370ac278        2 days ago          132MB

5)导出镜像

[ ~]# docker save -o /root/http http

6)导入镜像(要先把原来的http镜像删掉)

[ ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

http                myhttpd             b7cc370ac278        2 days ago          132MB

[ ~]# docker rmi b7

[ ~]# docker load --input /root/http

三:Docker容器操作

1:容器的创建与启动

1)将镜像文件拷贝到系统

2)导入镜像

[ ~]# docker load --input centos-7

[ ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

http                myhttpd             b7cc370ac278        2 days ago          132MB

httpd               latest              b7cc370ac278        2 days ago          132MB

centos              latest              d123f4e55e12        18 months ago       197MB

3)创建容器

[ ~]# docker create -it centos  /bin/bash

[ ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

a20112bccf57        centos              "/bin/bash"         2 seconds ago       Created                                 nifty_lumiere

c713336886d5        d1                  "/bin/bash"         3 minutes ago       Created                                 hopeful_keldysh

cd65cd6d83b9        httpd               "/bin/bash"         6 minutes ago       Created                                 stoic_yonath

4)启动容器

[ ~]# docker start a2011

a2011

[ ~]# docker ps -a | grep a2011

a20112bccf57        centos              "/bin/bash"         7 minutes ago       Up 15 seconds                           nifty_lumiere

5)创建并启动一次容器

注意:此方法启动完容器后,容器就会停止

[ ~]# docker run centos /bin/bash -c ls

6)永久启动一个容器

[ ~]# docker run -d centos /bin/bash -c "while true;do echo ok;done;"

注释:

-c:在容器中执行指令

-d:以守护进程运行

7)停止容器

[ ~]# docker stop a20112bccf57

8)进入容器

[ ~]# docker start a2011

[ ~]# docker exec -it a2011 /bin/bash

[ /]# ls

anaconda-post.log  dev  home  lib64       media  opt   root  sbin  sys  usr

bin                etc  lib   lost+found  mnt    proc  run   srv   tmp  var

[ /]# exit

9)导出容器

[ ~]# docker export a20>/root/mycentos

10)导入容器

注意:导入的容器在系统中会成为一个镜像

[ ~]# docker import mycentos centos123

[ ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

centos123           latest              b6d1b0ebddca        11 seconds ago      197MB

http                myhttpd             b7cc370ac278        2 days ago          132MB

httpd               latest              b7cc370ac278        2 days ago          132MB

ubuntu              latest              d131e0fa2585        13 days ago         102MB

centos              latest              d123f4e55e12        18 months ago       197MB

11)删除容器

[ ~]# docker stop a20

a20

[ ~]# docker rm a20

a20

[ ~]# docker ps -a

四:Docker资源控制

1:对cpu的控制

1)限制cpu使用率

[ ~]# echo 2000 >/sys/fs/cgroup/cpu/system.slice/docker.service/cpu.cfs_quota_us

注释:使用率为20%

2)多任务按比例分配cpu

[ ~]# docker run -tid --cpu-shares 1024 centos

[ ~]# docker run -tid --cpu-shares 1024 ubuntu

3)为容器分配内核

[ ~]# docker run -tid --cpuset-cpus 0 centos

注释:为centos分配0号内核,注意,不存在的内核不能调用

2:对内存使用的限制

(1) 分配内存上限

[ ~]# docker run -tid -m 512m 192.168.1.149:8000/centos

2)对内存blkio(每秒写入次数)的限制

[ ~]# docker run -tid --device-write-bps /dev/sda:1MB centos

相关推荐