Skip to content

Latest commit

 

History

History
100 lines (66 loc) · 3.34 KB

02-docker.md

File metadata and controls

100 lines (66 loc) · 3.34 KB

Docker

Docker is the preferred approach to running the examples from this repository.

Build the image

Build the Docker image by executing the following commands:

  1. Clone this repository locally:

    git clone https://github.com/akutz/go-generics-the-hard-way
  2. Change directories to the root of the cloned repository:

    cd go-generics-the-hard-way
    
  3. Build the image:

    docker build -t go-generics-the-hard-way .

And voilá, you have the image!


Please note the image is also available remotely and can obtained by executing the following commands:

docker pull akutz/go-generics-the-hard-way && \
docker tag akutz/go-generics-the-hard-way go-generics-the-hard-way

Use the image

The image may be used one of three ways:

Self-contained

This method does not require cloning this repository locally since the Docker image includes the contents of this repository. For example, the following command will run the boxing benchmark using the examples sources from inside the image:

docker run -it --rm go-generics-the-hard-way \
  go test -tags benchmarks -run Boxing -bench Boxing -benchmem -count 5 -v ./06-benchmarks/

Mount repository

Alternatively, it is also possible to run the examples cloned on your local filesystem using the Docker image. This has two benefits:

  • The examples will be up-to-date
  • It is easier to capture file output from the examples such as profiles

For example, the following command will:

  • Run the boxing benchmark using the examples from the cloned repository
  • Produce a memory profile
  • Produce an SVG image from the memory profile
docker run -it --rm -v "$(pwd):/go-generics-the-hard-way" \
  go-generics-the-hard-way bash -c ' \
  go test -tags benchmarks -run Boxing -bench Boxing -benchmem -count 5 -memprofile memprofile.out -v ./06-benchmarks/ && \
  go tool pprof -svg memprofile.out'

At the end there will be a file in the current, local directory named profile001.svg that visualizes the memory profile produced from the benchmark.

Dev Container

Alternatively, it is also possible to run the examples through opening the cloned reposity in a VS Code devcontainer:

Note: This assumes you are using VS Code as your IDE

  1. To get started, install the extension Remote - Containers by Microsoft (ms-vscode-remote.remote-containers).
  2. Open up your Command Palette and type or select Remote-Containers: Reopen in Container command.
  • The extension should be able to pick up the settings described in the .devcontainer/devcontainer.json and build the Dockerfile
  1. Open up your terminal and you should be inside a container and at the project root.
  • A good test is to do
go version

If you see go version go1.18beta2 linux/arm64 you should be good to go.


Next: Hello world