github.com/googlecloudplatform/kubernetes-workshops@v0.0.0-20180501174420-d8199445b2c3/bundles/kubernetes-101/workshop/labs/containerizing-your-application.md (about)

     1  # Containerizing your application
     2  
     3  In this lab you will build your application, run it locally, and then package it into a container. Containers are more lightweight than a full virtual machine for your app and they package your application so that it runs the same across different environments such as development, QA, and production.
     4  
     5  ## Get the application code
     6  
     7  The first hurdle to is the application itself.  How do you write it?  How do you deploy it?
     8  
     9  ## Build the app
    10  
    11  Set the GOPATH variable, so that we can build our application.
    12  ```bash
    13  export GOPATH=~/go
    14  ```
    15  
    16  Build the app as a static binary.
    17  ```bash
    18  cd app/monolith
    19  # main.go contains the app's entry point
    20  go build -tags netgo -ldflags "-extldflags '-lm -lstdc++ -static'" .
    21  ```
    22  
    23  ## Test the app's functionality.
    24  ```bash
    25  # Start up the application in the background.  Feel free to use different ports, if necessary.
    26  ./monolith --http :10180 --health :10181 &
    27  
    28  # Test out the app's default functionality.
    29  curl http://127.0.0.1:10180
    30  
    31  # Attempt to access the app's secure endpoint.
    32  curl http://127.0.0.1:10180/secure
    33  
    34  # Get a JWT token from our application.  We'll use this to access the app's secure endpoint.
    35  # The password for the next step is 'password'.
    36  TOKEN=$(curl http://127.0.0.1:10180/login -u user | jq -r '.token')
    37  
    38  # Examine the token, if you'd like.
    39  echo $TOKEN
    40  
    41  # Pass the token along the secure endpoint to get the message.
    42  curl -H "Authorization: Bearer $TOKEN" http://127.0.0.1:10180/secure
    43  ```
    44  
    45  ## Package the app
    46  Once we have a working binary, we can use Docker to package it. 
    47  ```bash
    48  # Check out the Dockerfile.  What is it doing?
    49  cat Dockerfile
    50  
    51  # Use the Dockerfile to build a new monolith image.
    52  docker build -t askcarter/monolith:1.0.0 .
    53  ```
    54  
    55  After building the image, use Docker to verfiy that it still functions the same.
    56  ```bash
    57  # Start an instance of your newly created docker image.
    58  docker run -d askcarter/monolith:1.0.0
    59  
    60  # Use docker ps to get the container's ID
    61  docker ps
    62  
    63  # Use docker inspect to get find out the IP Address of your running container image.
    64  docker inspect <container-id>
    65  
    66  # Use the IP Address to test the instance's functionality.
    67  curl http://<docker-ip>
    68  ```
    69  
    70  ## Clean up
    71  After verifying everything works as expected, clean up your environment.
    72  ```bash
    73  # Stop the running docker container.
    74  docker stop <container-id>
    75  
    76  # Remove the docker container from the system.
    77  docker rm <container-id>
    78  
    79  # Remove the docker image from the system.
    80  docker rmi askcarter/monolith:1.0.0
    81  ```
    82  
    83  ## [Optional] Push the Image to Docker Hub
    84  If you've set up a Docker Hub account, you can optionally push the image to the Docker Hub. 
    85  ```bash
    86  # Associate the docker command line tool with your Docker Hub account.
    87  docker login
    88  
    89  # Upload the image into your Docker repository.
    90  docker push <your_repo>/monolith:1.0.0
    91  ```