构建Melis系统开发的Docker镜像
我们没有必要从头开始构建一个docker镜像完善开发系统,而是在一个相对完善的docker镜像的基础上加以扩展,达到我们的目的。
从doker hub获取原始镜像
执行
sudo docker pull gcc
下载后查看当前系统的docker镜像列表:
进入docker:
首先我们看一下系统当前是没有活动的docker容器的:
此时,我们创建一个,并且将主机的home目录映射为容器的/data/目录
sudo docker run -it -v ~/:/data gcc /bin/bash
start up command also can be:
sudo docker run -it -v ~/:/data $docker_image_id /bin/bash
a record entry installed after each running session
the CONTAINER ID and NAMES item are dynamic allocated each running session.
below two commands are used to manange the running session.
sudo docker stop 94ad6ab47180
sudo docker start 94ad6ab47180
if you wnat to inteactive with the docker,you can add -i option after above command
sudo docker start 94ad6ab4718 -i
同一个内核:
在docker中获取到的内核信息和在HOST主机中获取到的内核信息相同,说明docker用的是主机的内核。
构建melis系统:
我们一步一步来,首先我们把系统放在HOST共享目录中,通过容器去编译,
a deep usage about docker please refer:
基于Docker环境的YOLCAT语意分割模型实验环境搭建_papaofdoudou的博客-CSDN博客_yolcat
other options on docker:
sudo docker exec 866a9a9a3221 ps -ef
above command just get the pid from the view of docker inside,but how to get the realy pid from the host? you can get use below commands:
sudo docker container top 866a9a9a3221
we can view the heriarchy of process by command;
pstree -p -H 308334 #308334 is a.out PID
sudo docker top serene_wu
see the log from the view of host
docker exec -d serene_wu sleep 2000
sudo docker logs serene_wu|more
docker related process
related is dockerd, containerd, containerd-shim
namespace of containerd-shim(305281)─┬─bash(305302)───a.out(308334)
containerd is in the same namespace with hosts init, and a.out in docker is in the same name space with the docker bash.
how to get the current namespace of process?
how to create a namespace in helloworld?
#define _GNU_SOURCE
#include <fcntl.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define STACK_SIZE (1024 * 1024)
int idle(void *args)
{
printf("I'm child process, and my pid is: %d\n", getpid());
for (;;) {
sleep(1);
}
return 0;
}
pid_t clone_wrapper(int (*func)(void *), int flag, void *args)
{
char *stack, *stack_top;
stack = (char *)malloc(STACK_SIZE);
if (stack == NULL) {
printf("alloc stack for child failed!\n");
return -1;
}
stack_top = stack + STACK_SIZE; /* Assume stack grows downward */
return clone(func, stack_top, flag , args);
}
char *get_pid_ns(int pid)
{
char bytes[32];
sprintf(bytes, "/proc/%d/ns/pid", pid);
return strdup(bytes);
}
int main(void)
{
pid_t childs[2];
char *ns_file;
int fd;
printf("I'm parent, and my pid is: %d\n", getpid());
childs[0] = clone_wrapper(idle, CLONE_NEWPID, NULL);
if (childs[0] == -1) {
printf("error: create child thread failed!\n");
return -1;
}
printf("first child's pid is: %d\n", childs[0]);
ns_file = get_pid_ns(childs[0]);
printf("%s line %d, nsfile %s.\n", __func__, __LINE__, ns_file);
if (!ns_file) {
printf("get child pid ns failed!\n");
return -1;
}
fd = open(ns_file, O_RDONLY);
if (fd == -1) {
printf("open child pid ns failed!\n");
return -1;
}
if (setns(fd, 0) == -1) {
printf("set ns failed!\n");
return -1;
}
printf("I'm parent, and my pid is: %d\n", getpid());
childs[1] = clone_wrapper(idle, 0, NULL);
if (childs[1] == -1) {
printf("error: create child thread failed!\n");
return -1;
}
printf("second child's pid is: %d\n", childs[1]);
sleep(3);
kill(childs[0], SIGTERM);
kill(childs[1], SIGTERM);
waitpid(childs[0], NULL, 0);
waitpid(childs[1], NULL, 0);
return 0;
}
reference
理解Docker容器的进程管理 - ilinux_one - 博客园
结束!
相关文章
- 【Docker】解决docker通过volumes挂载文件不生效,修改后容器内数据不同步,需要重启容器才能同步的问题
- Docker - 构建镜像:使用docker build命令和Dockerfile文件
- 运维基础之Docker(5)docker部署airflow
- Docker - docker in docker(dind)
- Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
- 80.第十七章 企业级容器技术docker -- Docker 数据管理(八)
- 76.第十七章 企业级容器技术docker -- Docker 镜像制作和管理(四)
- y24.第二章 Docker从入门到精通 -- docker镜像制作和管理(六)
- 【云原生之Docker实战】使用docker部署nginx-proxy-manager-zh反向代理工具
- 【云原生之Docker实战】使用docker部署mm-wiki文档系统
- 【云原生之Docker实战】在docker环境下部署DooTask任务管理工具
- 【云原生之Docker实战】使用docker部署nightingale运维监控平台
- 【云原生之Docker实战】使用docker部署Wiznote私人笔记系统
- Docker容器原理及相关知识
- 1-使用华为云的ubuntu系统安装docker Linux安装docker安装 国内安装有时候很慢 最好翻墙安装
- Docker数据卷—Volumes
- Docker安装(安装docker)
- Docker学习笔记20:docker使用之资源汇总
- Docker学习笔记08:docker使用之镜像使用
- Docker学习笔记06:docker使用之Hello World
- s7.基于二进制包一键安装docker脚本
- 推送docker镜像至Docker Hub
- docker 使用 GPU 安装Nvidia Docker