在阿里云ECS云服务器上部署和使用开源的应用程序容器引擎Docker

发布时间:2024-01-08 18:02:31

Docker 是一个开源的应用程序容器引擎,具有可移植性、可扩展性、高安全性和可管理性等优势。它允许开发人员将应用程序和依赖项打包到可移植容器中,从而在 Linux 机器上高效构建、部署和管理应用程序。阿里云提供Docker镜像仓库,用于快速部署Docker。

先决条件

请确保您已创建具有以下配置的ECS实例。如果您尚未创建实例,请参考:阿里云国际服务器创建

  • 操作系统:CentOS 7.x 64位、CentOS 8.x 64位、Alibaba Cloud Linux 3 64位、Alibaba Cloud Linux 2 64位
  • 网络类型:VPC
  • IP 地址:公网?IP 地址
  • 安全组:80、22、8080端口允许的入方向流量。

部署 Docker

1. 连接ECS实例。

2. 安装 Docker。

本示例使用镜像版本Alibaba Cloud Linux 3。

a) 执行以下命令,添加Docker社区版(Docker-CE)的DNF仓库。

sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-
ce/linux/centos/docker-ce.repo

b) 执行以下命令,安装Alibaba Cloud Linux 3专用的DNF仓库插件。

sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus

c) 执行以下命令安装Docker。

sudo dnf -y install docker-ce --nobest

如果执行命令后返回类似如下的错误信息,请推荐 /etc/yum.repos.d 目录下的 CentOS 软件库,然后重新安装 Docker-CE。

1

3. 执行以下命令,查看Docker是否安装成功。

sudo docker -v

如下图所示,表示已安装Docker。

2

4. 运行以下命令以启动 Docker,并将 Docker 配置为在系统启动时运行:

sudo systemctl start docker
sudo systemctl enable docker

5. 执行以下命令,查看Docker是否启动。

sudo systemctl status docker

类似于以下命令输出,表示Docker已启动。

3

Docker 基本用法

本节仅介绍 Docker 的基本用法。

  • 管理 Docker 守护程序
sudo systemctl start docker # Run the Docker daemon.
sudo systemctl stop docker # Stop the Docker daemon.
sudo systemctl restart docker # Restart the Docker daemon.
sudo systemctl enable docker # Configure Docker to start on system startup.
sudo systemctl status docker # Check the status of Docker
  • 管理映像

本文将以阿里云容器镜像服务中的Apache镜像为例,演示如何使用Docker管理镜像。

o 拉取图像。

sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5

o 修改标签。阿里云容器镜像服务中的镜像名称很长。使用标签使图像易于识别。

sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1

o 查看现有图像。

sudo docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
  • 管理容器
  • 若要查询要在以下代码片段中使用的值,请运行该命令。<Image ID>docker images

o 启动一个新容器。

sudo docker run -it <Image ID> /bin/bash

o 在后台启动一个新容器,并指定容器的名称。

sudo docker run -d --name <Container name> <Image ID>

o 查询容器 ID。

sudo docker ps

o 从容器创建映像。

sudo docker commit <Container ID or container name> <Repository name >:< Tag>

使用Docker创建镜像

本节介绍如何从 Dockerfile 创建简单的自定义 NGINX 映像。

1. 执行以下命令,拉取镜像。 本示例中,从阿里云容器镜像服务中拉取Apache镜像。

sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5

2.修改图像的标签,使图像更易于识别。

sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1

3. 运行以下命令以创建和编辑 Dockerfile:

a) 运行以下命令以创建和编辑 Dockerfile:

vim Dockerfile

b) 按键进入插入模式,将以下内容添加到文件中:i

#Declare a base image. 
FROM aliweb:v1
#Declare the owner of the base image. 
MAINTAINER DTSTACK
#Specify the commands that you want to run before the container starts. You must append the commands to the end of the RUN command. A Dockerfile can contain up to 127 lines. If the total length of your commands exceeds 127 lines, we recommend that you write the commands to a script. 
RUN mkdir /dtstact
#Specify the commands that are run on system startup. The last command must be a frontend command that runs constantly. Otherwise, the container exits after all commands are run. 
ENTRYPOINT ping www.aliyun.com

c) 按 键,回车 ,然后按 键保存并关闭 Dockerfile 文件。Esc:wqEnter

4. 运行以下命令,基于基础 NGINX 映像创建映像。按以下格式创建命令:.docker build -t <Image name>:<Image version>.

命令末尾的句点 (.) 表示 Dockerfile 的路径。您必须添加句点 (.)。

例如,创建名为aliweb、版本为v2的镜像,执行以下命令。

sudo docker build -t aliweb:v2 .

5. 执行以下命令,查看新镜像是否构建成功。

sudo docker images

A command output similar to the one displayed in the following figure indicates that the image is created.

4

Install and Use docker-compose

docker-compose 是 Docker 团队提供的开源容器编排工具。它允许您定义和运行多个 Docker 容器。通过使用 YAML 文件,您可以配置应用程序所需的所有服务。使用 docker-compose 命令解析 YAML 文件配置后,您可以创建并启动所有指定的 Docker 服务。Compose具有降低运维成本、提高部署效率等优势。

注意:只有 Python 3 及以上版本支持 docker-compose。确保您已安装 pip。

安装 docker-compose

1. 运行以下命令以安装 setuptools。

pip3 install -U pip setuptools

2. 运行以下命令以安装 docker-compose:

pip3 install docker-compose

3. 执行以下命令,查看docker-compose是否安装成功。

docker-compose --version

如果返回docker-compose版本,则表示安装docker-compose。

使用 docker-compose 部署应用程序

本节介绍如何使用docker-compose部署WordPress。

1. 创建和编辑 docker-compose.yaml 文件。

a) 运行以下命令以创建docker-compose.yaml文件。

vim docker-compose.yaml

b) 按键进入插入模式,将以下内容添加到文件中。i

在此示例中,添加了用于安装 WordPress 的内容。

version: '3.1'               # Specify the version of Docker Compose.
services:
wordpress:               # Specify a service name.
image: wordpress       # Specify an image name.
restart: always        # Configure the container to start each time Docker starts.
ports:
- 80:80              # Specify port mappings.
environment:           # Configure environment variables.
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: 123456
WORDPRESS_DB_NAME: wordpress
volumes:               # Configure mappings between containers and ECS volumes.
- wordpress:/var/www/html
db:                      # Specify a service name.
image: mysql:5.7       # Specify an image name.
restart: always        # Configure the container to start each time Docker starts.
ports:
- 3306:3306         #  Specify port mappings.
environment:           # Configure environment variables.
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: 123456
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:               # Configure mappings between containers and ECS volumes.
- db:/var/lib/mysql
volumes:
wordpress:
db:

c) 按 键退出插入模式,输入然后按 Enter 键保存并关闭文件。Esc:wq

2. 运行以下命令以启动应用程序:

sudo env "PATH=$PATH" docker-compose up -d

3. 在浏览器地址栏中输入格式的地址,进入WordPress配置页面。您可以根据界面提示配置参数,访问WordPress。https://<Public IP address of the ECS instance>

文章来源:https://blog.csdn.net/jiuhebaobao/article/details/135460080
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。