Docker is the most popular container technology nowadays and many developers run it locally on their computers and laptops. Docker can run natively on Ubuntu and some other Linux distributions, but not on Mac OS X yet. In this post we will learn how to run a simple Go RESTful service in Docker on Mac. In our example we will piggy back on a simple RESTful service developed in an earlier posts How To Create A Simple RESTful Service In Go.
Docker installation on Mac OS X
If you do not have Docker (and Docker Machine) installed, please follow official Mac OS X installation instruction.
Creating Docker image
First we need to create a Docker image with our RESTful service.
mkdir restful-go cd restful-go touch Dockerfile
Docker image is built from Dockerfile. Edit the Dockerfile with your favourite editor so that it has the following content:
FROM golang:latest RUN go get github.com/upitau/goinbigdata/examples/hellorest CMD /go/bin/hellorest EXPOSE 8080
This simple Dockerfile tells Docker to:
- Pull latest Golang image from Docker Hub
- Install github.com/upitau/goinbigdata/examples/hellorest go package with "Hello World" RESTful service
- Run the RESTful service
- Expose port 8080 used by the RESTful service
Detailed description of Dockerfile commands can be found in official reference.
Now we are ready to build a Docker image:
restful-go> docker build -t restful-hello .
This tells Docker to build image with name restful-hello from Dockerfile in current directory (denoted by dot).
Sending build context to Docker daemon 2.048 kB Step 1 : FROM golang:latest ---> bf9e64d14fb2 Step 2 : RUN go get github.com/upitau/goinbigdata/examples/hellorest ---> Running in e8f0d707afee ---> 564ff81a2c21 Removing intermediate container e8f0d707afee Step 3 : CMD /go/bin/hellorest ---> Running in 41a6b941e7c5 ---> dd3a894026df Removing intermediate container 41a6b941e7c5 Step 4 : EXPOSE 8080 ---> Running in 9f5af3905479 ---> de4e96d82b57 Removing intermediate container 9f5af3905479 Successfully built de4e96d82b57
We can now see our restful-hello Docker image among others:
REPOSITORY TAG IMAGE ID CREATED SIZE restful-hello latest de4e96d82b57 40 seconds ago 751.6 MB golang latest bf9e64d14fb2 6 days ago 744 MB nginx latest 69203b7cd029 12 days ago 134.6 MB
Running Go RESTful service in Docker
When image is created, running Docker container is as simple as:
docker run -d -p 8080:8080 restful-hello
This command tells Docker to run a new container based on image restful-hello. Param -d tells Docker to run container as daemon. Param -p maps port 8080 of Docker host to the same port on Docker container. This port mapping is necessary. When we will try to connect to RESTful service later, we will connect to Docker host which, in its turn, will relay our request to Docker container. The output is an id of created container.
Let's check that our Docker container is up and running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10efaf236e68 restful-hello "/bin/sh -c /go/bin/h" 20 seconds ago Up 19 seconds 0.0.0.0:8080->8080/tcp sharp_hodgkin
Our RESTful service is up and running, and ready to serve.
Calling restful-hello service
As we noted earlier, to call our service we need to call Docker host. On Linux, Docker host is the same as localhost, but on Mac it is a virtual machine running in VirtualBox. Therefore, the first thing we need to do is to get ip address of Docker host.
Now, when we know the ip, we can finally call our service:
Running RESTful service in Docker on Mac OS X requires a bit more effort than running on Ubuntu, but is still quite easy. First, we need to create a Docker image with our service. And then create and run a Docker container based on the image.