热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Docker容器技术介绍(一)Docker安装和简单使用

容器技术被认为是未来部署软件的主流趋势之一,高性能,轻量级及跨平台是容器技术的主要特征,传统的应用部署过程大概是:下载软件-

容器技术被认为是未来部署软件的主流趋势之一,高性能,轻量级及跨平台是容器技术的主要特征,传统的应用部署过程大概是:下载软件->编译安装->启动软件,比如现在要安装 LNMP 软件,就得先下载Nginx+Mysql+PHP,然后一个个安装,这对于一两台服务器时还能接受,要是面对成千上万的服务器集群,还不得累死吗,有没有办法把这几个软件打包成一个软件一次性安装呢,容器的出现使得很好的解决了这类问题。


什么是容器?


以上是摘自Docker官网的一段话,应该说是比较权威的解释:容器以一种特定的格式来打包软件,这种格式可以各自独立的运行在同一个操作系统之上,不像虚拟机,容器不会捆绑一个完整的操作系统,而仅仅打包软件运行所需要的依赖库和设置。这样可以实现一个高效、轻量级、自给自足的系统,并且可以保证软件总是以相同的方式运行,而不管它被部署在哪里。

从上可知,容器是一种打包应用的方式,可以帮你打包应用中的所有软件和软件所依赖的环境,并且可以实现跨平台部署。相比虚拟机,容器更高效和轻量化,一台服务器运行1000个虚拟机基本是难以想象的,但IBM的服务器据称已经在一台服务器上运行万级的容器。


为什么使用容器?




Docker是什么? docker是容器领导者。


Docker是世界领先的软件容器平台,在进行多人协作开发时,开发者可以使用Docker来消除所谓“我这里运行是好的”(works on my machine)问题,运维人员使用Docker来并行的运行和管理应用来获得更优计算密度,基于各自独立的容器。企业使用Docker来建立敏捷的交付方式,实现更快的、更安全的和更自信的交付新功能而不管是Linux或Windos服务器。


Docker安装:

Centos 6.x

[root@localhost ~]# yum install epel-release
[root@localhost ~]# yum install docker-io
[root@localhost ~]# service docker start


输入docker version 验证安装是否成功

[root@localhost ~]# docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64


Centos 7.x

最新版的Docker需要64位CentOS 7系统,并且分社区版(CE)和企业版(EE),一般选择免费的社区版即可

1.卸载旧版本(如果安装了的话)

[root@localhost ~]# yum remove docker docker-common docker-selinux docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-common
No Match for argument: docker-selinux
No Match for argument: docker-engine
No Packages marked for removal


2.安装


最简单的安装方式是直接 yum install:

[root@localhost ~]# yum install docker

如果想要安装最新的稳定版,可以使用下面的安装方式:

[root@localhost software]# wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.06.2.ce-1.el7.centos.x86_64.rpm
[root@localhost software]# ls
docker-ce-17.06.2.ce-1.el7.centos.x86_64.rpm
[root@localhost software]# yum install docker-ce-17.06.2.ce-1.el7.centos.x86_64.rpm 


3.启动Docker服务

[root@localhost software]# systemctl start docker
[root@localhost software]# ps -aux | grep docker
root 23624 2.0 2.5 434500 25768 ? Ssl 06:43 0:00 /usr/bin/dockerd
root 23627 0.1 0.7 263940 7544 ? Ssl 06:43 0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
root 23744 0.0 0.0 112660 972 pts/0 S+ 06:43 0:00 grep --color=auto docker


可以看到Docker在后台启动了两个守护进程:dockerd 和 docker-containerd


Docker的三个基本概念:镜像、容器、仓库

镜像相当于创建容器的一个模具,一个说明书,而容器就是一个实实在在运行的镜像实例,可以用可执行文件和进程来比喻镜像和容器的关系,而仓库就是用来存放镜像的,相当于git仓库,用来存放不同版本的镜像。

Docker安装和简单使用

安装运行Nginx

/*docker search 命令可以搜索镜像,默认从官方镜像库hub.docker.com搜索*/

[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 6903 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1130 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 446 [OK]
jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as... 227 [OK]
kong Open-source Microservice & API Management ... 115 [OK]
webdevops/php-nginx Nginx with PHP-FPM 90 [OK]
kitematic/hello-world-nginx A light-weight nginx container that demons... 86
bitnami/nginx Bitnami nginx Docker Image 36 [OK]
linuxserver/nginx An Nginx container, brought to you by Linu... 24
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 16 [OK]
tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 11 [OK]
wodby/drupal-nginx Nginx for Drupal 8 [OK]
webdevops/nginx Nginx container 8 [OK]
.....


/* pull 镜像 */

[root@localhost software]# docker pull nginx
latest: Pulling from nginx
1cb018da208f: Pull complete
c1a0d2b79b3f: Pull complete
0aec54b378f5: Pull complete
b599d18a520b: Pull complete
a1cfc1b806a4: Pull complete
c2504687d157: Pull complete
a00416541f84: Pull complete
6adf11c406b6: Pull complete
9daddd1b8b0f: Pull complete
036477bc0d5a: Pull complete
Digest: sha256:248f3c1a01b35a098c85b31c356787068b1c1adbbd2f902fb2d6f49b95fd380f
Status: Downloaded newer image for nginx:latest


/* 列出本地镜像 */

[root@localhost software]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              da5939581ac8        2 days ago          108MB


/* 启动一个容器,并把容器中的80端口映射到宿主机的8080端口 -d表示让容器在后台运行 */

[root@localhost software]# docker run -d -p 8080:80 nginx
9af52c8b4dab91643860a7355acb1f04013ed5fd8d0b313568322f26affa2c40


/* 列出当前正在运行的容器 */

[root@localhost software]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES
9af52c8b4dab        nginx               "nginx -g 'daemon ..."   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   xenodochial_volhard


/* 可以看到上面docker ps的信息没有显示全,有的被截断了 可以在命令加上 --no-trunc 参数强制完全显示 */

[root@localhost ~]# docker ps --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
724db6813f511ade01cb907ec82008a8930e4db0275cbaf36f6c8503a0cec7c9 nginx "nginx -g 'daemon off;'" 16 seconds ago Up 15 seconds 0.0.0.0:8080->80/tcp goofy_morse


/* 测试nginx是否正常启动 */

[root@localhost software]# curl '127.0.0.1:8080'





...


/* 停止容器运行 */

[root@localhost software]# docker stop 9af52c8b4dab
9af52c8b4dab
[root@localhost software]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


/* -a 参数会列出所有的容器,包括已停止的 */

[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
724db6813f51 nginx "nginx -g 'daemon of 5 minutes ago Exited (0) 6 seconds ago goofy_morse


/* docker rm 删除容器 */

[root@localhost ~]# docker rm 724db6813f51
724db6813f51
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES


/* docker rmi 删除镜像 */

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 b44ce450cb60 12 days ago 188 MB
nginx latest 036477bc0d5a 13 days ago 108.3 MB
[root@localhost ~]# docker rmi 036477bc0d5a
Untagged: nginx:latest
Deleted: 036477bc0d5a32c5618eb350669ebadcf082c41f1be3b0141d22284742bc796f
Deleted: 9daddd1b8b0faf3af09a3b25704ac2cb742a9a31114c0773c69f745364ac9569
Deleted: 6adf11c406b68fe96f4e5cbb5f8baf7123f8df13f77cda9d2439e8e3c994fc5b
Deleted: a00416541f8403247a87f49310e7ae687397bacd613b055a644aafedd4c0cbe1
Deleted: c2504687d1573cf99eb64283cfa4c160d4e2d15befbd617f5551e5a02242513e
Deleted: a1cfc1b806a41781e27bef106fb38e49f8d158253d318e032927d507c0285a37
Deleted: b599d18a520b37f6e0a587485e54dcc141c3a181f25b651fd45d071670b61b72
Deleted: 0aec54b378f58193513ddf12e1f855d44fb11ccf5810bd93687f6e13d107e8a0
Deleted: c1a0d2b79b3f6e909c6c621d4e33a3bda0ac46560d18602ca7ff3367360251e7
Deleted: 1cb018da208f97b77e79f5d42a29a05246afae8f7072683d93e06576af32b470
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 b44ce450cb60 12 days ago 188 MB



只输入docker可以查看docker支持的所有命令,输入docker COMMAND --help可以查看特定命令的帮助

[root@localhost software]# dockerUsage: docker COMMANDA self-sufficient runtime for containersOptions:--config string Location of client config files (default "/root/.docker")-D, --debug Enable debug mode--help Print usage-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 quitManagement Commands:config Manage Docker configscontainer Manage containersimage Manage imagesnetwork Manage networksnode Manage Swarm nodesplugin Manage pluginssecret Manage Docker secretsservice Manage servicesstack Manage Docker stacksswarm Manage Swarmsystem Manage Dockervolume Manage volumesCommands:attach Attach local standard input, output, and error streams to a running containerbuild Build an image from a Dockerfilecommit Create a new image from a container's changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a container's filesystem as a tar archivehistory Show the history of an imageimages List imagesimport Import the contents from a tarball to create a filesystem imageinfo Display system-wide informationinspect Return low-level information on Docker objectskill Kill one or more running containersload Load an image from a tar archive or STDINlogin Log in to a Docker registrylogout Log out from a Docker registrylogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the container
.....


[root@localhost software]# docker run --helpUsage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]Run a command in a new containerOptions:--add-host list Add a custom host-to-IP mapping (host:ip)-a, --attach list Attach to STDIN, STDOUT or STDERR--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)--blkio-weight-device list Block IO weight (relative device weight) (default [])--cap-add list Add Linux capabilities--cap-drop list Drop Linux capabilities--cgroup-parent string Optional parent cgroup for the container--cidfile string Write the container ID to the file--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota--cpu-rt-period int Limit CPU real-time period in microseconds--cpu-rt-runtime int Limit CPU real-time runtime in microseconds-c, --cpu-shares int CPU shares (relative weight)--cpus decimal Number of CPUs--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
......




Docker的简单使用就介绍到这里,想更深入了解Docker技术欢迎关注后续文章!

That‘s it ^^




推荐阅读
  • 云原生应用最佳开发实践之十二原则(12factor)
    目录简介一、基准代码二、依赖三、配置四、后端配置五、构建、发布、运行六、进程七、端口绑定八、并发九、易处理十、开发与线上环境等价十一、日志十二、进程管理当 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了作者在开发过程中遇到的问题,即播放框架内容安全策略设置不起作用的错误。作者通过使用编译时依赖注入的方式解决了这个问题,并分享了解决方案。文章详细描述了问题的出现情况、错误输出内容以及解决方案的具体步骤。如果你也遇到了类似的问题,本文可能对你有一定的参考价值。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • GAMETECH腾讯云游戏行业技术沙龙成都站圆满落幕
    11月13日,由腾讯云主办、游戏茶馆协办的2020年首场GAME-TECH腾讯云游戏行业技术沙龙在成都圆满落幕。本次沙龙邀请了腾讯云游戏行业解决方案总监宋永周、腾讯云游戏行业高级解决方案架构师曾梓恩、腾讯云游戏行业高级产品架构师郑晓曦、腾讯云游戏行业高级解决方案架构师温球良和天美L1(王者荣耀)服务器技术副总监杨光,为参会同行们带来了干货满满的技术建议。本文介绍了腾讯云游戏云的优势和为不同游戏研运场景提供的服务。腾讯云在中国游戏云服务市场领跑,成为众多游戏开发者的合作伙伴。 ... [详细]
  • Google在I/O开发者大会详细介绍Android N系统的更新和安全性提升
    Google在2016年的I/O开发者大会上详细介绍了Android N系统的更新和安全性提升。Android N系统在安全方面支持无缝升级更新和修补漏洞,引入了基于文件的数据加密系统和移动版本的Chrome浏览器可以识别恶意网站等新的安全机制。在性能方面,Android N内置了先进的图形处理系统Vulkan,加入了JIT编译器以提高安装效率和减少应用程序的占用空间。此外,Android N还具有自动关闭长时间未使用的后台应用程序来释放系统资源的机制。 ... [详细]
  • 本文介绍了RxJava在Android开发中的广泛应用以及其在事件总线(Event Bus)实现中的使用方法。RxJava是一种基于观察者模式的异步java库,可以提高开发效率、降低维护成本。通过RxJava,开发者可以实现事件的异步处理和链式操作。对于已经具备RxJava基础的开发者来说,本文将详细介绍如何利用RxJava实现事件总线,并提供了使用建议。 ... [详细]
  • GPT-3发布,动动手指就能自动生成代码的神器来了!
    近日,OpenAI发布了最新的NLP模型GPT-3,该模型在GitHub趋势榜上名列前茅。GPT-3使用的数据集容量达到45TB,参数个数高达1750亿,训练好的模型需要700G的硬盘空间来存储。一位开发者根据GPT-3模型上线了一个名为debuid的网站,用户只需用英语描述需求,前端代码就能自动生成。这个神奇的功能让许多程序员感到惊讶。去年,OpenAI在与世界冠军OG战队的表演赛中展示了他们的强化学习模型,在限定条件下以2:0完胜人类冠军。 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
  • springboot启动不了_Spring Boot + MyBatis 多模块搭建教程
    作者:枫本非凡来源:www.cnblogs.comorzlinp9717399.html一、前言1、创建父工程最近公司项目准备开始重构,框 ... [详细]
  • 负载均衡_Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分
    nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解 ... [详细]
  • 1.脚本功能1)自动替换jar包中的配置文件。2)自动备份老版本的Jar包3)自动判断是初次启动还是更新服务2.脚本准备进入ho ... [详细]
author-avatar
大王水灵灵_598
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有