just wanted to give a try to docker so i gave a try J
somehow i thought that docker is a really magical tool and you just point it to your executable and it gathers all dependencies and pops the image wrapped in a pink pandelka.
well - not so J
but it really is not too difficult to make a docker image - there are just a few not too difficult steps.
my first impulse of course was to snap an ubuntu image and set up everything there manually:
luckily i noticed somewhere that there is a handy golang docker image already so i grabbed it and tenderly started it
now, you can of course can still setup the golang image to your needs manually, but it is so much clever to have docker do this automatically for you!
for this purpose we will use a Dockerfile and build our image from it.
this will actually enable us to start as many instances of our app simultaneously in dedicated docker images with a wave of the hand.
so what do we need to put in this file?
first we need to tell docker which base image we will use; in our case this is the golang image:
but for this example i won't grab it from github, but from my local drive; and i want docker to copy everything from the current folder to a specified location:
now, i recently was bothered a bit for mainenance, so i started using glide (https://github.com/Masterminds/glide/) which seems a nice way for dependencies management, until golang team comes up with a native solution - if ever.
so we need to grab and install glide, and from then on we will build throuh this tool
if we want to do (or rather RUN) something in a different folder we can get away with something like
which of course changes the current folder temporarily
next we'll use glide to check what is in its glide.yaml file and download and setup dependent libraries:
finally we must tell docker what to start when the image is loaded.
remember that the value of docker is its container-ness (aahem). i.e. it is meant to run a service in a container.
of course you can run as many services in a container as you like, but really for decoupling one container=one service is probably preferable
so...
the better way is to override the entry point when starting the docker image:
ah, lets not forget the build step prior to running, which is...ta-daaa!:
so this is all about this short intro to docker.
as always i'm writing this to document my own efforts, so i don't have to go searching too far for these steps :)
anyway, hope it be helpful for somebody else too - next stop: Heroku!
chaooo
somehow i thought that docker is a really magical tool and you just point it to your executable and it gathers all dependencies and pops the image wrapped in a pink pandelka.
well - not so J
but it really is not too difficult to make a docker image - there are just a few not too difficult steps.
my first impulse of course was to snap an ubuntu image and set up everything there manually:
and then install go, setup paths blah blah
docker run ubuntu:14.04 -t -i /bin/bash
luckily i noticed somewhere that there is a handy golang docker image already so i grabbed it and tenderly started it
not many thing around, but enough to get us started
docker run -t -i golang /bin/bash
now, you can of course can still setup the golang image to your needs manually, but it is so much clever to have docker do this automatically for you!
for this purpose we will use a Dockerfile and build our image from it.
this will actually enable us to start as many instances of our app simultaneously in dedicated docker images with a wave of the hand.
so what do we need to put in this file?
first we need to tell docker which base image we will use; in our case this is the golang image:
i will deploy the app i think i mentioned in another article, which is hosted at https://github.com/esdee-git/deligo.
FROM golang
but for this example i won't grab it from github, but from my local drive; and i want docker to copy everything from the current folder to a specified location:
the golang image hierarchy root is at /go, so this is where i place my stuff in my image too
ADD . /go/src/deligo
now, i recently was bothered a bit for mainenance, so i started using glide (https://github.com/Masterminds/glide/) which seems a nice way for dependencies management, until golang team comes up with a native solution - if ever.
so we need to grab and install glide, and from then on we will build throuh this tool
actually this of course is not needed, if we want to only export the executable 'deligo' - for which the ADD command would've been enough, but since this is an exercise in docker lets have as many even redundant steps as bearable.
RUN go get github.com/Masterminds/glide
RUN go build github.com/Masterminds/glide
if we want to do (or rather RUN) something in a different folder we can get away with something like
but the nicer way to do it is with
RUN cd src/deligo && [some command]
WORKDIR src/deligo
which of course changes the current folder temporarily
next we'll use glide to check what is in its glide.yaml file and download and setup dependent libraries:
i didn't bother to setup the proper enviroment variable for this test, so i'm adding it to the command line; glide needs this so go tools are aware that we're using the new (for go 1.5) vendor approach to dependencies
RUN GO15VENDOREXPERIMENT=1 glide install
RUN GO15VENDOREXPERIMENT=1 go build
finally we must tell docker what to start when the image is loaded.
remember that the value of docker is its container-ness (aahem). i.e. it is meant to run a service in a container.
of course you can run as many services in a container as you like, but really for decoupling one container=one service is probably preferable
so...
if you want to check how your image was built, you can comment the above and instead
ENTRYPOINT ["./deligo"]
which will allow you to look around and test the new environment as much as you like, but this will require to rebuild the image in order to enable the true entry point after testing
ENTRYPOINT /bin/bash
the better way is to override the entry point when starting the docker image:
so after you're done exploring justrun
docker run -t -i --entrypoint /bin/bash deligo
which will start the image with the assigned entry point
docker run -t -i deligo
ah, lets not forget the build step prior to running, which is...ta-daaa!:
"--rm" will auto-remove some temporary containers build during construction of our image
docker build --rm -t deligo .
so this is all about this short intro to docker.
as always i'm writing this to document my own efforts, so i don't have to go searching too far for these steps :)
anyway, hope it be helpful for somebody else too - next stop: Heroku!
chaooo