您的位置:首页 > 运维架构 > Docker

Docker安装及基本操作入门

2017-12-19 00:00 821 查看
摘要: Docker安装的基本步骤及基本命令的使用

1、安装环境

CentOS Linux release 7.3.1611

Docker 版本 1.12.6

2、Docker安装步骤

1、yum 命令安装 Docker

yum install docker

安装完成后界面会打印如下信息:

Running transaction
Installing : 2:docker-1.12.6-68.gitec8512b.el7.centos.x86_64                                                                                                                                                                                                            1/1
Verifying  : 2:docker-1.12.6-68.gitec8512b.el7.centos.x86_64                                                                                                                                                                                                            1/1

Installed:
docker.x86_64 2:1.12.6-68.gitec8512b.el7.centos

Complete!

注意 yum默认安装的docker为1.12.6 版本,比较旧而且并不是开源社区维护的版本,目前开源社区最新版本为17.09.1,社区分为开原版本(docker-ce)和企业版本(docker-ee)。

所有版本如下图所示:



如果需要安装最新版本可以使用两种方式分别为 直接下载 docker RPM包 离线安装和 配置yum在线安装,分别如下:

a:离线安装

官方下载网址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

选择下载最新版本,如下:

docker-ce-17.09.1.ce-1.el7.centos.x86_64.rpm
docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm

下载完成后即可安装

b:yum配置官方源

为docker 增加一个新的yum配置文件:

vi /etc/yum.repos.d/docker.repo

具体内容如下:

[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=0
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://download.docker.com/linux/centos/7/source/stable
enabled=0
gpgcheck=1
3ff0
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://download.docker.com/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://download.docker.com/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

配置完成后即可搜索到最新的Docker文件如下图:

[root@localhost yum.repos.d]# yum search docker-ce
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.sohu.com
* epel: mirrors.sohu.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
=========================================================================================================================== N/S matched: docker-ce ===========================================================================================================================
docker-ce.x86_64 : The open-source application container engine
docker-ce-selinux.noarch : SELinux Policies for the open-source application container engine

Name and summary matches only, use "search all" for everything.

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

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


2、查看已安装Docker的版本

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

docker -v

显示版本信息如下:

Docker version 1.12.6, build ec8512b/1.12.6


3、启动Docker服务

systemctl start docker.service


4、执行测试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/[/code] 看到此信息,则说明安装Docker成功。

3、Docker基本命令使用

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

1、对镜像的操作

1、查看本地镜像

查看本地有哪些镜像文件

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


2、搜索镜像文件

可通过搜索查找自己想要的镜像,此处我们搜索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]


3、获取远程镜像

可按照搜索结果名称进行获取,此处我们获取 发布版本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


4、运行镜像

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

[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 ! 。

5、退出(关闭)镜像

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


exit 命令会退出容器并且关闭容器,如果只需要退出容器但不需要关闭则可按快捷键:Ctrl+P+Q

6、提交容器成为镜像

此处定义提交的容器的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


7、删除镜像文件

删除刚才新提交的镜像文件: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


2、对容器的操作

1、查看正在运行的镜像

在镜像运行后,关闭之前可以查看正在运行的镜像

[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


2、停止正在执行的容器

[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 ~]$


3、删除已经停止的容器

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

[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


4、启动已经停止的容器

[zhpt@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
d86eed7c
3ff0
4bf0        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 /]#


5、对容器重命名

因为容器的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 /]#


6、进入正在运行的容器

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


3、其他常用命令

1、导出镜像为文件

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

2、导入文件为镜像

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

3、重命名镜像

docker tag 2f8543cab450 elasticsearch-head:v1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Docker