diff options
| -rw-r--r-- | .gitattributes | 1 | ||||
| -rw-r--r-- | Dockerfile | 15 | ||||
| -rw-r--r-- | README.md | 51 | ||||
| -rw-r--r-- | app/main.go | 28 | ||||
| -rw-r--r-- | codecrafters.yml | 11 | ||||
| -rw-r--r-- | go.mod | 11 | ||||
| -rw-r--r-- | go.sum | 0 | ||||
| -rwxr-xr-x | your_docker.sh | 11 |
8 files changed, 128 insertions, 0 deletions
diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..576f3b4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:1.22-alpine + +RUN apk add --no-cache 'curl>=7.66' + +# Download docker-explorer +ARG docker_explorer_version=v18 +RUN curl -Lo /usr/local/bin/docker-explorer https://github.com/codecrafters-io/docker-explorer/releases/download/${docker_explorer_version}/${docker_explorer_version}_linux_amd64 +RUN chmod +x /usr/local/bin/docker-explorer + +COPY . /app +WORKDIR /app + +RUN sed -i -e 's/\r$//' /app/your_docker.sh + +ENTRYPOINT ["/app/your_docker.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..7e543ba --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +[](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for Go solutions to the +["Build Your Own Docker" Challenge](https://codecrafters.io/challenges/docker). + +In this challenge, you'll build a program that can pull an image from +[Docker Hub](https://hub.docker.com/) and execute commands in it. Along the way, +we'll learn about [chroot](https://en.wikipedia.org/wiki/Chroot), +[kernel namespaces](https://en.wikipedia.org/wiki/Linux_namespaces), the +[docker registry API](https://docs.docker.com/registry/spec/api/) and much more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your Docker implementation is `app/main.go`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` + +That's all! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +You'll use linux-specific syscalls in this challenge. so we'll run your code +_inside_ a Docker container. + +Please ensure you have [Docker installed](https://docs.docker.com/get-docker/) +locally. + +Next, add a [shell alias](https://shapeshed.com/unix-alias/): + +```sh +alias mydocker='docker build -t mydocker . && docker run --cap-add="SYS_ADMIN" mydocker' +``` + +(The `--cap-add="SYS_ADMIN"` flag is required to create +[PID Namespaces](https://man7.org/linux/man-pages/man7/pid_namespaces.7.html)) + +You can now execute your program like this: + +```sh +mydocker run alpine:latest /usr/local/bin/docker-explorer echo hey +``` diff --git a/app/main.go b/app/main.go new file mode 100644 index 0000000..d1c4195 --- /dev/null +++ b/app/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + // Uncomment this block to pass the first stage! + // "os" + // "os/exec" +) + +// Usage: your_docker.sh run <image> <command> <arg1> <arg2> ... +func main() { + // You can use print statements as follows for debugging, they'll be visible when running tests. + fmt.Println("Logs from your program will appear here!") + + // Uncomment this block to pass the first stage! + // + // command := os.Args[3] + // args := os.Args[4:len(os.Args)] + // + // cmd := exec.Command(command, args...) + // output, err := cmd.Output() + // if err != nil { + // fmt.Printf("Err: %v", err) + // os.Exit(1) + // } + // + // fmt.Println(string(output)) +} diff --git a/codecrafters.yml b/codecrafters.yml new file mode 100644 index 0000000..77886cf --- /dev/null +++ b/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Go version used to run your code +# on Codecrafters. +# +# Available versions: go-1.22 +language_pack: go-1.22 @@ -0,0 +1,11 @@ +// DON'T EDIT THIS! +// +// Codecrafters relies on this file being intact to run tests successfully. Any changes +// here will not reflect when CodeCrafters tests your code, and might even cause build +// failures. +// +// DON'T EDIT THIS! + +module github.com/codecrafters-io/docker-starter-go + +go 1.22 diff --git a/your_docker.sh b/your_docker.sh new file mode 100755 index 0000000..439ca82 --- /dev/null +++ b/your_docker.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# DON'T EDIT THIS! +# +# CodeCrafters uses this file to test your code. Don't make any changes here! +# +# DON'T EDIT THIS! +set -e +tmpFile=$(mktemp) +go build -o "$tmpFile" app/*.go +exec "$tmpFile" "$@" |