Docker 安装
官方地址: https://docs.docker.com/install/
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表示一次性使用,即用即删