Docker在CentOS 7 环境下的安装及基本操做入门

Docker 是一个开源的应用容器引擎,让开发者能够打包他们的应用以及依赖包到一个可移植的容器中,而后发布到任何流行的 Linux 机器上,也能够实现虚拟化。容器是彻底使用沙箱机制,相互之间不会有任何接口。php

        此处主要讲解在CentOS-7.3中安装Docker环境python

 

一、安装环境

         CentOS Linux release 7.3.1611mysql

         最新版本 Docker  19.03.1linux

二、Docker安装步骤

       注意 yum默认安装的docker为 1.13 版本,比较旧并且并非开源社区维护的版本,目前开源社区最新版本为 19.03.1,社区分为开源版本(docker-ce)和企业版本(docker-ee)。 须要配置Docker官方源才能够安装最新版本,此处咱们安装开源社区版本 docker-ce。sql

一、卸载旧版本的Docker

yum remove docker \
                  docker-common \
                  docker-selinux \
                  docker-engine

二、添加国内yuan

    首先添加依赖,以下:docker

yum install -y yum-utils device-mapper-persistent-data lvm2

    鉴于国内网络问题,强烈建议使用国内源,以下:json

yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

    安装最新版docker,此处安装社区开源版,以下:ubuntu

[root@localhost yum.repos.d]# yum install -y docker-ce

 

三、查看已安装Docker的版本

       执行以下命令可查看Docker版本:centos

docker -v

       显示版本信息以下:ruby

Docker version 19.03.1, build 74b1e89

四、启动Docker服务 并 设置开机自启动 docker

systemctl start docker && systemctl enable docker

五、执行测试hello-world镜像

docker run hello-world

      此时会出现没法链接的状况,由于Docker的官网被墙,默认的镜像源地址没法链接,会提示以下信息:

[root@localhost docker]# docker run hello-world
/usr/bin/docker-current: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See '/usr/bin/docker-current run --help'.

       解决此问题则须要修改镜像源地址,修改 /etc/docker/daemon.json 文件,填写Docker 官方中国区镜像源地址,修改后/etc/docker/daemon.json 内容以下:

{
 "registry-mirrors": ["https://registry.docker-cn.com"]
}

        也可以使用国内其余Docker镜像源地址

  • 网易:http://hub-mirror.c.163.com
  • ustc:https://docker.mirrors.ustc.edu.cn

        修改完成后从新执行 测试hello-world镜像,本地没有的话则会从远端镜像库下载,成功执行信息以下:

[root@localhost docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:445b2fe9afea8b4aa0b2f27fe49dd6ad130dfe7a8fd0832be5de99625dad47cd

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

       看到此信息,则说明安装Docker成功。

三、docker-compose 安装

        Compose 是一个用户定义和运行多个容器的 Docker 应用程序。在 Compose 中你可使用 YAML 文件来配置你的应用服务。而后,只须要一个简单的命令,就能够建立并启动你配置的全部服务。

使用 Compose 基本会有以下三步流程:

  1. 在 Dockfile 中定义你的应用环境,使其能够在任何地方复制。
  2. 在 docker-compose.yml 中定义组成应用程序的服务,以便它们能够在隔离的环境中一块儿运行。
  3. 最后,运行dcoker-compose up,Compose 将启动并运行整个应用程序。

 

        安装python-pip

yum -y install epel-release
yum -y install python-pip

        安装docker-compose

pip install docker-compose

        装完成后,执行查询版本的命令,便可安装docker-compose

[root@localhost ~]# docker-compose version
docker-compose version 1.21.2, build a133471
docker-py version: 3.3.0
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017

四、Docker基本命令使用

须要首先清晰的认识Docker中镜像与容器之间的关系,镜像和容器就至关于Java中的类和对象的关系,镜像是对容器的初始定义,运行镜像以后会获得容器,容器提交版本后会生成镜像,Docker 的大部分操做都是针对容器的。

一、对镜像的操做

一、查看本地镜像

      查看本地有哪些镜像文件

[root@localhost docker]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              f2a91732366c        4 weeks ago         1.848 kB

二、搜索镜像文件

      可经过搜索查找本身想要的镜像,此处咱们搜索centos

[root@localhost docker]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   3893      [OK]       
docker.io   docker.io/ansible/centos7-ansible            Ansible on Centos7                              103                  [OK]
docker.io   docker.io/jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8...   90                   [OK]
docker.io   docker.io/consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   37                   [OK]
docker.io   docker.io/imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              35                   [OK]
docker.io   docker.io/tutum/centos                       Simple CentOS docker image with SSH access      34                   
docker.io   docker.io/gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glu...   21                   [OK]
docker.io   docker.io/kinogmt/centos-ssh                 CentOS with SSH                                 18                   [OK]
docker.io   docker.io/centos/python-35-centos7           Platform for building and running Python 3...   14                   
docker.io   docker.io/openshift/base-centos7             A Centos7 derived base image for Source-To...   13                   
docker.io   docker.io/openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use...   9                    
docker.io   docker.io/openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 ima...   6                    
docker.io   docker.io/darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
docker.io   docker.io/openshift/ruby-20-centos7          DEPRECATED: A Centos7 based Ruby v2.0 imag...   3                    
docker.io   docker.io/blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
docker.io   docker.io/indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developm...   1                    [OK]
docker.io   docker.io/miko2u/centos6                     CentOS6 日本語環境                                   1                    [OK]
docker.io   docker.io/openshift/php-55-centos7           DEPRECATED: A Centos7 based PHP v5.5 image...   1                    
docker.io   docker.io/pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag nam...   1                    
docker.io   docker.io/pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile...   1                    
docker.io   docker.io/jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                    [OK]
docker.io   docker.io/openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for us...   0                    
docker.io   docker.io/pivotaldata/centos                 Base centos, freshened up a little with a ...   0                    
docker.io   docker.io/pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated ...   0                    
docker.io   docker.io/smartentry/centos                  centos with smartentry                          0                    [OK]

三、获取远程镜像

       可按照搜索结果名称进行获取,此处咱们获取 发布版本Centos:The official build of CentOS

[root@localhost docker]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
85432449fd0f: Pull complete 
Digest: sha256:3b1a65e9a05f0a77b5e8a698d3359459904c2a354dc3b25ae2e2f5c95f0b3667

四、运行镜像

      此处运行刚才获取的镜像,镜像运行以后成为容器。

[root@localhost docker]# docker run -i -t docker.io/centos /bin/bash
[root@d86eed7c4bf0 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

                此时已进入镜像文件内部,此时咱们能够对此容器进行更改,好比在 /home目录下建立test.txt文件,内容为 Hello my test file ! 。

五、退出(关闭)镜像

[root@d86eed7c4bf0 /]# exit   
exit
[root@localhost docker]#

exit 命令会退出容器而且关闭容器,若是只须要退出容器但不须要关闭则可按快捷键:Ctrl+P+Q

六、提交容器成为镜像

     此处定义提交的容器的Tab为 v1.1

[zhpt@localhost ~]$ docker commit -m ="Commit message" -a="Author" 22fd7c4c0668 docker.io/centos:v1.1
sha256:acef7126a6ac4e5abd3acfcd92655d1e41a45b9cc57b7d60eea7d75dc175667e

     但是用 docker images 命令查看新提交的镜像,以下:

[zhpt@localhost ~]$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos        v1.1                acef7126a6ac        4 minutes ago       271.3 MB
docker.io/centos        latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world   latest              f2a91732366c        4 weeks ago         1.848 kB

 七、删除镜像文件

     删除刚才新提交的镜像文件:docker.io/centos:v1.1

[zhpt@localhost ~]$ docker rmi docker.io/centos:v1.1
Untagged: docker.io/centos:v1.1
Deleted: sha256:acef7126a6ac4e5abd3acfcd92655d1e41a45b9cc57b7d60eea7d75dc175667e
Deleted: sha256:47744657ffff5c29bc5f73c0bc8ad9677688a50d89dda5777ef7467603fe2469
[zhpt@localhost ~]$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos        latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world   latest              f2a91732366c        4 weeks ago         1.848 kB

二、对容器的操做

一、查看正在运行的镜像

      在镜像运行后,关闭以前能够查看正在运行的镜像

[zhpt@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         About a minute ago   Up About a minute                       elated_wescoff

二、中止正在执行的容器

[zhpt@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         15 hours ago        Up 4 minutes                            elated_wescoff
[zhpt@localhost ~]$ docker kill d86eed7c4bf0
d86eed7c4bf0
[zhpt@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[zhpt@localhost ~]$

也可中止全部正在执行的容器

[zhpt@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         15 hours ago        Up 2 minutes                            elated_wescoff
[zhpt@localhost ~]$ docker kill $(docker ps -q)
d86eed7c4bf0
[zhpt@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[zhpt@localhost ~]$

 三、删除已经中止的容器

     只可删除已经中止的容器,正在运行的容器是没法删除的。

[zhpt@localhost ~]$ docker rm 22fd7c4c0668
22fd7c4c0668
[zhpt@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         39 minutes ago      Exited (127) 38 minutes ago                        elated_wescoff
bda7d545ac4c        docker.io/centos    "/bin/bash"         2 hours ago         Exited (0) About an hour ago                       clever_mestorf
e1c6121e4845        hello-world         "/hello"            2 hours ago         Exited (0) 2 hours ago                             suspicious_ardinghelli

       删除全部已经中止的容器

[root@localhost zhpt]# docker rm $(docker ps -a -q)
2853ae52f76d
5c721cd6810b
fd2123aa64a2
84a6405887b4
bda7d545ac4c
e1c6121e4845

四、启动已经中止的容器

[zhpt@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         15 hours ago        Exited (137) 47 seconds ago                       elated_wescoff
bda7d545ac4c        docker.io/centos    "/bin/bash"         17 hours ago        Exited (0) 16 hours ago                           clever_mestorf
e1c6121e4845        hello-world         "/hello"            17 hours ago        Exited (0) 6 minutes ago                          suspicious_ardinghelli
[zhpt@localhost ~]$ docker start -i d86eed7c4bf0
[root@d86eed7c4bf0 /]#

五、对容器重命名

      由于容器的ID是随机码,而容器的名字又是看似无心义的命名,咱们可使用命令重命名,可以使用新名称对容器进行操做。

[zhpt@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         15 hours ago        Exited (137) 4 minutes ago                       elated_wescoff
bda7d545ac4c        docker.io/centos    "/bin/bash"         17 hours ago        Exited (0) 16 hours ago                          clever_mestorf
e1c6121e4845        hello-world         "/hello"            17 hours ago        Exited (0) 14 minutes ago                        suspicious_ardinghelli
[zhpt@localhost ~]$ docker rename elated_wescoff my_centos
[zhpt@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         15 hours ago        Exited (137) 4 minutes ago                       my_centos
bda7d545ac4c        docker.io/centos    "/bin/bash"         17 hours ago        Exited (0) 16 hours ago                          clever_mestorf
e1c6121e4845        hello-world         "/hello"            17 hours ago        Exited (0) 14 minutes ago                        suspicious_ardinghelli
[zhpt@localhost ~]$ docker start -i my_centos 
[root@d86eed7c4bf0 /]#

六、进入正在运行的容器

[zhpt@localhost ~]$ docker start -i my_centos 
[root@d86eed7c4bf0 /]# [zhpt@localhost ~]$ 
[zhpt@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d86eed7c4bf0        docker.io/centos    "/bin/bash"         16 hours ago        Up 14 minutes                           my_centos
[zhpt@localhost ~]$ docker attach d86eed7c4bf0
[root@d86eed7c4bf0 /]#

    推荐使用以下方式:

[root@localhost es]# docker exec -it 0f19337a8ac9 /bin/bash
root@0f19337a8ac9:/usr/src/app#

三、其余经常使用命令

    一、导出镜像为文件

  • docker save 2f8543cab450 > /root/images/elasticsearch-head.tar

    二、导入文件为镜像

  • docker load < /home/zhpt/es/elasticsearch-head.tar

    三、重命名镜像

  • docker tag 2f8543cab450  elasticsearch-head:v1
相关文章
相关标签/搜索