Docker基础入门

docker基础教程:

第一章:docker与虚拟机的区别

docker与虚拟机的主要区别在于docker是一个在你本机操作系统中独立运行的一个进程,而虚拟机则是一个完整的隔离的操作系统,它运行在你本机操作系统之上,需要更多时间和资源来加载,所以docker容器化比虚拟机有更大优势。但是docker并不是虚拟机的替代品,而是为了解决特定的问题。

例如:

1、加载速度与虚拟机不同,所需硬件资源很少。

2、在同一操作系统上可以同时运行多个docker容器。

3、可以对容器进行修改并部署。

第二章:docker的安装

在开始docker之前要确保已安装好docker

第一步:安装工具包

$ yum install -y yum-utils #安装工具包,缺少这些依赖将无法完成

执行结果:

Loaded plugins: fastestmirror, langpacks
base                                                                                          | 3.6 kB  00:00:00
epel                                                                                          | 4.3 kB  00:00:00
extras                                                                                        | 3.4 kB  00:00:00
update                                                                                        | 3.4 kB  00:00:00
(1/3): epel/7/x86_64/updateinfo                                                               | 797 kB  00:00:00
(2/3): epel/7/x86_64/primary_db                                                               | 4.7 MB  00:00:00
(3/3): update/7/x86_64/primary_db                                                             | 4.8 MB  00:00:00
Loading mirror speeds from cached hostfile
Package yum-utils-1.1.31-40.el7.noarch already installed and latest version
Nothing to do

第二步:设置远程仓库

$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

执行结果:

Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

第三步:安装

yum install docker-ce

执行结果:

Loaded plugins: fastestmirror, langpacks
docker-ce-stable                                                                              | 2.9 kB  00:00:00
docker-ce-stable/x86_64/primary_db                                                            | 4.8 kB  00:00:00
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:17.03.1.ce-1.el7.centos will be installed
--> Processing Dependency: docker-ce-selinux >= 17.03.1.ce-1.el7.centos for package: docker-ce-17.03.1.ce-1.el7.centos.x86_64
--> Processing Dependency: libcgroup for package: docker-ce-17.03.1.ce-1.el7.centos.x86_64
--> Processing Dependency: libseccomp.so.2()(64bit) for package: docker-ce-17.03.1.ce-1.el7.centos.x86_64
--> Running transaction check
---> Package docker-ce-selinux.noarch 0:17.03.1.ce-1.el7.centos will be installed
--> Processing Dependency: policycoreutils-python for package: docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch
---> Package libcgroup.x86_64 0:0.41-11.el7 will be installed
---> Package libseccomp.x86_64 0:2.3.1-2.el7 will be installed
--> Running transaction check
---> Package policycoreutils-python.x86_64 0:2.5-11.el7_3 will be installed
--> Processing Dependency: setools-libs >= 3.3.8-1 for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-5 for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.6.5-3.el7_3.1 will be installed
---> Package checkpolicy.x86_64 0:2.5-4.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-5.1.el7_3 will be installed
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-1.1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================
 Package                         Arch            Version                             Repository                 Size
=====================================================================================================================
Installing:
 docker-ce                       x86_64          17.03.1.ce-1.el7.centos             docker-ce-stable           19 M
Installing for dependencies:
 audit-libs-python               x86_64          2.6.5-3.el7_3.1                     update                     70 k
 checkpolicy                     x86_64          2.5-4.el7                           base                      290 k
 docker-ce-selinux               noarch          17.03.1.ce-1.el7.centos             docker-ce-stable           28 k
 libcgroup                       x86_64          0.41-11.el7                         base                       65 k
 libseccomp                      x86_64          2.3.1-2.el7                         base                       56 k
 libsemanage-python              x86_64          2.5-5.1.el7_3                       update                    104 k
 policycoreutils-python          x86_64          2.5-11.el7_3                        update                    445 k
 python-IPy                      noarch          0.75-6.el7                          base                       32 k
 setools-libs                    x86_64          3.3.8-1.1.el7                       base                      612 k

Transaction Summary
=====================================================================================================================
Install  1 Package (+9 Dependent packages)

Total download size: 20 M
Installed size: 24 M
Is this ok [y/d/N]: y
Downloading packages:
(1/10): audit-libs-python-2.6.5-3.el7_3.1.x86_64.rpm                                          |  70 kB  00:00:00
(2/10): checkpolicy-2.5-4.el7.x86_64.rpm                                                      | 290 kB  00:00:00
(3/10): libseccomp-2.3.1-2.el7.x86_64.rpm                                                     |  56 kB  00:00:00
(4/10): libcgroup-0.41-11.el7.x86_64.rpm                                                      |  65 kB  00:00:00
(5/10): policycoreutils-python-2.5-11.el7_3.x86_64.rpm                                        | 445 kB  00:00:00
(6/10): setools-libs-3.3.8-1.1.el7.x86_64.rpm                                                 | 612 kB  00:00:00
(7/10): libsemanage-python-2.5-5.1.el7_3.x86_64.rpm                                           | 104 kB  00:00:00
(8/10): python-IPy-0.75-6.el7.noarch.rpm                                                      |  32 kB  00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm is not installed
(9/10): docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm                                  |  28 kB  00:00:00
(10/10): docker-ce-17.03.1.ce-1.el7.centos.x86_64.rpm                                         |  19 MB  00:00:00
---------------------------------------------------------------------------------------------------------------------
Total                                                                                 23 MB/s |  20 MB  00:00:00
Retrieving key from https://download.docker.com/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://download.docker.com/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libcgroup-0.41-11.el7.x86_64                                                                     1/10
  Installing : setools-libs-3.3.8-1.1.el7.x86_64                                                                2/10
  Installing : checkpolicy-2.5-4.el7.x86_64                                                                     3/10
  Installing : libsemanage-python-2.5-5.1.el7_3.x86_64                                                          4/10
  Installing : audit-libs-python-2.6.5-3.el7_3.1.x86_64                                                         5/10
  Installing : python-IPy-0.75-6.el7.noarch                                                                     6/10
  Installing : policycoreutils-python-2.5-11.el7_3.x86_64                                                       7/10
  Installing : docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch                                                 8/10
setsebool:  SELinux is disabled.
libsemanage.semanage_direct_install_info: Overriding docker module at lower priority 100 with module at priority 400.
  Installing : libseccomp-2.3.1-2.el7.x86_64                                                                    9/10
  Installing : docker-ce-17.03.1.ce-1.el7.centos.x86_64                                                        10/10
  Verifying  : libseccomp-2.3.1-2.el7.x86_64                                                                    1/10
  Verifying  : python-IPy-0.75-6.el7.noarch                                                                     2/10
  Verifying  : audit-libs-python-2.6.5-3.el7_3.1.x86_64                                                         3/10
  Verifying  : libsemanage-python-2.5-5.1.el7_3.x86_64                                                          4/10
  Verifying  : docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch                                                 5/10
  Verifying  : libcgroup-0.41-11.el7.x86_64                                                                     6/10
  Verifying  : policycoreutils-python-2.5-11.el7_3.x86_64                                                       7/10
  Verifying  : docker-ce-17.03.1.ce-1.el7.centos.x86_64                                                         8/10
  Verifying  : checkpolicy-2.5-4.el7.x86_64                                                                     9/10
  Verifying  : setools-libs-3.3.8-1.1.el7.x86_64                                                               10/10

Installed:
  docker-ce.x86_64 0:17.03.1.ce-1.el7.centos

Dependency Installed:
  audit-libs-python.x86_64 0:2.6.5-3.el7_3.1                    checkpolicy.x86_64 0:2.5-4.el7
  docker-ce-selinux.noarch 0:17.03.1.ce-1.el7.centos            libcgroup.x86_64 0:0.41-11.el7
  libseccomp.x86_64 0:2.3.1-2.el7                               libsemanage-python.x86_64 0:2.5-5.1.el7_3
  policycoreutils-python.x86_64 0:2.5-11.el7_3                  python-IPy.noarch 0:0.75-6.el7
  setools-libs.x86_64 0:3.3.8-1.1.el7

Complete!

第四步:启动

$ service docker start #启动docker
chkconfig docker on    #加入开机启动

第五步:查看版本

$ docker version

执行结果:

Client:
 Version:      17.03.1-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Mon Mar 27 17:05:44 2017
 OS/Arch:      linux/amd64

第六步:校验

$ docker run hello-world

执行结果:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pull complete
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
Status: Downloaded newer image for hello-world:latest

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.
 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 --help  #帮助
docker -v      #简单查看版本
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

docker info #查看信息

Containers: 0
Images: 2
Storage Driver: devicemapper
 Pool Name: docker-253:0-34097258-pool
 Pool Blocksize: 65.54 kB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 308.3 MB
 Data Space Total: 107.4 GB
 Data Space Available: 16.08 GB
 Metadata Space Used: 782.3 kB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.147 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2015-10-14)
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
CPUs: 4
Total Memory: 985.6 MiB
Name: localhost.localdomain
ID: B5ZW:FJXF:FNUD:OLH7:FCNI:56DJ:XEQY:I6J4:PPHQ:OKRW:CIJK:Y26P

第三章:docker的基本概念

镜像(image)

docker镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变

容器(container)

镜像(image)和容器(container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等操作。

仓库(repository)

镜像构建完成后,可以很容易的在当前宿主机上运行,但是,如果需要在其他服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,docker registry就是这个的服务。一个docker registry中可以包含多个仓库(repository),每个仓库可以包含多个标签(tag),每个标签对应一个镜像。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就是常用于对应该软件的各个版本。我们可以通过<仓库名>:<标签>的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以latest作为默认标签。

1、镜像
#获取镜像

docker pull ubuntu:14.04

#以ubuntu:14.04镜像为基础启动并运行一个容器

docker run -it --rm ubuntu:14.04 bash

-it:这是两个参数,一个是-i:交互式操作,一个是-t:终端。我们这里打算进入bash执行一些命令并查看返回结果,因此我们需要交互式终端。

--rm:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动 docker rm。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用 --rm 可以避免浪费空间。

ubuntu:14.04:这是指用 ubuntu:14.04 镜像为基础来启动容器。

bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 bash。

#列出镜像
docker images / docker image ls

#镜像占用
docker system df

#清楚悬挂镜像
docker image prune

#删除镜像
docker rmi 镜像id / docker image rm 镜像id
2、容器
#启动以守护模式创建的名字为demo-u的容器,并以交互模式进入容器
docker run --name demo-u -t -i -d ubuntu:14.04 bash

#运行后就可以通过ID或名字进入容器,并输出hello world
docker exec -it demo-u /bin/sh -c "echo hello world"

#查看运行中的容器
docker container ls
docker ps

#所有容器
docker container ls -a
docker ps -a

#查看容器日志
docker container logs demo-u
docker logs demo-u

#终止容器
docker container stop demo-u
docker stop demo-u

#启动容器
docker container start demo-u
docker start demo-u

#重启容器
docker container restart demo-u
docker restart demo-u

#进入容器,退出后容器也停止
docker attach demo-u

#导出容器
docker export

#导入容器
docker import

#删除容器
docker container rm
docker rm 容器名/容器id

#清除所有容器
docker container prune
3、数据卷
#创建数据卷
docker volume create

#列出数据卷
docker volume ls

#删除数据卷
docker volume rm

#清除没用的数据卷
docker volume prune
4、挂载

也就是目录共享,两种方式:

  • -v
  • –mount 推荐
# 使用php本地服务器查看php环境,加载主机的 ~/web 目录到容器的 /var/www/web目录
mkdir -p ~/web && cd ~/web && echo "<?php phpinfo();" > index.php
docker run -d \
    --name web \
    -p 8080:8080 \
    --mount type=bind,source=`pwd`,target=/var/www/web \
    php:7.2-fpm \
    /bin/sh -c "cd /var/www/web && php -S 0.0.0.0:8080"

或者

docker run -d \
    --name web \
    -p 8080:8080 \
    -v `pwd`:/var/www/web \
    php:7.2-fpm \
    /bin/sh -c "cd /var/www/web && php -S 0.0.0.0:8080"

打开浏览器 0.0.0.0:8080

# 查看数据卷
docker volume inspect web

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不被定义的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值