Creating or pushing Docker images

Docker is certainly a tool that has attracted a lot of hype. The main reason for this, beyond human fashion, is that Docker simplifies things such as dependency management and model integration, allowing reproducible, widely deployable builds. We can define the things we need from an OS up front and parcel them all up at a point in time where we know the dependencies are fresh so that all our tweaking and troubleshooting will not be in vain.

We will need two things to create our image and get it to where we want it to go:

  • Dockerfile: This defines our image, the version of Linux, the commands to run, and the default command to run when the container is launched
  • Makefile: This creates the image and pushes it to AWS ECS

Let's first look at the Dockerfile:

FROM ubuntu:16.04

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends
curl
git
pkg-config
rsync
awscli
wget
&&
apt-get clean &&
rm -rf /var/lib/apt/lists/*

RUN wget -nv https://storage.googleapis.com/golang/go1.12.1.linux-amd64.tar.gz &&
tar -C /usr/local -xzf go1.12.1.linux-amd64.tar.gz

ENV GOPATH /home/ubuntu/go

ENV GOROOT /usr/local/go

ENV PATH $PATH:$GOROOT/bin

RUN /usr/local/go/bin/go version &&
echo $GOPATH &&
echo $GOROOT

RUN git clone https://github.com/PacktPublishing/Hands-On-Deep-Learning-with-Go

RUN go get -v gorgonia.org/gorgonia &&
go get -v gorgonia.org/tensor &&
go get -v gorgonia.org/dawson &&
go get -v github.com/gogo/protobuf/gogoproto &&
go get -v github.com/golang/protobuf/proto &&
go get -v github.com/google/flatbuffers/go &&
go get -v .

WORKDIR /

ADD staging/ /app

WORKDIR /app

CMD ["/bin/sh", "model_wrapper.sh"]

We can discern the general approach just by looking at the capitalized declarations at the start of each line:

  1. Pick the base OS image with FROM.
  2. Set boot with ARG.
  3. Run a bunch of commands with RUN to get our Docker image into the desired state. Then ADD a directory of the staging data, mounted to /app.
  4. Change to a new WORKDIR.
  5. Execute the CMD command and our container will run.

We now need a Makefile. This file contains the commands that will build the images we just defined in our Dockerfile and push them to Amazon's container-hosting service, ECS.

This is our Makefile:

cpu-image:
mkdir -p staging/
cp model_wrapper.sh staging/
docker build --no-cache -t "ACCOUNTID.dkr.ecr.ap-southeast-2.amazonaws.com/$(MODEL_CONTAINER):$(VERSION_TAG)" .
rm -rf staging/

cpu-push: cpu-image
docker push "ACCOUNTID.dkr.ecr.ap-southeast-2.amazonaws.com/$(MODEL_CONTAINER):$(VERSION_TAG)"

As with the other examples that we have already covered, we are using the sp-southeast-2 region; however, feel free to specify your own. You will also need to include your own 12-digit AWS account ID.

From this directory (when the time comes, not just yet!) we can now create and push Docker images.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset