Skip to content

Commit

Permalink
add env suppose
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjahahahaha committed Nov 15, 2021
1 parent f08cc3e commit 1099be3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
11 changes: 5 additions & 6 deletions cmd/commands.go
Expand Up @@ -12,10 +12,9 @@ import (
)

const (
initUsage = `Init container process run user's process in container.Do not call it outside.`
runUsage = `Create a container with namespace and cgroups limit: myDocker run -t [command]`
initUsage = `Init container process run user's process in container.Do not call it outside.`
runUsage = `Create a container with namespace and cgroups limit: myDocker run -t [command]`
ENV_EXEC_PID = "mydocker_pid"
ENV_EXEC_CMD = "mydocker_cmd"
)

var (
Expand All @@ -26,6 +25,7 @@ var (
Detach bool // 后台运行
Name string // 容器名称
ImageTarPath string // 镜像的tar包路径
EnvSlice []string // 环境变量
)

var initDocker = &cobra.Command{
Expand Down Expand Up @@ -54,7 +54,7 @@ var runDocker = &cobra.Command{
id := container.RandStringContainerID(10)
log.Log.Infof("Container ID [%s]", id)
// 获取交互flag值与command, 启动容器
container.Run(tty, strings.Split(args[0], " "), ResourceLimitCfg, CgroupName, Volume, Name, ImageTarPath, id)
container.Run(tty, strings.Split(args[0], " "), ResourceLimitCfg, CgroupName, Volume, Name, ImageTarPath, id, EnvSlice)
return nil
},
}
Expand Down Expand Up @@ -121,7 +121,6 @@ var stopCommand = &cobra.Command{
},
}


var removeCommand = &cobra.Command{
Use: "rm [container_id]",
Short: "remove a container",
Expand All @@ -130,4 +129,4 @@ var removeCommand = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
container.RemoveContainer(args[0])
},
}
}
1 change: 1 addition & 0 deletions cmd/init.go
Expand Up @@ -11,4 +11,5 @@ func init() {
runDocker.Flags().BoolVarP(&Detach, "detach", "d", false, "Run container in background and print container ID")
runDocker.Flags().StringVarP(&Name, "container-name", "n", "", "set a container nickname")
runDocker.Flags().StringVarP(&ImageTarPath, "image-tar-path", "i", "./busybox.tar", "used image tar file path")
runDocker.Flags().StringSliceVarP(&EnvSlice, "set-environment", "e", []string{}, "set environment")
}
21 changes: 21 additions & 0 deletions container/exec.go
Expand Up @@ -2,6 +2,7 @@ package container

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -45,6 +46,9 @@ func ExecContainer(containerID string, commandAry []string) {
return
}

// 将容器进程的环境变量都放到exec进程内
cmd.Env = append(os.Environ(), getEnvsByPid(pid)...)

if err := cmd.Run(); err != nil {
log.LogErrorFrom("ExecContainer", "Run", err)
return
Expand Down Expand Up @@ -93,6 +97,23 @@ func getContainerByID(containerID string) (*ContainerInfo, error) {
return &containerInfo, nil
}

// getEnvsByPid
// @Description: 根据Pid获取进程的环境变量
// @param pid
// @return []string
func getEnvsByPid(pid string) []string {
// 进程的环境变量存放在/proc/self/environ
path := fmt.Sprintf("/proc/%s/environ", pid)
contentBytes, err := ioutil.ReadFile(path)
if err != nil {
log.LogErrorFrom("getEnvsByPid", "ReadFile", err)
return nil
}
// 多个环境变量中的分隔符是\u0000
envs := strings.Split(string(contentBytes), "\u0000")
return envs
}

// StopContainer
// @Description: 关闭容器
// @param containerID
Expand Down
5 changes: 4 additions & 1 deletion container/process.go
Expand Up @@ -17,7 +17,7 @@ const (
// @param tty
// @return *exec.Cmd
// @return *os.File 管道写入端
func NewParentProcess(tty bool, volume, ImageTarPath, cId string) (*exec.Cmd, *os.File) {
func NewParentProcess(tty bool, volume, ImageTarPath, cId string, EnvSlice []string) (*exec.Cmd, *os.File) {
// 创建匿名管道
readPipe, writePipe, err := NewPipe()
if err != nil {
Expand Down Expand Up @@ -48,6 +48,9 @@ func NewParentProcess(tty bool, volume, ImageTarPath, cId string) (*exec.Cmd, *o
// 在这里传入管道文件读取端的句柄
// ExtraFiles指定要由新进程继承的其他打开文件。它不包括标准输入、标准输出或标准错误。
cmd.ExtraFiles = []*os.File{readPipe}
// 添加环境变量
// os.Environ()就是系统默认的配置(宿主机的环境变量),默认新启动进程都是默认继承父进程的环境变量
cmd.Env = append(os.Environ(), EnvSlice...)
return cmd, writePipe
}

Expand Down
4 changes: 2 additions & 2 deletions container/run.go
Expand Up @@ -27,9 +27,9 @@ import (
// @param cgroupName
// @param volume
// @param name
func Run(tty bool, cmdArray []string, res *subsystems.ResourceConfig, cgroupName string, volume, cName, ImageTarPath, cId string){
func Run(tty bool, cmdArray []string, res *subsystems.ResourceConfig, cgroupName string, volume, cName, ImageTarPath, cId string, EnvSlice []string){
// 获取到管道写端
parent, pipeWriter := NewParentProcess(tty, volume, ImageTarPath, cId)
parent, pipeWriter := NewParentProcess(tty, volume, ImageTarPath, cId, EnvSlice)
if parent == nil {
log.LogErrorFrom("Run", "NewParentProcess", fmt.Errorf(" parent process is nil"))
return
Expand Down
1 change: 0 additions & 1 deletion container/workspace.go
Expand Up @@ -156,7 +156,6 @@ func CreateMountPoint(rootURL, imageName, mntURL, cId string) {
writerPath := filepath.Join(rootURL, "diff", cId + "_writeLayer")
imageDir := filepath.Join(rootURL, "diff", imageName)
dirs := "dirs=" + writerPath + ":" + imageDir
fmt.Println(dirs)
cmd := exec.Command("mount", "-t", "aufs", "-o", dirs, "mnt_" + cId[:4], mntURL)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
Binary file removed mydocker
Binary file not shown.

0 comments on commit 1099be3

Please sign in to comment.