一、 前言:容器化技术的核心价值
容器技术(如 Docker)已成为现代应用开发和部署的标准方案,它能提供:
✅ 环境一致性:彻底解决"在我机器上能跑"的问题
✅ 快速部署:秒级启动应用实例
✅ 资源隔离:安全稳定的运行环境
✅ 便捷扩展:轻松实现水平扩容
本文将以最简单的 Hello World 程序为例,体验天翼云容器镜像服务 :
编写基础应用
容器化打包
镜像服务使用全流程
二、编写基础应用-C语言HelloWorld实现
2.1 编写经典程序
```c
/* hello.c */
#include <stdio.h>
int main() {
printf("Hello World from C Container!\n");
return 0;
}
```
2.2 本地编译测试
```bash
gcc hello.c -o hello && ./hello -static
```
预期输出:
```
Hello World from C Container!
```
三、容器镜像构建
3.1 单阶段Dockerfile
```dockerfile
## FROM gcc:latest
## 天翼云公有云上面ctyunos 的容器基础镜像
FROM registry-huadong1.crs.ctyun.cn/open-source/alpine:3.21
COPY hello.c .
RUN gcc hello.c -o hello
CMD ["./hello"]
```
3.2 构建与运行
```bash
# 构建镜像
docker build -t c-hello .
# 运行容器
docker run --rm c-hello
```
3.3 跨平台构建
```bash
docker buildx build --platform linux/amd64,linux/arm64 -t c-hello-multi .
```
四、容器镜像上传
3.1 镜像文件查看
1、nerdctl images 或者 docker images
REPOSITORY TAG IMAGE ID CREATED PLATFORM SIZE BLOB SIZE
hello v2.3.4 f4d27c427cfc 13 days ago linux/amd64 172.3 MiB 171.5 MiB
hello1 v2.3.4 5e1397d47bd8 13 days ago linux/amd64 188.8 MiB 185.9 MiB
2、推送镜像
docker tag f4d27c427cfc registry-xinan1.crs-internal.ctyun.cn/msgc_test/moontest:v2.3.4
docker push registry-xinan1.crs-internal.ctyun.cn/msgc_test/moontest:v2.3.4