1. docker 安装
请参考:[https://www.jianshu.com/p/665…]
2. 制作 docker 镜像
2.1 准备测试程序 docker_test.cpp
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char** args)
{FILE* pfile = fopen("docker_test.txt", "wb");
if(NULL == pfile)
{return 0;}
char buf[50] = "hello, welcome to learn docker";
while(1)
{printf(buf);
fwrite(buf, 1, strlen(buf), pfile);
sleep(1);
}
return 0;
}
2.2 编写 docker 文件
FROM centos
RUN yum install -y gcc gcc-c++ make patch sudo
RUN mkdir /usr/src/docker_test
COPY docker_test.cpp /usr/src/docker_test
WORKDIR /usr/src/docker_test
RUN g++ -o test docker_test.cpp
CMD ["./test"]
2.3 编译
$sudo docker build -t docker-test:v1 .
2.4 运行 docker 镜像
$sudo docker images
REPOSITORY TAG IMAGE ID SIZE
docker-test v1 ba22adea3409 399MB
$sudo docker run -d docker-test:v1
##2.5 进入 / 退出 docker
$ sudo docker ps
CONTAINER ID IMAGE COMMAND a13f3b550314 docker-test:v1 "./test"
$ sudo docker exec -it f7fff670a3a4 /bin/bash
$ cd /usr/src/docker_test/
$ ls
docker_test.cpp docker_test.txt test
$exit