2019-10-11 20:21:47 +02:00
# Dockerfile for EMSDK
2020-09-02 19:50:52 +02:00
This Dockerfile builds a self-contained version of Emscripten SDK that enables Emscripten to be used without any
2019-10-11 20:21:47 +02:00
other installation on the host system.
2020-04-06 17:58:16 +02:00
It is published at https://hub.docker.com/u/emscripten/emsdk
2019-10-11 20:21:47 +02:00
### Usage
Simple usage of this container to compile a hello-world
```bash
# create helloworld.cpp
cat << EOF > helloworld.cpp
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
EOF
```
```bash
# compile with docker image
docker run \
--rm \
2020-09-02 19:50:52 +02:00
-v $(pwd):/src \
2019-10-11 20:21:47 +02:00
-u $(id -u):$(id -g) \
2020-04-06 17:58:16 +02:00
emscripten/emsdk \
2019-10-11 20:21:47 +02:00
emcc helloworld.cpp -o helloworld.js
# execute on host machine
node helloworld.js
```
Teardown of compilation command:
|part|description|
|---|---|
|`docker run` | A standard command to run a command in a container|
|`--rm` |remove a container after execution (optimization)|
|`-v $(pwd):$(pwd)` |Mounting current folder from the host system into mirrored path on the container<br>TIP: This helps to investigate possible problem as we preserve exactly the same paths like in host. In such case modern editors (like Sublime, Atom, VS Code) let us to CTRL+Click on a problematic file |
|`-u $(id -u):$(id -g)` | Run the container as a non-root user with the same UID and GID as local user. Hence all files produced by this are accessible to non-root users|
2020-04-06 17:58:16 +02:00
|`emscripten/emsdk` |Get the latest tag of this container|
2019-10-11 20:21:47 +02:00
|`emcc helloworld.cpp -o helloworld.js` |Execute `emcc` command with following arguments inside container, effectively compile our source code|
### Building Dockerfile
2021-03-28 17:25:23 +02:00
This image has following optional arguments
2019-10-11 20:21:47 +02:00
2021-03-28 17:25:23 +02:00
| arg | default value | description |
| --- | --- | --- |
| `EMSCRIPTEN_VERSION` | `tot` <br/>(special case, tip-of-tree) | One of released version of Emscripten. For example `2.0.0` <br/> Minimal supported version is **1.39.0 ** |
2019-10-11 20:21:47 +02:00
**Building**
This step will build Dockerfile as given tag on local machine
```bash
# using docker
docker build \
2020-06-04 22:02:17 +02:00
--network host \
2020-09-02 19:50:52 +02:00
--build-arg=EMSCRIPTEN_VERSION=1.39.17 \
-t emscripten/emsdk:1.39.17 \
-f docker/Dockerfile \
2019-10-11 20:21:47 +02:00
.
```
```bash
# using predefined make target
2020-09-02 19:50:52 +02:00
make version=1.39.17 build test
2019-10-11 20:21:47 +02:00
```
**Tagging**
In case of using `docker build` command directly, given `--tag` should match version of released Emscripten (you can see list of non-legacy versions by executing `emsdk list` ).
**Pushing**
This step will take local image and push to default docker registry. You need to make sure that you logged in docker cli (`docker login` ) and you have rights to push to that registry.
```bash
# using docker
2020-09-02 19:50:52 +02:00
docker push emscripten/emsdk:1.39.17
2019-10-11 20:21:47 +02:00
```
```bash
# using predefined make target
2020-09-02 19:50:52 +02:00
make version=1.39.17 push
2019-10-11 20:21:47 +02:00
```
2020-09-02 19:50:52 +02:00
In case of pushing the most recent version, this version should be also tagged as `latest` and pushed.
2019-10-11 20:21:47 +02:00
```bash
# using docker cli
2020-09-02 19:50:52 +02:00
docker tag emscripten/emsdk:1.39.17 emscripten/emsdk:latest
2020-04-06 17:58:16 +02:00
docker push emscripten/emsdk:latest
2019-10-11 20:21:47 +02:00
```bash
2020-09-02 19:50:52 +02:00
# using make
make version=1.39.17 alias=latest push
2019-10-11 20:21:47 +02:00
```
### Extending
If your project uses packages that this image doesn't provide you might want to:
* Contribute to this repo: Maybe your dependency is either non-intrusive or could be useful for other people
* Create custom image that bases on this image
1. create own Dockerfile that holds:
```dockerfile
# Point at any base image that you find suitable to extend.
2020-09-02 19:50:52 +02:00
FROM emscripten/emsdk:1.39.17
2019-10-11 20:21:47 +02:00
# Install required tools that are useful for your project i.e. ninja-build
RUN apt update && apt install -y ninja-build
```
2020-09-02 19:50:52 +02:00
2019-10-11 20:21:47 +02:00
2. build it
2020-09-02 19:50:52 +02:00
```bash
2019-10-11 20:21:47 +02:00
docker build -t extended_emscripten .
```
3. test
2020-09-02 19:50:52 +02:00
```bash
2019-10-11 20:21:47 +02:00
docker run --rm extended_emscripten ninja --version
2020-09-02 19:50:52 +02:00
# 1.10.0
2019-10-11 20:21:47 +02:00
```