前言
云服务器Elastic Compute Service(ECS)是阿里云提供的一种基础云计算服务。使用云服务器ECS就像使用水、电、煤气等资源一样便捷、高效。
您无需提前采购硬件设备,而是根据业务需要,随时创建所需数量的云服务器ECS实例。
在使用过程中,随着业务的扩展,您可以随时扩容磁盘、增加带宽。如果不再需要云服务器,也能随时释放资源,节省费用。
关于阿里云的购买我这里不过多讲解,阿里社区有很多博主都发了的;
最近不是云原生挺火的吗?我这里给大家介绍一下如何在ECS上搭建Docker环境!
一、安装Docker CE
Docker有两个分支版本:Docker CE和Docker EE,即社区版和企业版
本教程基于CentOS 7安装Docker CE
1. 安装Docker的依赖库
[root@liza ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
2. 添加Docker CE的软件源信息
[root@liza ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
3. 安装Docker CE
[root@liza ~]# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:00
(2/2): docker-ce-stable/x86_64/primary_db | 52 kB 00:00:00
Metadata Cache Created
[root@liza ~]# yum -y install docker-ce
4. 启动Docker服务
[root@liza ~]# systemctl start docker
二、配置阿里云镜像仓库(镜像加速)
Docker的默认官方远程仓库是hub.docker.com,由于网络原因,下载一个Docker官方镜像可能会需要很长的时间,甚至下载失败
为此,阿里云容器镜像服务ACR提供了官方的镜像站点,从而加速官方镜像的下载
下面介绍如何使用阿里云镜像仓库
1. 登录容器镜像服务控制台
登录成功页面如下。(若弹出开通服务窗口,关闭即可)
2. 单击【镜像中心】 > 【镜像加速器】,可以看到阿里云为您提供了一个专属的镜像加速地址
3. 配置Docker的自定义镜像仓库地址
[root@liza ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://ktfoq759.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://ktfoq759.mirror.aliyuncs.com"]
}
4. 重新加载服务配置文件
[root@liza ~]# systemctl daemon-reload
5. 重启Docker服务
[root@liza ~]# systemctl restart docker
三、使用Docker安装Nginx服务
1. 查看Docker镜像仓库中Nginx的可用版本
[root@liza ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14291 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1941 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 801 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 139
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 132
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 109 [OK]
bitnami/nginx Bitnami nginx Docker Image 93 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 86 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 82 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 65 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 46
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 44 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 28
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 16 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK]
flashspys/nginx-static Super Lightweight Nginx Image 8 [OK]
mailu/nginx Mailu nginx frontend 8 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 7 [OK]
bitwarden/nginx The Bitwarden nginx web server acting as a r… 7
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK]
wodby/nginx Generic nginx 1 [OK]
2. 拉取最新版的Nginx镜像
[root@liza ~]# docker pull nginx:latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
3. 查看本地镜像
[root@liza ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 44 hours ago 133MB
4. 运行容器
[root@liza ~]# docker run --name nginx-test -p 8080:80 -d nginx
47097e5b37b62b2ae2cd47a87df20ace66b62116c54d86e5da9e8add5800efba
命令参数说明:
- --name nginx-test:容器名称。
- -p 8080:80: 端口进行映射,将本地8080端口映射到容器内部的80端口。
- -d nginx: 设置容器在后台一直运行。
5. 访问Nginx服务
在浏览器地址栏输入 http://(ECS公网地址):8080
,然后就可以访问Nginx服务