Class Two. 基于ECS快速搭建Docker环境

Class Two. 基于ECS快速搭建Docker环境

一. Docker简介

Docker在我看来是一种对硬件虚拟机的简化,容器只包含需要用到的组件便可运行。

1. Docker的用途

  • 统一测试环境和生产环境: 测试环境所用的容器打包后即可发布到生产环境,生产环境不需要过多的配置,节省运维成本。
  • 弹性服务: dokcer容器可以动态扩容,随开随关,非常适合当下微服务框架的场景。
  • 将业务的多个服务分拆为多个容器,确保安全性、可扩展性、冗余性。

2. Docker的优势

老三样,Docker相比传统虚拟机的的优势还是有很多的。

  • 启动快
  • 资源占用少,节省硬件资源
  • 体积小,方便发布

二. Docker安装

1. 安装yum工具包

阿里云实验环境中,没有安装yum工具包,导致了第二步的yum-config-manager命令找不到

yum -y install yum-utils

2. 添加docker使用的yum源

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

3. 安装Docker

实验环境中给出的是yum install -y docker-ce,经测试添加-ce后缀后找不到软件包

yum makecache
yum -y install docker

4. 启动Docker服务

systemctl start docker

5. Docker服务开机自启

systemctl enable docker 

6. 查看Docker服务状态

systemctl status docker 
### 执行后显示内容如下
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-01-21 17:29:02 CST; 8s ago
     Docs: http://docs.docker.com
 Main PID: 2009 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─2009 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native...           └─2014 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m ...
Jan 21 17:29:01 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:01.940569659+08:00" level=warning msg="Docker could not enable ...system"Jan 21 17:29:01 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:01.968042681+08:00" level=info msg="Graph migration to content-...econds"Jan 21 17:29:01 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:01.968624098+08:00" level=info msg="Loading containers: start."
Jan 21 17:29:02 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:02.035640807+08:00" level=info msg="Firewalld running: false"
Jan 21 17:29:02 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:02.114031515+08:00" level=info msg="Default bridge (docker0) is...ddress"Jan 21 17:29:02 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:02.153205905+08:00" level=info msg="Loading containers: done."
Jan 21 17:29:02 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:02.188235730+08:00" level=info msg="Daemon has completed initialization"
Jan 21 17:29:02 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:02.188275552+08:00" level=info msg="Docker daemon" commit="0be3...=1.13.1Jan 21 17:29:02 iZuf62x4rqf1vhbte76rdxZ systemd[1]: Started Docker Application Container Engine.
Jan 21 17:29:02 iZuf62x4rqf1vhbte76rdxZ dockerd-current[2009]: time="2021-01-21T17:29:02.195687697+08:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.

三. 配置Docker镜像加速器

Docker的官方镜像仓库位于美国,由于距离遥远,经常会遇到官方镜像仓库在国内的一些网络问题。

所以国内的一些公司(阿里、腾讯、网易等)开发了一些镜像加速器。

镜像加速器的原理大概是:国内公司使用优质的跨国链路每天定时的从官方仓库下载最新的docker镜像到国内服务器上,国内用户使用镜像加速器时访问国内的镜像服务器,即可加快镜像的拉取速度

1. 阿里云控制台找到镜像加速器页面

阿里云容器镜像服务链接

image-20210121173807041

2. 按照操作文档配置加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://*******.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

3. 验证是否配置成功

执行后,显示自己的加速器链接则表示成功

docker info -f '{{.RegistryConfig.Mirrors}}'

四. 验证Docker是否正常

1. 运行hello-world镜像

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
0e03bdcc26d7: Pull complete 
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for docker.io/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.
    (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://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

五. 常用Docker命令

1. docker search 搜索镜像

docker search nginx

2. docker pull 拉取镜像

docker pull nginx

3. docker images 查看镜像

docker images

4. docker ps 查看容器状态

  • -a参数: 显示所有容器
  • -n : 显示最新创建的容器
docker ps -a 
### 查看所有容器状态

5. docker restart 重启容器

docker restart 容器ID或name

6. docker rm 删除容器

docker rm 容器ID或name

7. docker rmi 删除镜像

docker rmi 镜像ID或name

8. docker run 创建运行容器

docker run 命令的参数有很多,不一一例举

  • –name nginx-test:容器名称。
  • -p 8080:80: 端口进行映射,将本地8080端口映射到容器内部的80端口。
  • -d nginx: 设置容器在后台一直运行。
  • –restart always :开启容器自启动(错误后也会自重启)
  • –rm: 运行完后自动删除
  • -d: 开启后台守护进程运行
docker run --name nginx-test -p 8080:80 --restart always -d nginx 
###运行后可以使用以下命令验证,出现Welcome字样表示运行成功
curl -s 127.0.0.1:8080 |grep Welcome

阿里云高校计划

阿里云高校计划,陪伴两千多所高校在校生云上实践、云上成长。
在这里你可以领用免费的cpu资源,还可以参加免费训练营,实践提高:
https://developer.aliyun.com/adc/student/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

帅浩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值