I chose to call the environment variable github.įor this example, I also needed to allow port 3000 to be able to access the guest The special sauce of this simple solution is passing an environment variable into the Docker command line interface, which provides the URL where the Docker image is going to pull down the GitHub repo. Just remember that you need to add anything required to compile and resolve dependencies to your Docker Image.
#DOCKER RUN IMAGE EXAMPLE CODE#
If you are new to Docker, and want to build your own image, you can copy my code from above, and save it to a file called Dockerfile, then you can run the command below which will create your own local version of my Docker image called dockertest.įor this tutorial, I’ll use my image, but if you’re already a savvy Docker user feel free to use your own. RUN echo "make dockertest\n" > /startscript.sh RUN echo "git clone \$github\n" > /startscript.sh RUN echo "mkdir github\n" > /startscript.sh RUN echo "#!/bin/bash\n" > /startscript.sh #You can start with any base Docker Image that works for you This is what one of my Dockerfiles looks like: The special sauce is a few commands that I put in the Dockerfile which launches the Makefile from the GitHub repository. I add the required language(s) such as Go or Node. I typically start with a base Ubuntu image. Add a Dockerfileįor the Dockerfile I try to keep this as lean as possible. If it works in your environment, and you added all the dependencies to the Makefile, it should work for the Docker Image. You can also test your code and build locally, before you commit it into GitHub by running: Makefiles also use a special syntax called YAML. Makefile(s) are sensitive, and require tabs, so if your Makefile does not work, just double check and make sure you’re not using spaces. Just be aware that the Docker image is launching a rule called dockertest, that has three rules Setup, Build, and Run.
#DOCKER RUN IMAGE EXAMPLE HOW TO#
If you want to learn more about makefiles, it might be worth looking at this tutorial on how to use a Makefile Makefile Tutorial on Youtube. If you are not familiar with Makefiles, Makefiles are what developers use to help other developers compile and run their projects. #This runs setup, build, and launches the application PATH := go get go get go get go get go build go install.
The Makefile will pull down any required packages or dependencies, then it will clone the GitHub repo, build, and launch the application. This is an example of the Makefile I use, the Makefile is very simple, you can certainly do
Let’s learn more about each: The Makefile
To perform this you only need a Makefile and a Docker image. Pull –The Docker image automatically clones the GitHub repository.Launch Docker Image – Launches Docker with an environment variable to a GitHub repository.Here’s a diagram, in case visual learning is more your style: This example is very simple, but you’ll get the idea of how it works. (The -e switch indicates the URL for a GitHub repository.) The example I provided uses a GitHub repo that builds a web server written with Node.js. You launched the Docker image with an environment variable indicating the Git repo to clone the source code from. Then, connect to You should see a web page displaying Hello World. Docker run -p 3000:3000 -e github = '' -it oktaadmin/dockertest