1.docker的介绍
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
一个完整的Docker有以下几个部分组成:
DockerClient客户端 Docker Daemon守护进程 Docker Image镜像 DockerContainer容器
2.docker的架构
Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。Docker 容器通过 Docker 镜像来创建。容器与镜像的关系类似于面向对象编程中的对象与类。
docker | 对象 |
容器 | 对象 |
镜像 | 类 |
Docker采用 C/S架构 Docker daemon 作为服务端接受来自客户的请求,并处理这些请求(创建、运行、分发容器)。 客户端和服务端既可以运行在一个机器上,也可通过 socket 或者RESTful API 来进行通信。
Docker daemon 一般在宿主主机后台运行,等待接收来自客户端的消息。 Docker 客户端则为用户提供一系列可执行命令,用户用这些命令实现跟 Docker daemon 交互。(引自百度百科)
3.docker相对于传统虚拟技术的优势
docker在容器的基础上,进行了进一步的封装,从文件系统,网络互连到进程隔离等等,极大的简化了容器的创建与维护,更加的轻便便捷。
具体如下:
- 更高效的利用系统资源
- 更快速的启动时间
- 一致的运行环境
- 持续交付部署
- 迁移更轻松
- 维护扩展更加简单
4.部署步骤
- 环境准备
docker要求内核版本必须高于3.10,因此先查看自己的内核版本
[root@180-234 app]# uname -r 3.10.0-327.el7.x86_64
查看系统版本
[root@180-234 app]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)
- 安装
直接yum安装
[root@180-234 app]# yum -y install docker-io
[root@180-234 app]# yum list installed |grep docker docker.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos @extras docker-client.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos @extras docker-common.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos @extras
- 启动docker服务
[root@180-234 app]# systemctl start docker.service [root@180-234 app]# systemctl status docker.service ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2019-06-18 17:06:56 CST; 3s ago Docs: http://docs.docker.com Main PID: 2545 (dockerd-current) CGroup: /system.slice/docker.service ├─2545 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt nati... └─2555 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2... Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.461256816+08:00" level=info msg="Loading containers: start." Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.487427967+08:00" level=warning msg="Running modprobe bridge br_netfilter failed w... Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.502453408+08:00" level=info msg="Firewalld running: false" Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.600666484+08:00" level=info msg="Default bridge (docker0) is assigned wit...address" Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.643170897+08:00" level=info msg="Loading containers: done." Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.644456143+08:00" level=warning msg="Not using native diff for overlay2, t... to fix" Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.652556714+08:00" level=info msg="Daemon has completed initialization" Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.652587480+08:00" level=info msg="Docker daemon" commit="b2f74b2/1.13.1" g...n=1.13.1 Jun 18 17:06:56 180-234 dockerd-current[2545]: time="2019-06-18T17:06:56.657414854+08:00" level=info msg="API listen on /var/run/docker.sock" Jun 18 17:06:56 180-234 systemd[1]: Started Docker Application Container Engine. Hint: Some lines were ellipsized, use -l to show in full.
5.docker的基础命令
- 从公网拉取一个镜像hello-world
[root@180-234 app]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8 Status: Image is up to date for hello-world:latest
- 查看已有的docker镜像
[root@180-234 app]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 5 months ago 1.84kB hello-world latest fce289e99eb9 5 months ago 1.84kB
- 获取帮助
[root@180-234 app]# docker command --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -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 以下省略........
- 查看镜像列表
[root@180-234 app]# docker search hello-world NAME DESCRIPTION STARS OFFICIAL AUTOMATED hello-world Hello World! (an example of minimal Dockeriz… 953 [OK] kitematic/hello-world-nginx A light-weight nginx container that demonstr… 126 tutum/hello-world Image to test docker deployments. Has Apache… 61 [OK] dockercloud/hello-world Hello World! 15 [OK] crccheck/hello-world Hello World web server in under 2.5 MB 8 [OK] ppc64le/hello-world Hello World! (an example of minimal Dockeriz… 2 markmnei/hello-world-java-docker Hello-World-Java-docker 1 [OK] souravpatnaik/hello-world-go hello-world in Golang 1 carinamarina/hello-world-app This is a sample Python web application, run… 1 [OK] kevindockercompany/hello-world 0 freddiedevops/hello-world-spring-boot 0 burdz/hello-world-k8s To provide a simple webserver that can have … 0 [OK] koudaiii/hello-world 0 infrastructureascode/hello-world A tiny "Hello World" web server with a healt… 0 [OK] ansibleplaybookbundle/hello-world-db-apb An APB which deploys a sample Hello World! a… 0 [OK] uniplaces/hello-world 0 ansibleplaybookbundle/hello-world Simple containerized application that tests … 0 ansibleplaybookbundle/hello-world-apb An APB which deploys a sample Hello World! a… 0 [OK] nirmata/hello-world 0 [OK] stumacsolutions/hello-world-container 0 mbrainar/hello-world Python-based hello-world web service 0 sharor/hello-world 0 s390x/hello-world Hello World! (an example of minimal Dockeriz… 0 ebenjaminv9/hello-world Hello-world 0 kousik93/hello-world 0
- 启动一个容器ubuntu
基于ubuntu镜像启动容器,如果本地不存在会从公网拉取
[root@180-234 app]# docker run ubuntu
- 查看所有镜像
[root@180-234 app]# docker images
- 删除一个镜像
docker rmi <-f> 镜像名 :-f为强制删除
- 导出一个镜像
docker save -o image_name.tar image_name
- 以ubuntu镜像启动一个名叫ubuntu的容器
[root@180-234 app]# docker run --name=ubuntu 4c108a37151f
- 启动一个容器 同时进入
docker run -it 4c108a37151f ---->镜像ID
- 后台运行一个容器
[root@180-234 app]# docker run -itd --name=ubuntu ubuntu 649f6f4cf0c790b84da6bb55354f661c54f023fd16e9fb0ed61f1560a3c02973
- 进入容器
[root@180-234 app]# docker exec -it 649f6f4cf0c7 /bin/bash