==官网:

https://www/docker.com

docker [码头工人]


==Docker 概述

Docker 是一个应用程序开发、部署、运行的平台,使用 go 语言开发。

相较于传统的主机虚拟化,Docker 提供了轻量级的应用隔离方案,并且为我们提供了应用程序快速扩容、缩容的能力。

docker 服务相当于鲸鱼,container容器就是集装箱

==实验准备

部署docker容器虚拟化平台

==卸载旧版本

yum remove docker \

docker-client \

docker-client-latest \

docker-common \

docker-latest \

docker-latest-logrotate \

docker-logrotate \

docker-engine

==安装docker环境依赖

yum install -y yum-utils \

device-mapper-persistent-data \

lvm2

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

==安装源

y yum-config-manager \

--add-repo \

https://download.docker.com/linux/centos/docker-ce.repo

==docker安装

yum install docker-ce docker-ce-cli containerd.io -y

==启动

systemctl start docker && systemctl enable docker

==搜索 镜像

docker search centos

NAME DESCRIPTION STARS OFFICIAL AUTOMATED

centos The official build of CentOS. 6634 [OK]

ansible/centos7-ansible Ansible on Centos7 134 [OK]

consol/centos-xfce-vnc Centos container with "headless" VNC session… 129 [OK]

jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 118 [OK]

centos/systemd systemd enabled base container. 100 [OK]

centos/mysql-57-centos7 MySQL 5.7 SQL database server 88

imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]

tutum/centos Simple CentOS docker image with SSH access 48

centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 40

kinogmt/centos-ssh CentOS with SSH 29 [OK]

guyton/centos6 From official centos6 container with full up… 10 [OK]

nathonfowlie/centos-jre Latest CentOS image with the JRE pre-install… 8 [OK]

centos/tools Docker image that has systems administration… 7 [OK]

drecom/centos-ruby centos ruby 6 [OK]

mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]

darksheer/centos Base Centos Image -- Updated hourly 3 [OK]

indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 2 [OK]

dokken/centos-7 CentOS 7 image for kitchen-dokken 2

ovirtguestagent/centos7-atomic The oVirt Guest Agent for Centos 7 Atomic Ho… 2

amd64/centos The official build of CentOS. 2

miko2u/centos6 CentOS6 日本語環境 2 [OK]

blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]

mcnaughton/centos-base centos base image 1 [OK]

smartentry/centos centos with smartentry 0 [OK]

starlabio/centos-native-build Our CentOS image for native builds 0 [OK]

==配置镜像加速器

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

针对Docker客户端版本大于 1.10.0 的用户

您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'

{

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

}

EOF

sudo systemctl daemon-reload && sudo systemctl restart docker

==拉取镜像

docker pull centos

==开启网络转发功能:

默认开启

[root@bogon ~]# cat /proc/sys/net/ipv4/ip_forward

1

==查看:

docker images


==docker 运行:

[root@bogon ~]# docker run -it centos bash

[root@e148efa38210 /]#

-i 以交互式运行容器

-t 为容器重新分配一个伪输入终端

-d 后台运行

-c 后面跟待完成命令

==实例:

docker run -d centos:latest /bin/bash -c "while true; do echo hello world; sleep 1; done"

19db9652bfe4643b5ba36097d32db0f2e2139e0ed705b7703a1a36bb55c1cf20

一个死循环一直往docker里面写hello word

while true;

do

echo hello world;

sleep 1;

done

==查看:从容器中取日志,查看输出的内容,可用于后期检查docker实例

输出中弹出的错误

语法:docker logs 容器name id

docker logs 19db9652bfe4643b5ba36097d32db0f2e2139e0ed705b7703a1a36bb55c1cf20


docker ps #列出所有运行的容器

docker ps -a #累出所有容器(包括沉睡/退出状态的容器)


docker kill + 容器id # 杀死进程



映射端口:

docker run -d -p 80:80 centos:latest

==docker 容器命名和重命名

容器命名语法:docker run -d --name 容器实例名 容器镜像名 要执行的命令

docker run -itd --name docker1 centos:latest bash

容器重命名语法:docker rename 旧容器名 新容器名

例:

==运行一个名字为docker1的容器

docker run -itd --name docker1 centos:latest bash

==例2:

docker rename docker1 docker2

==创建docker容器实例时指定主机名

docker run -it --name 容器名 -h 指定主机名 镜像 /bin/bash


[root@kill ~]# docker run -it --name docker3 -h kill.com centos bash

[root@kill /]#

==让docker容器开机自动启动

语法:docker run --restart=always -itd --name 容器名 镜像 /bin/bash

参数:--restart=always #在容器退出时候总是重启

docker run --restart=always -itd --name docker4 centos bash

测试 systemctl restart docker

docker ps 查看

==重启策略:

no ,默认策略,在容器退出时不重启容器

on-failure,在容器非正常退出时(退出状态非0),才会重启容器

on-failure:3 ,在容器非正常退出时,最多重启3次 failure(失败)

always,在容器中退出时总是重启


unless-stopped ,在容器退出时总是重启容器,但不考虑守护进程启动时就已经停止了的容器

==如果创建时未指定 --restart=always,可以通过update 命名设置

语法:docker update--restart=always 容器ID或名字

例:

docker run -itd --name lihong666 centos bash

docker update --restart=always lihong666

systemctl stop docker

systemctl start docker

docker ps #查看