Build a small container runtime that can start a process and isolate its filesystem and process view. CodeCrafters lists this track as deprecated; the exercises remain available as a historical workbook.

Set up your project

Choose a starter for your language. C · Go · Nim · PHP · Python · Ruby · Rust · Swift.

Use the examples in each stage to check your implementation locally. CodeCrafters operates its hosted runner separately.

Contents

Core project

1. Execute a program

In this stage, you’ll implement a very basic version of docker run.

Original stage je9

2. Wireup stdout & stderr

You’ll now pipe the program’s stdout and stderr to the parent process.

Tests

The tester will run your program like this:

./your_program.sh run alpine:latest /usr/local/bin/docker-explorer echo hey

For now, don’t worry about pulling the alpine:latest image. We will just execute a local program for this stage and print its output. You’ll work on pulling images from Docker Hub in stage 6.

docker-explorer is a custom test program that exposes commands like echo and ls.

To test this behaviour locally, you could use the echo + echo_stderr commands that docker-explorer exposes. Run docker-explorer --help to view usage.

If you’ve got any logs or print statements in your code, make sure to remove them. The tester can’t differentiate between debug logs and the actual output!

Note: The README in your repository contains setup information for this stage and beyond (takes < 5 min).

Original stage kf3

3. Handle exit codes

In this stage, you’ll need to relay the program’s exit code to the parent process.

If the program you’re executing exits with exit code 1, your program should exit with exit code 1 too.

To test this behaviour locally, you could use the exit command that docker-explorer exposes. Run docker-explorer --help to view usage.

Just like the previous stage, the tester will run your program like this:

mydocker run alpine:latest /usr/local/bin/docker-explorer exit 1

Original stage cn8

4. Filesystem isolation

In the previous stage, we executed a program that existed locally on our machine. This program had write access to the whole filesystem, which means that it could do dangerous things!

In this stage, you’ll use chroot to ensure that the program you execute doesn’t have access to any files on the host machine. Create an empty temporary directory and chroot into it when executing the command. You’ll need to copy the binary being executed too.

For Rust

At the time of writing this, the implementation of chroot in Rust’s standard library (std::os::unix::fs::chroot) is still a nightly-only experimental API. We’ve included libc as a dependency instead.

For Nim

Since Nim’s posix module doesn’t have chroot defined, you’ll need to implement this yourself! For examples on how to do this, view the source for other syscalls like chdir.

For Go

When executing your program within the chroot directory, you might run into an error that says open /dev/null: no such file or directory. This is because Cmd.Run() and its siblings expect /dev/null to be present. You can work around this by either creating an empty /dev/null file inside the chroot directory, or by ensuring that Cmd.Stdout, Cmd.Stderr and Cmd.Stdin are not nil. More details about this here.

For Rust

When executing your program within the chroot directory, you might run into an error that says no such file or directory even if the binary exists within the chroot. This is because Command::output() expects /dev/null to be present. You can work around this by creating an empty /dev/null file inside the chroot directory. This cryptic error effects Go programs too, more details here.

Just like the previous stage, the tester will run your program like this:

mydocker run alpine:latest /usr/local/bin/docker-explorer ls /some_dir

Original stage if6

5. Process isolation

In the previous stage, we guarded against malicious activity by restricting an executable’s access to the filesystem.

There’s another resource that needs to be guarded: the process tree. The process you’re executing is currently capable of viewing all other processes running on the host system, and sending signals to them.

In this stage, you’ll use PID namespaces to ensure that the program you execute has its own isolated process tree. The process being executed must see itself as PID 1.

For PHP

You’ll need to use the pcntl_unshare function for this, which was added in PHP 7.4, and isn’t properly documented yet (as of 22 Jan 2021). Here’s the pull request where it was added.

Just like the previous stage, the tester will run your program like this:

mydocker run alpine:latest /usr/local/bin/docker-explorer mypid

Original stage lu7

6. Fetch an image from the Docker Registry

Your docker implementation can now execute a program with a fair degree of isolation – it can’t modify files or interact with processes running on the host.

In this stage, you’ll use the Docker registry API to fetch the contents of a public image on Docker Hub and then execute a command within it.

You’ll need to:

The base URL for Docker Hub’s public registry is registry.hub.docker.com.

The tester will run your program like this:

mydocker run alpine:latest /bin/echo hey

The image used will be an official image from Docker Hub. For example: alpine:latest, alpine:latest, busybox:latest. When interacting with the Registry API, you’ll need to prepend library/ to the image names.

For Rust

Since Rust doesn’t have an archive extraction utility in its stdlib, you might want to shell out and use tar.

You can use the reqwest crate to make HTTP requests, we’ve included it in the Cargo.toml file. We’ve also included serde_json to help with parsing JSON.

For Go

Since Go doesn’t have an archive extraction utility in its stdlib, you might want to shell out and use tar.

For Nim

Since Nim doesn’t have an archive extraction utility in its stdlib, you might want to shell out and use tar.

For C

Since C doesn’t have an archive extraction utility in its stdlib, you might want to shell out and use tar.

You can assume that libcurl is available in the build environment.

Original stage hs1

Back to contents

Source and licence

Stage material by CodeCrafters. Source snapshot · Original licence.

MIT License

Copyright (c) 2022 CodeCrafters

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.