您的位置 首页 golang

「快学Docker」Docker简介、安装和Hello World实现

往期文章

前言

docker 已经成为了一门炙手可热的技术,每个程序员(特别是后端程序员)都应该学习下Docker这门技术。

Docker是什么

来自官网的定义:Docker是以Docker容器为资源分割和调度的基本单位,封装了整个软件运行时环境,为开发者和系统管理员设计的,用于构建、发布和运行应用的平台。Docker是开源的,其基于Go语言开发。Docker通过操作系统内核技术(namespace、cgroups等内核、资源隔离技术)为容器提供资源隔离和安全保障。

Docker可以做什么

1、消除线上、线下的环境差异,保证了应用生命周期的环境一致性和标准化

2、版本化控制。可以像 Git 一样进行版本控制,版本回滚等。

3、高资源利用率和资源隔离。可以精确地为应用分配CPU、内存等资源,保证了应用间互不影响。

4、跨平台性。Docker将应用和其依赖的运行时环境打包成镜像,实现了“构建一次,到处运行”的理念。

5、等等等等~~~

Docker安装

Docker命令的执行一般都需要root权限,为了方便,所以下面的安装方式都是在root用户下进行的。非root用户需要在命令前添加 sudo

脚本安装方式(通用)

这种方式是通过下载脚本来安装的,是比较通用的一种安装方式。

 curl -fsSL  | sh
 

执行结果如下:

警告可以忽略它。一切正常的话,就可以启动我们的Docker了。

启动docker:

systemctl start docker
或者
service docker start
 

centos 系统安装Docker-ce

移除老版本

$ sudo yum remove docker \
 docker-client \
 docker-client-latest \
 docker-common \
 docker-latest \
 docker-latest-logrotate \
 docker-logrotate \
 docker-engine
 

安装docker ce

$ sudo yum install -y yum-utils \
 device-mapper-persistent-data \
 lvm2
$ sudo yum-config-manager \
 --add-repo \
 
sudo yum install docker-ce docker-ce-cli containerd.io
 

详细的安装手册,大家可以参考官方文档。官方文档做了比较详细的说明。

非Linux系统

我推荐使用Linux系统来安装docker。如果想在windows、Mac上安装docker,可以使用docker for windows或者Desktop for Mac工具类解决吧。这里就不多说了。

docker-ce,docker-ee和docker-io的区别?

较旧版本的Docker称为docker或docker-engine或docker.io。新的docker分为两个版本,社区版(ce)和企业版(ee)。

所以,尽量不要通过yum -y install docker-io的方式来安装docker,因为这样安装到的是老版本的docker了。

Hello World实现

编程界的惯例,先来个Hello World体验下docker吧

搜索hello-world镜像

 docker search hello
 

可以看到有一个官方的hello world镜像。

获取hello-world镜像

docker pull hello-world
 

输出结果:

Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest:  sha256 :41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
Status: Downloaded newer image for hello-world:latest
 

启动hello-world

docker run -it hello-world
 

输出结果:

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:
 
For more examples and ideas, visit:
 
 

看第一行,输出了Hello from Docker!。这就是docker官方为我们准备的hello world实现了,大家也可以自己去体验一下。

总结

本文简单地介绍了Docker,并且介绍了Docker的安装方法和演示了docker官方提供的hello world实现。这里用到了一些docker命令,如search,pull,run,可以先不用理会它,后面再具体分析。

Tips

如果排版混乱,可以点击下面的链接,到我的博客查看(由于申请了原创首发,博客会延迟发布)。

文章来源:智云一二三科技

文章标题:「快学Docker」Docker简介、安装和Hello World实现

文章地址:https://www.zhihuclub.com/101309.shtml

关于作者: 智云科技

热门文章

网站地图