Coder Social home page Coder Social logo

go-container-samples's Introduction

go-container-samples Go Reference

go-csv-samples is an open-source implementation of a basic container build from scratch in Go solely for learning purpose. It uses namespaces and cgroups, mount a tmpfs that's isolated from host filesystem.

What it does

Note: Before starting, you will have to extract the content of ubuntu-fs.tar.gz file into ./os-root-fs/ubuntu which will be mounted as the container's root directory.

Note: You will have to be inside a Linux box (Ubuntu in my case) to try this.

The example 5-isolate-cgroup-sample is the full implementation, and it needs root privilege for creating cgroup:

Usage

sudo su
go run main.go run /bin/bash

It will:

  • Fork itself with CLONE_NEWUTS, CLONE_NEWPID, CLONE_NEWNS flags with isolated hostname, processes and mounts
  • The forked process will create cgroup to limit memory usage of itself and any child process it creates
  • Mount ./ubuntu directory as root filesystem using chroot to limit access to host machine's filesystem
  • Mount /newtemp directory as tmpfs. Any change made to this directory will not be visible from host.
  • Mount proc (where CLONE_NEWPID namespace was already set) so that container can run ps and see only the processes running inside it.
  • Execute the supplied argument /bin/bash inside the isolated environment

Sources of the inspiration and information


Bonus tip: Setting up VS Code for cross-platform development

I have used Windows to develop the container from scratch and have run it inside Ubuntu in a virtualbox by sharing the development directory. While this setup is fine for running the code inside Linux, the development experience is not great because a lot of pieces of this application is Linux specific. For example, calls like syscall.Sethostname or the Cloneflags field in the syscall.SysProcAttr{} struct is not available in intellisense in VSCode when the dev environment is not Linux. VSCode will mark those lines as errors, because they are platform specific and declared in the standard library in Go for Linux only.

Fortunately, there is a workaround, and it is very simple. Search for "go.toolsEnvVars" in VSCode settings, copy it to User Settings and change it to:

"go.toolsEnvVars": {
    "GOOS": "linux"
}

Now restart VSCode and after that it will recognize all Linux specific declarations and will not see them as errors. Go-to-definition will work properly too.

PS: the contents of ubuntu_fs.tar.gz file has been extracted from Ubuntu docker image by using docker export... command.

go-container-samples's People

Contributors

teocci avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.