Docker 实现 HelloWorld

Docker 安装

官方地址: https://docs.docker.com/install/
About Docker CE

ubuntu

安装依赖工具

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

导入官方公钥, sudo apt-key add - 中的 - 表示接收标准输入

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

加入安装源

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

正式安装

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

创建 Docker 镜像

创建空基础镜像

tar -c --files-from /dev/null | docker import - null 管道的输入为空, 生成空镜像 null, 标签默认为 latest

查看镜像

docker images 或者 docker image ls

准备 HelloWorld 程序

HelloWorld.c

#include <stdio.h>
#include <stdlib.h>
int main(){
	printf("HelloWorld\n");
	return EXIT_SUCCESS;
}

静态编译 gcc HelloWorld.c -static -o HelloWorld

Dockerfile

FROM null
ADD ./HelloWorld /HelloWorld
CMD ["/HelloWorld"]

FROM 继承空基础镜像, ADD 向镜像根目录添加可执行文件, CMD 启动时首个运行的命令

目录结构

创建 HelloWorld 镜像

docker build --tag helloworld:0.1 ./ 在Dockerfile所在目录编译创建镜像, 镜像名称使用小写, 指定标签为0.1

查看镜像列表

运行镜像

docker run --rm helloworld:0.1 --rm表示一次性使用,即用即删

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值