DevOps 之路 —— Docker基础
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
Docker 是 DevOps 的重要一个组成部分,不可缺少,可以说云计算无法离开docker,或者说无法离开容器,容器已经成为云计算的重要基础设施。
Docker 是基于Linux 内核的一种虚拟化容器(Container),主要用到 CGroups,Namespace(ipc,network, user,pid,mount),UnionFileSystem 等技术封装成一种自定义的容器格式,用于提供一整套虚拟运行环境。
利用Docker容器技术,开发人员&运维 可以快速的对应用程序进行“集装箱化”封装,随时部署、分发应用程序,免去了传统运维带来的复杂性、不可控、结果不一致等问题。
集装箱化的优点:
灵活:即使是复杂的应用程序也可封装。
轻量级:容器利用并共享主机内核。
便携式:您可以在本地构建,部署到云上并在任何地方运行。
可扩展性:您可以增加和自动分发容器副本。
可堆叠:您可以垂直堆叠服务并及时并及时堆叠服务。
虚拟机和容器对比:
VM(VMware)在宿主机的硬件和操作系统基础上构建虚拟机,虚拟机会占用较多的CPU资源及内存,数据重量级的虚拟化,对于云计算而言最大的弊端就是启动缓慢,如需要启动突发实例,启动时间会很长,无法快速响应
Docker容器是共享操作系统内核,属于轻量级虚拟化技术,容器本身的资源开销极低,容器的启动也非常快(秒级)
Docker引擎的主要组成部分:
Daemon :Docker进程守护 ,负责后台进程管理,镜像管理,容器管理以及数据卷
Client : 用于与Docker Daemon交互
Image :Docker容器运行的镜像文件,通常是一个linux系统,里面包含一个或多个可运行的服务,例如Nginx、Tomcat、Spring Boot 等。
Services :服务是docker swarm引入的概念,可以在多宿主机之间伸缩容器数目,支持负载均衡已经服务路由功能。
Docker 实践:
1.服务安装
更新安装源
apt-get update
apt-get install -y apt-transport-https gnupg-agent software-properties-common
添加阿里云安装源的密钥
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
apt-get update
apt-get install -y apt-transport-https gnupg-agent software-properties-common
添加阿里云安装源
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
安装最新版 docker engine
apt update
apt install docker-ce
配置 docker 加速镜像
cat <<EOF > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://a35khyup.mirror.aliyuncs.com"
],
"exec-opts": [
"native.cgroupdriver=systemd"
]
}
EOF
重启 docker
service docker restart
1.基本命令 (ubuntu 18.04)
@查看docker 命令集
docker
@输出----------------------------------------------------------------------------------------------------------------------------------
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
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
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to