目录

  1. 本地搭建Docker环境
    1. 一、What is Docker
      1. 1. 如何理解容器化
        1. ①远古时代
        2. ②虚拟化时代
        3. ③容器时代
      2. 2. Docker应用场景
    2. 二、What is Image and Container?
      1. 1. What is Image?
      2. 2. What is Container?
      3. 3. Relation between image and container
      4. 4. View from Docs
      5. 5. Container and virtual machines
    3. 三、 Docker Engine and Architecture
      1. 1. Docker Engine
      2. 2. Docker Architecture
    4. 四、 Install and Experience
      1. 1、 在Windows上准备centos7
        1. ①、 下载安装vagrant
        2. ②、 下载安装virtual box
        3. ③、 安装centos7
        4. ④、 若想通过ssh客户端连接
        5. ⑤、 box的打包分发
      2. 2、 安装Docker
      3. 3、 Docker基本体验
      4. 4、 解疑

本地搭建Docker环境

一、What is Docker

官方网站

1
Modernize your applications, accelerate innovation Securely build, share and run modern applications anywhere

使您的应用程序现代化,加速创新安全地在任何地方构建、共享和运行现代应用程序

Docs

1
Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

Docker是开发人员和系统管理员使用容器开发、部署和运行应用程序的平台。使用Linux容器部署应用程序称为容器化。容器不是新的,但它们用于轻松部署应用程序的用途。

1. 如何理解容器化

①远古时代

image-20191103143223919

问题:成本高、部署慢、浪费资源、硬件限制、不利于迁移扩展

②虚拟化时代

image-20191103143336773

优点:相对利用好资源、相对容易扩展等。

缺点:虚拟机太重了,一上来占用较多物理资源,移植性差,资源利用率低等。

③容器时代

image-20191103143456254

2. Docker应用场景

www.docker.com —>Solutions

1
2
3
4
5
有助于Microservices的落地和部署
充分利用物理机资源,同时能够整合服务器资源
提高开发效率,测试效率,部署效率,有利于DevOps的落地,CICD
云原生落地,应用更好的迁移
...

二、What is Image and Container?

1. What is Image?

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed
to run an application: code, runtime, system tools, system libraries and settings.

Docker容器映像是一个轻量级的、独立的、可执行的软件包,包含运行应用程序所需的一切:代码、运行时、系统工具、系统库和设置。

2. What is Container?

A container is a standard unit of software that packages up code and all its dependencies so the application runs
quickly and reliably from one computing environment to another.

容器是一个标准的软件单元,它将代码及其所有依赖项打包,以便应用程序从一个计算环境快速可靠地运行到另一个计算环境。

3. Relation between image and container

Container images become containers at runtime and in the case of Docker containers- images become containers when they
run on Docker Engine.

容器映像在运行时成为容器,在Docker容器的情况下,映像在Docker引擎上运行时成为容器。

4. View from Docs

A container is launched by running an image. An image is an executable package that includes everything needed to run
an application–the code, a runtime, libraries, environment variables, and configuration files.

A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with
state, or a user process). You can see a list of your running containers with the command, docker ps, just as you
would
in Linux.

通过运行图像启动容器映像是一个可执行包,包含运行应用程序所需的所有内容—代码、运行时、库、环境变量和配置文件。

容器是映像的运行时实例——映像在执行时在内存中变成什么(即,具有状态的映像或用户进程)。您可以使用命令docker
ps查看正在运行的容器的列表,就像在linux中一样。

5. Container and virtual machines

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete
process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources
through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

容器在Linux上本机运行,并与其他容器共享主机的内核。它运行一个离散的进程,占用的内存不比任何其他可执行文件多,因此它很轻。

相比之下,虚拟机(vm)运行一个完整的“来宾”操作系统,通过虚拟机监控程序对主机资源进行虚拟访问。一般来说,vm为环境提供的资源比大多数应用程序需要的资源多。

image-20191103145008913

三、 Docker Engine and Architecture

1. Docker Engine

Docker Engine is a client-server application with these major components:

  • A server which is a type of long-running program called a daemon process (the dockerd command)
    .(一种称为守护进程的长时间运行程序(dockerd命令)的服务器。)
  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.(一个rest
    api,它指定程序可以用来与守护进程对话并指示它做什么的接口。)
  • A command line interface (CLI) client (the docker command).(命令行界面(cli)客户端(“docker”命令))

image-20191103145245344

2. Docker Architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting
of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same
system,
or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST
API,
over UNIX sockets or a network interface.

Docker使用客户机-服务器架构。Docker客户端与Docker守护进程通信,后者负责构建、运行和分发Docker容器。Docker客户端和守护进程可以在同一个系统上运行,也可以将Docker客户端连接到远程Docker守护进程。Docker客户端和守护进程使用一个rest
api,通过unix套接字或网络接口进行通信。

image-20191103145535203

四、 Install and Experience

image-20191103145848986

1、 在Windows上准备centos7

采用vagrant + virtual box的方式进行环境搭建。

①、 下载安装vagrant

1
2
3
4
5
访问 [Vagrant](https://www.vagrantup.com) 官网
点击Download,分为Windows、MacOS、Linux等
选择对应的版本
傻瓜式安装
命令行输入vagrant,测试是否安装成功

image-20191103150515810

②、 下载安装virtual box

1
2
3
4
5
6
7
8
访问 [VirtualBox](https://www.virtualbox.org/) 官网
选择左侧的“Downloads”
选择对应的操作系统版本
傻瓜式安装
[win10中若出现]安装virtualbox快完成时立即回滚,并提示安装出现严重错误
(1)打开服务
(2)找到Dvice Install Service 和Device Setup Manager,然后启动
(3)再次尝试安装

③、 安装centos7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
在本地磁盘中创建frist-docker-centos7文件夹,并进入其中[目录路径不要有中文字符]
在此目录下打开cmd,运行vagrant init centos/7,此时会在当前目录下生产Vagrantfile文件
运行vagrant up[注意不要运行,默认拉取远端的centos7太慢],此时会找centos7的镜像,本地有就用本地的,本地没有就会拉取远端的。
准备centos7的box
(1)选中命令行中提的链接,比如https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
(2)复制到迅雷中下载
(3)vagrant box add centos/7 D:/first-docker-centos7/virtualbox.box
(4)vagrant box list 查看本地的box[这时候可以看到centos/7]
修改Vagrantfile
(1)config.vm.box=“centos/7”
(2)config.vm.network “public_network”
(3)config.vm.provider "virtualbox" do | vb |
vb.memory = "3000"
vb.name= "biwin2-centos7" /* 名字可自定义 */
vb.cpus= 2
根据本地的centos7 box创建虚拟机
vagrant up[打开virtual box,可以发现centos7创建成功]
vagrant基本操作
(1)vagrant ssh;进入刚才创建的centos7中
(2)vagrant status;查看centos7的状态
(3)vagrant halt;停止centos7
(4)vagrant destroy;删除centos7
(5)vagrant status;查看当前vagrant创建的虚拟机
(6)Vagrantfile中可以写脚本命令,使得centos7更加丰富,但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload

至此,使用vagrant+virtualbox搭建centos7完成,后面可以修改Vagrantfile对虚拟机进行相应的配置。

④、 若想通过ssh客户端连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
查看centos7的相关信息
vagrant ssh-config
关注:Hostname Port IdentityFile
Mobaxterm连接(Xshell连接同理)
IP: 127.0.0.1
PORT: 2222
User: vagrant
Password: vagrant
文件:Identifyfile指向的路径
目前通过上方的链接信息实际上并不能远程连接,我们还需要以下步骤
在cmd中进行远程连接:vagrant ssh
sudo -i
vi /etc/ssh/sshd_config
修改PasswordAuthentication yes
passwd 修改密码(我本机密码都是vagrant)
systemctl restart sshd
root vagrant 登录

⑤、 box的打包分发

1
2
3
4
5
6
7
8
9
10
11
退出虚拟机
vagrant halt
打包
vagrant package --output default-docker.box
得到default-docker.box
将default-docker.box添加到其他的vagrant环境中
vagrant box add default-docker default-docker.box
得到Vagrantfile
vagrant init default-docker
根据Vagrantfile启动虚拟机
vagrant up[此时可以得到和之前一模一样的环境,但是网络要重新配置]

2、 安装Docker

https://docs.docker.com/install/linux/docker-ce/centos/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
进入centos7
vagrant ssh
卸载之前的docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-engine
安装必要的依赖
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
设置docker仓库
sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo,设置阿里云镜像加速器
安装docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
启动docker
sudo systemctl start docker && sudo systemctl enable docker
测试docker安装是否成功
sudo docker run hello-world

3、 Docker基本体验

1
2
3
4
5
6
7
8
9
创建tomcat容器
docker pull tomcat
docker run -d --name my-tomcat -p 9090:8080 tomcat

创建mysql容器
docker run -d --name my-mysql -p 3301:3306 -e MYSQL_ROOT_PASSWORD=biwin --privileged mysql

进入到容器里面
docker exec -it containerid /bin/bash

4、 解疑

(1)docker pull在哪里拉取的镜像?

默认是在hub.docker.com

(2)docker pull tomcat拉取的版本是?

默认是最新的版本,可以在后面指定版本“:”

(3)简单先说下命令

docker pull 拉取镜像到本地

docker run 根据某个镜像创建容器

-d 让容器在后台运行,起始就是一个进程

–name 给容器指定一个名字

-p 将容器的端口映射到宿主机的端口

docker exec -it 进入到某个容器中并交互式运行

参考:Docker基础(一)——环境搭建及常用命令