Docker 简单入门

本贴最后更新于 2369 天前,其中的信息可能已经斗转星移

Docker 简介:

Docker 是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括 VMs(虚拟机)、bare metal、OpenStack 集群和其他的基础应用平台。

Docker 通常用于如下场景:

  • web 应用的自动化打包和发布;
  • 自动化测试和持续集成、发布;
  • 在服务型环境中部署和调整数据库或其他的后台应用;
  • 从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境

Docker 容器与 VM 的区别

715a503c5e23466b97350e1630ae4cca-VMArch.png 3b5673cc8e9d479da0c684e7f15f29fb-ContainerArch.png
VM 实在操作系统的基础上虚拟化出更多的操作系统,这些操作系统本身占有大量的资源来保证这个 VM 的正常运行,从而能保证 VM 支持应用的运行。而 Docker,直接操内核的基础上进行资源的划分隔离,利用的是系统内核的容器技术,使用起来更加的轻量,快捷。VM 的移植需要 VM 播放器,而 Docker 只需要你的操作系统支持。我们可以在一台虚拟机器上使用 Docker 创建一个 Ubuntu 容器集群。,但是你试试在一个虚拟机里里在创建 3 台虚拟机。我想在一个 Docker 容器里创建 web 运行环境,可以从镜像仓库拉取 Nginx,Redis,WordPress 博客系统镜像,这里的每一个镜像都能作为单独的一个容器,同时又可以作为一个服务,我们在一个容器中可以对多个容器进行服务编排,构建出一个完整从前台到数据库的服务架构。这显然是 VM 不能够做到的,你可以说我也可以在一个 VM 上安装 mysql,一台 VM 安装 Redis,一台 VM 部署 wordPress,这样当然可以,但是三台 VM 占用了 30G 的磁盘空间,以及至少 2G 的内存,最后在这三台 VM 上启动了不到 1G 的应用。最后,机器爆掉了。

Docker 的安装[Linux 下的安装]

//\ 安装 Docker CE,需使用以下版本的 64 位 ubuntu 系统:

Zesty 17.04 (LTS)
Yakkety 16.10
Xenial 16.04 (LTS)
Trusty 14.04 (LTS)

Docker CE 支持 Ubuntu on x86_64,armhf 和 s390x (IBM z Systems) 架构。

一、卸载自带的 Docker,版本过低

sudo apt-get remove docker docker-engine docker.io

二、添加 Docker 数据源

# Update the apt package index:
> sudo apt-get update

# Install packages to allow apt to use a repository over HTTPS:
>  sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

# Add Dockers official GPG key:
> curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
> sudo apt-key fingerprint 0EBFCD88

#Use the following command to set up the stable repository

admin64:
>sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

armhf:
>sudo add-apt-repository \
   "deb [arch=armhf] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

s390x:
>sudo add-apt-repository \
   "deb [arch=s390x] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

以上三条执行一个就可以.

三、下载最新版 Docker:

#下载
sudo apt-get install docker-ce

#查看Docker版本
rzx@rzx-vm:~$ docker -v
Docker version 17.09.0-ce, build afdb6d4

获取的是最新版本17.09.到这里Docker安装好了。

四、配置某个指定版本 Docker:

1、首先列出所有版本

rzx@rzx-vm:~$ apt-cache madison docker-ce
 docker-ce | 17.09.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
 docker-ce | 17.06.2~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
 docker-ce | 17.06.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
 docker-ce | 17.06.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages

2、选择一个版本进行配置:我这里选择了最新的版本,所以与刚才配置的一样。

rzx@rzx-vm:~$ sudo apt-get install docker-ce=17.09.0~ce-0~ubuntu
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
docker-ce 已经是最新版 (17.09.0~ce-0~ubuntu)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 120 个软件包未被升级。

五、运行示例 hello-world

rzx@rzx-vm:~$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.32/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
当前用户未授权,给这个用户授权Docker,也可以使用Sudo.

1、sudo 授权运行

rzx@rzx-vm:~$ sudo docker run hello-world
[sudo] rzx 的密码:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9a0669468bf7: Pull complete
Digest: sha256:0e06ef5e1945a718b02a8c319e15bae44f47039005530bc617a5d071190ed3fc
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
发现本地没有hello-world这个镜像,从镜像库Pull一个镜像下来,最终输出 Hello from Docker!

2、授权此当前用户

sudo usermod -aG docker rzx  

重启系统,再起启动

rzx@rzx-vm:~$ docker run hello-world
Hello from Docker!
运行成功

六、启动 Docker 服务

sudo service docker start

七、查看 Docker 进程

> ps -ef | grep docker
root       2156      1  0 15:09 ?        00:00:02 /usr/bin/dockerd 
root       2315   2156  0 15:09 ?        00:00:00 docker-containerd

Docker 镜像仓库

1、Address 注册一个账号,自己建立一个仓库,用于发布/共享/存储自己的镜像。

2、常用的镜像:

  Nginx
  Redis
  Mysql
  Ubuntu
  CentOS
  node

3、查看我的 Docker 本地镜像

rzx@rzx-vm:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              725dcfab7d63        4 days ago          1.84kB
只有一个hello-world

4、下载一个镜像 tomcat

rzx@rzx-vm:~$ docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
3e17c6eae66c: Pull complete
74d44b20f851: Pull complete
a156217f3fa4: Pull complete
8bceb9f57693: Pull complete
268410abd9cf: Pull complete
c5e2fb2d87cf: Pull complete
09681f714a7b: Pull complete
c20de950ce52: Pull complete
9f0d6027a2e7: Pull complete
b700e8d9a90e: Pull complete
8c4cc15d0add: Pull complete
5951d54ec94f: Pull complete
0ba82679ed3b: Pull complete
Digest: sha256:0f74521fd37dcbe2db5f26d16861240c283c774089dd45c9d6008cfbc8e8539a
Status: Downloaded newer image for tomcat:latest

可以看到本地镜像仓库已经多出一个 tomcat

rzx@rzx-vm:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              725dcfab7d63        4 days ago          1.84kB
tomcat              latest              1269f3761db5        4 weeks ago         560MB

启动 Tomcat :去镜像仓库查看 Tomcat

rzx@rzx-vm:~$ docker run -it --rm -p 8888:8080 tomcat
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /docker-java-home/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
08-Nov-2017 08:00:08.867 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.23
...省略。。。。
08-Nov-2017 08:00:14.572 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
08-Nov-2017 08:00:14.674 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
08-Nov-2017 08:00:14.721 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 4309 ms

访问 8888 端口:
32d4e490c4b6429a9c646d06ee0e4a61-tomcat.png

又看到了熟悉的 tom 猫。Tomcat 镜像将 tomcat 运行所需要的环境一同打包,不需要任何配置,就可以运行。后面可以将 war 包放入到此 tomcat 中运行。

Docker 的体系架构

628b41e3bc1641838995db1c3dec1c3d-DockerArchitecture.png

图中可以看到 Docker 体系主要是三个部分:

  1. Docker_HOST:宿主机,宿主机是存放容器镜像和启动容器的地方,同样可以构建镜像。
  2. Client:客户端,通过客户端(多语言 JAVA,Python...,我们使用的是终端 CLI)操作 Docker 的守护进程进行容器的构建,拉取,启动,运行。
  3. Registry:镜像仓库,我们可以从这里拉取/共享各种各样的镜像。*

Docker 的基本命令

docker build -t friendlyname .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

Docker 的使用:镜像构建/运行/发布

一、编写 Dockerfile:

二、编写 requirements.txt 和 app.py 两个文件

三、Bulid APP:构建成一个 Images 镜像

四、Run APP

五、Push APP

参考文件:

Docker 官方文档

...待续...

  • Docker

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的操作系统上。容器完全使用沙箱机制,几乎没有性能开销,可以很容易地在机器和数据中心中运行。

    478 引用 • 902 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...