-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d707040
commit 11616b4
Showing
25 changed files
with
562 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
java_docker.jar | ||
target/* | ||
target/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM maven:3.9.4-eclipse-temurin-8-alpine | ||
|
||
RUN apk add curl | ||
|
||
# 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 | ||
|
||
ADD . /app | ||
WORKDIR /app | ||
|
||
RUN sed -i -e 's/\r$//' /app/your_docker.sh | ||
|
||
ENTRYPOINT ["/app/your_docker.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/docker.png) | ||
|
||
This is a starting point for Java 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 `src/main/java/Main.java`. | ||
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 ubuntu:latest /usr/local/bin/docker-explorer echo hey | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 Java version used to run your code | ||
# on Codecrafters. | ||
# | ||
# Available versions: java-1.8 | ||
language_pack: java-1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.codecrafters</groupId> | ||
<artifactId>build-your-own-bittorrent</artifactId> | ||
<version>1.0</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.dampcake</groupId> | ||
<artifactId>bencode</artifactId> | ||
<version>1.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.java.dev.jna</groupId> | ||
<artifactId>jna-platform</artifactId> | ||
<version>5.12.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.30</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.10.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<configuration> | ||
<finalName>java_docker</finalName> <!-- Please do not change this final artifact name--> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
<appendAssemblyId>false</appendAssemblyId> | ||
<archive> | ||
<manifest> | ||
<addClasspath>true</addClasspath> | ||
<!-- This is the main class of your program which will be executed--> | ||
<mainClass>Main</mainClass> | ||
</manifest> | ||
</archive> | ||
<outputDirectory>${dir}</outputDirectory> | ||
</configuration> | ||
|
||
<executions> | ||
<execution> | ||
<id>make-assembly</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import com.sun.jna.Library; | ||
import com.sun.jna.Native; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.Arrays; | ||
|
||
// your_docker.sh run <image> <command> <arg1> <arg2> ... | ||
// run ubuntu:latest /usr/local/bin/docker-explorer exit 1 | ||
// run ubuntu:latest /usr/local/bin/docker-explorer ls /some_dir | ||
// run ubuntu:latest echo hey | ||
public class Main { | ||
|
||
public interface LibC extends Library { | ||
// GO: <CLONE_NEWPID = 0x20000000 // New pid namespace> | ||
int CLONE_NEWPID_NAMESPACE_FLAG = 0x20000000; | ||
|
||
int chroot(String path); // Define the JNA interface for libc, which includes the chroot function | ||
|
||
int unshare(int flags); | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
// You can use print statements as follows for debugging, they'll be visible when running tests. | ||
System.out.println("Logs from your program will appear here!"); | ||
|
||
// Uncomment this block to pass the first stage! | ||
// System.out.println(Arrays.toString(args)); | ||
// String imageName = args[1]; | ||
// String command = args[2]; | ||
// String[] commandWithArgs = Arrays.copyOfRange(args, 2, args.length); | ||
// try { | ||
// ProcessBuilder processBuilder = new ProcessBuilder(commandWithArgs); | ||
// processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); | ||
// processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); | ||
// | ||
// Process process = processBuilder.start(); | ||
// | ||
// int exitCode = process.waitFor(); | ||
// | ||
// if (exitCode != 0) { | ||
// System.err.printf("Err: %d\n", exitCode); | ||
// System.exit(1); | ||
// } | ||
// } catch (IOException | InterruptedException e) { | ||
// System.err.printf("Err: %s\n", e.getMessage()); | ||
// System.exit(1); | ||
// } | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/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 | ||
|
||
ls -alh | ||
|
||
mvn -B --quiet package -Ddir=/tmp/codecrafters-docker-target | ||
exec java -jar /tmp/codecrafters-docker-target/java_docker.jar "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM maven:3.9.4-eclipse-temurin-11-alpine | ||
FROM maven:3.9.4-eclipse-temurin-8-alpine | ||
|
||
RUN apk add curl | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
java_docker.jar | ||
target/* | ||
target/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM maven:3.9.4-eclipse-temurin-8-alpine | ||
|
||
RUN apk add curl | ||
|
||
# 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 | ||
|
||
ADD . /app | ||
WORKDIR /app | ||
|
||
RUN sed -i -e 's/\r$//' /app/your_docker.sh | ||
|
||
ENTRYPOINT ["/app/your_docker.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/docker.png) | ||
|
||
This is a starting point for Java 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 `src/main/java/Main.java`. | ||
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 ubuntu:latest /usr/local/bin/docker-explorer echo hey | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 Java version used to run your code | ||
# on Codecrafters. | ||
# | ||
# Available versions: java-1.8 | ||
language_pack: java-1.8 |
Oops, something went wrong.