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

来源:互联网 发布:河南省农产品出口数据 编辑:程序博客网 时间:2024/06/06 12:51

容器技术被认为是未来部署软件的主流趋势之一,高性能,轻量级及跨平台是容器技术的主要特征,传统的应用部署过程大概是:下载软件->编译安装->启动软件,比如现在要安装 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 versionClient version: 1.7.1Client API version: 1.19Go version (client): go1.4.2Git commit (client): 786b29d/1.7.1OS/Arch (client): linux/amd64Server version: 1.7.1Server API version: 1.19Go version (server): go1.4.2Git commit (server): 786b29d/1.7.1OS/Arch (server): linux/amd64

Centos 7.x

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

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

[root@localhost ~]# yum remove docker docker-common docker-selinux docker-engineLoaded plugins: fastestmirrorNo Match for argument: dockerNo Match for argument: docker-commonNo Match for argument: docker-selinuxNo Match for argument: docker-engineNo 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]# lsdocker-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 dockerroot     23624  2.0  2.5 434500 25768 ?        Ssl  06:43   0:00 /usr/bin/dockerdroot     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-runcroot     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   AUTOMATEDnginx                                                  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 nginx1cb018da208f: 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:248f3c1a01b35a098c85b31c356787068b1c1adbbd2f902fb2d6f49b95fd380fStatus: Downloaded newer image for nginx:latest

/* 列出本地镜像 */
[root@localhost software]# docker images REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEnginx               latest              da5939581ac8        2 days ago          108MB

/* 启动一个容器,并把容器中的80端口映射到宿主机的8080端口 -d表示让容器在后台运行 */
[root@localhost software]# docker run -d -p 8080:80 nginx     9af52c8b4dab91643860a7355acb1f04013ed5fd8d0b313568322f26affa2c40

/* 列出当前正在运行的容器 */
[root@localhost software]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES9af52c8b4dab        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                  NAMES724db6813f511ade01cb907ec82008a8930e4db0275cbaf36f6c8503a0cec7c9   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'   <!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }</style>...

/* 停止容器运行 */
[root@localhost software]# docker stop 9af52c8b4dab      9af52c8b4dab[root@localhost software]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

/* -a 参数会列出所有的容器,包括已停止的 */
[root@localhost ~]# docker ps -a CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES724db6813f51        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 -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

/* docker rmi 删除镜像 */
[root@localhost ~]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEubuntu              14.04               b44ce450cb60        12 days ago         188 MBnginx               latest              036477bc0d5a        13 days ago         108.3 MB[root@localhost ~]# docker rmi 036477bc0d5a  Untagged: nginx:latestDeleted: 036477bc0d5a32c5618eb350669ebadcf082c41f1be3b0141d22284742bc796fDeleted: 9daddd1b8b0faf3af09a3b25704ac2cb742a9a31114c0773c69f745364ac9569Deleted: 6adf11c406b68fe96f4e5cbb5f8baf7123f8df13f77cda9d2439e8e3c994fc5bDeleted: a00416541f8403247a87f49310e7ae687397bacd613b055a644aafedd4c0cbe1Deleted: c2504687d1573cf99eb64283cfa4c160d4e2d15befbd617f5551e5a02242513eDeleted: a1cfc1b806a41781e27bef106fb38e49f8d158253d318e032927d507c0285a37Deleted: b599d18a520b37f6e0a587485e54dcc141c3a181f25b651fd45d071670b61b72Deleted: 0aec54b378f58193513ddf12e1f855d44fb11ccf5810bd93687f6e13d107e8a0Deleted: c1a0d2b79b3f6e909c6c621d4e33a3bda0ac46560d18602ca7ff3367360251e7Deleted: 1cb018da208f97b77e79f5d42a29a05246afae8f7072683d93e06576af32b470[root@localhost ~]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEubuntu              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 configs  container   Manage containers  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  volume      Manage volumesCommands:  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.....

[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 ^^