docker安装-官网实例中文(centos7)

本文档介绍了在CentOS7上安装Docker的步骤,包括关闭防火墙、配置SELINUX和安装Docker。接着,通过运行'hello-world'和'whalesay'镜像来演示Docker的基本用法,并详细解释了Docker镜像和容器的概念。此外,还指导了如何构建自定义镜像和创建Docker Hub账户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.    docker安装

1.1         关闭防火墙

# systemctl stopfirewalld

# systemctl disablefirewalld

1.2         SELINUX 配置

编辑文件/etc/selinux/config ,设置

SELINUX=DISABLED

然后运行命令

# setenforce 0

重启服务器

# reboot

 

1.3         开始安装

更新现有包

yum update

执行docker安装脚本

curl -sSLhttps://get.docker.com/ | sh

启动docker服务

systemctl start docker

确认docker安装成功

docker run hello-world

Unable to find image'hello-world:latest' locally

latest: Pulling fromhello-world

a8219747be10: Pullcomplete 91c95931e552: Already exists

hello-world:latest: Theimage you are pulling has been verified. Important: image verification is atech preview feature and should not be relied on to provide security. Digest:sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d

Status: Downloaded newerimage for hello-world:latest

Hello from Docker.

This message shows thatyour installation appears to be working correctly.

To generate thismessage, Docker took the following steps:

1. The Docker clientcontacted the Docker daemon.

2. The Docker daemonpulled the "hello-world" image from the Docker Hub.

(Assuming it was notalready locally available.)

3. The Docker daemoncreated a new container from that image which runs the executable that producesthe output you are currently reading. 4. The Docker daemon streamed that outputto the Docker client, which sent it to your terminal.

To try something moreambitious, you can run an Ubuntu container with:

 $ docker run -it ubuntu bash

For more examples andideas, visit: http://docs.docker.com/userguide/

 

 

2.    了解docker的镜像和容器

在上一步中,运行了

docker run hello-world

执行这一个命令,你完成了使用docker的核心任务。

你运行的命令分为三个部分。

一个容器是一个剥离到基础版本的linux操作系统。一个镜像是一个你加载到一个容器中的软件。当你运行命令的时候,docker做的事情:

·检查你是否拥有hello-world软件镜像

·从docker hub中下载镜像(more about the hub later)(存疑)

·将镜像加载到容器中并运行它

依赖于它构建的方式,一个镜像可能运行一个简单单一的命令然后退出。这也正是Hello-World所做的。

当然了,一个docker镜像所能做的不止于此。一个镜像可以启动一个像数据库一样复杂的软件,等待你添加数据,存储数据以便日后使用,然后等待其他人的使用。

但是,是谁构建了hello-world这个软件的镜像呢?在本例中,Docker官方做了这件事,但是其实所有人都可以做。docker允许你人们(公司)通过docker镜像创建或者共享软件。

使用docker,你不需要担心你的机器是否能够在docker镜像中运行软件,因为一个docker容器总是能运行它。

 

3.    寻找并运行whalesay镜像

step1:定位whalesay镜像

1.打开浏览器并访问这个地址https://hub.docker.com/

2.点击搜索框,输入whalesay

3.点击搜索结果中的docker/whalesay镜像。

浏览器展示的是whalesay镜像的资源库。

每个镜像资源库都包含着镜像的信息。它应该包含了以下内容:比如这个镜像包含了哪些种类的软件以及如何使用它。你可以注意到你可以注意到这个whalesay镜像是基于ubuntu的。在下一步中,你将在自己的机器上运行它。

 

step2:运行whalesay镜像

1.      打开终端窗口

2.      执行以下命令:

$ docker rundocker/whalesay cowsay boo

Unable to find image 'docker/whalesay:latest'locally

latest: Pulling fromdocker/whalesay

e9e06b06e14c: Pullcomplete

a82efea989f9: Pullcomplete

37bea4ee0c81: Pullcomplete

07f8e8c5e660: Pullcomplete

676c4a1897e6: Pullcomplete

5b74edbcaa5b: Pullcomplete

1722f41ddcb5: Pullcomplete

99da72cfe067: Pullcomplete

5d5bd9951e26: Pullcomplete

fb434121fc77: Alreadyexists

Digest:sha256:d6ee73f978a366cf97974115abe9c4099ed59c6f75c23d03c64446bb9cd49163

Status: Downloaded newerimage for docker/whalesay:latest

 _____

< boo >

 -----

    \

     \

      \

                    ##        .

              ## ## ##       ==

           ## ## ## ##      ===

      /""""""""""""""""___/===

  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~

       \______ o          __/

        \   \        __/

          \____\______/

 

 

The first time you run asoftware image, the `docker` command looks for it

on your local system. Ifthe image isn't there, then `docker` gets it from

the hub.

3.      输入以下命令,查看docker镜像列表

$ docker images

REPOSITORY       TAG         IMAGE ID        CREATED            VIRTUAL SIZE

docker/whalesay      latest      fb434121fc77        3 hours ago        247 MB

hello-world          latest      91c95931e552        5 weeks ago        910 B

当你在容器中运行一个镜像的时候,docker下载这个镜像到你的电脑上。这个本地镜像拷贝保存了你的操作。如果镜像源发生变动,docker只会再一次下载这个镜像。你当然可以自己手动删除这个镜像。

 

4.      运行以下命令:

$ docker rundocker/whalesay cowsay boo-boo

 _________

< boo-boo >

 ---------

    \

     \

      \    

                    ##        .           

              ## ## ##       ==           

           ## ## ## ##      ===           

       /""""""""""""""""___/===       

  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~  

       \______ o          __/           

        \   \        __/            

          \____\______/  

 

 

 

4.    构建自己的镜像:

step1:写一个docker文件

首先创建mydockerbuild目录。

$ mkdir mydockerbuild

切换进入此目录

$ cd mydockerbuild

创建Dockerfile文件,并写入以下内容

FROMdocker/whalesay:latest

RUN apt-get -y update&& apt-get install -y fortunes

CMD /usr/games/fortune-a | cowsay

step2:从docker文件构建一个镜像

1.      执行以下命令:

docker build -t docker-whale.

别忽略了语句最后的·点

$ docker build -tdocker-whale .

Sending build context toDocker daemon 158.8 MB

...snip...

Removing intermediatecontainer a8e6faa88df3

Successfully built7d9495d03763

 

step3:了解构建过程

docker build -t docker-whale. 命令获取了当前目录的Dockerfile,并且在你的机器上构建了一个叫做docker-whale的镜像。这个命令会花费几分钟并且它的输出比较长和复杂。在本部分,你将了解每部分的含义。

First Docker checks tomake sure it has everything it needs to build.

 

Sending build context toDocker daemon 158.8 MB

Then, Docker loads withthe whalesay image. It already has this image locally as your might recall fromthe last page. So, Docker doesn’t need to download it.

 

Step 0 : FROMdocker/whalesay:latest

 ---> fb434121fc77

Docker moves onto thenext step which is to update the apt-get package manager. This takes a lot oflines, no need to list them all again here.

 

Step 1 : RUN apt-get -y update &&apt-get install -y fortunes

 ---> Running in 27d224dfa5b2

Ignhttp://archive.ubuntu.com trusty InRelease

Ignhttp://archive.ubuntu.com trusty-updates InRelease

Ign http://archive.ubuntu.comtrusty-security InRelease

Hithttp://archive.ubuntu.com trusty Release.gpg

....snip...

Get:15http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB]

Get:16http://archive.ubuntu.com trusty-security/universe amd64 Packages [134 kB]

Reading package lists...

---> eb06e47a01d2

Then, Docker installsthe new fortunes software.

 

Removing intermediatecontainer e2a84b5f390f

Step 2 : RUN apt-get install -y fortunes

 ---> Running in 23aa52c1897c

Reading package lists...

Building dependencytree...

Reading stateinformation...

The following extrapackages will be installed:

  fortune-mod fortunes-min librecode0

Suggested packages:

  x11-utils bsdmainutils

The following NEWpackages will be installed:

  fortune-mod fortunes fortunes-min librecode0

0 upgraded, 4 newlyinstalled, 0 to remove and 3 not upgraded.

Need to get 1961 kB ofarchives.

After this operation,4817 kB of additional disk space will be used.

Get:1http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB]

...snip......

Setting up fortunes(1:1.99.1-7) ...

Processing triggers forlibc-bin (2.19-0ubuntu6.6) ...

 ---> c81071adeeb5

Removing intermediatecontainer 23aa52c1897c

Finally, Docker finishesthe build and reports its outcome.

 

Step 3 : CMD /usr/games/fortune -a | cowsay

 ---> Running in a8e6faa88df3

 ---> 7d9495d03763

Removing intermediatecontainer a8e6faa88df3

Successfully built7d9495d03763

 

step4:运行你自己的docker-whale

1.      查看你的镜像列表

$ docker images

REPOSITORY      TAG      IMAGE ID          CREATED          VIRTUAL SIZE

docker-whale         latest       7d9495d03763      4 minutes ago       273.7 MB

docker/whalesay      latest       fb434121fc77      4 hours ago         247 MB

hello-world          latest       91c95931e552      5 weeks ago         910 B

2.      运行

$ docker rundocker-whale

 _________________________________________

/ "He was a modest,good-humored boy. It  \

\ was Oxford that madehim insufferable." /

 -----------------------------------------

          \

           \

            \    

                          ##        .           

                    ## ## ##       ==           

                 ## ## ## ##      ===           

            /""""""""""""""""___/===       

        ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~  

             \______ o          __/           

              \    \       __/            

                              \____\______/  

 

就想你看到的,你已经修改过这个镜像了。你也会注意到,这次docker不再下载内容。因为docker资源在本地构建,并且已经有权限获取它。

5.    创建一个docker hub账户和资源库

参见docker官网

6.    为你的镜像加上标签并提交你的镜像

参见docker官网
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值