Docker with Cloud SDK & Environment Variables
Hello again. I am sharing a couple things I learned when working with Docker recently. Main points are how to pass in environment variables, setup Google Cloud SDK in Docker and turn on debugging.
Since I haven’t written about Docker before, I’ve provided a brief overview of Docker to give context and a grounding on my pointers. I recommend doing more digging to learn more elsewhere especially considering there are many other resources out there like this Docker overview.
What is Docker
It’s a way to build and deploy software and applications in an isolated environment. You can package the code to build and run software and all its dependencies in a container that can in effect run on any computing environment or server that has the capacity. It basically creates a computing bubble to build and run your code.
There are key concepts to be aware of
Dockerfile
The Dockerfile file holds the instructions on how to create a container that can run on the Docker platform. It packages up applications and the instructions drive the server configuration that it runs.
Docker Image
The Dockerfile is the base that you use to build a Docker image off of. The Docker image is the immutable set of instructions that you can then run to create a Docker container where your software will ultimately run and deploy.
Dockerfile => Docker image => Docker Container => Isolated code run
Use this command to build a Docker image.
docker build -t [REGISTRY &/OR IMAGE NAME] .
You can create an IMAGE locally or post it to a registry. If locally just give it a name and if to a registry then include the registry path and the name for the image. For example if I built it locally then I would replace IMAGE with something like my-docker-image but if I posted to a registry then I would replace it with something like gcr.io/myprojectid/my-docker-image
Use a ‘.’ to say use the current directory to build the image. You can change ‘.’ to a path if the files live elsewhere. Your Dockerfile needs to be in that directory as well as any files that it will load into the container when it runs.
Docker Registry
A Docker registry is where you can store Docker images especially if you want to share them publicly. There are several registries you can use to store images and they can be public or private. A couple examples include Docker Hub and Google Container Registry (gcr.io).
Use this command to push the image up to a registry.
docker push [REGISTRY & IMAGE NAME]
Common guidance I’ve found when starting out with Docker is to reuse images that have already been built to avoid recreating the wheel so to speak and there are a lot to choose from in different public registries.
Docker Container
This is the final destination where you use the image to launch a container and run your code. Note, it has to run on a server that has Docker platform installed.
Use this command to run a Docker image and create a container.
docker run [REGISTRY &/OR IMAGE NAME]
Add Environment Variables
When I was creating a container, I wanted to pass in my environment variables to increase security and give some flexibility to those variables. This steps through how to do that.
Add a line like this for each variable to your Dockerfile.
ENV [ENV VAR CONTAINER NAME] [ENV VAR NAME PASSED IN]
The ENV VAR CONTAINER NAME is the environment variable name that will be used in the Docker container and ENV VAR NAME PASSED IN is what you are passing in and assigning to that variable.
Example of a specific variable name.
ENV PROJECTID $PROJECT_ID
Pass the environment variable into the Docker image when you run it like the following.
docker run -p 8080:8080 -e PROJECT_ID=[PROJECT ID] [IMAGE]
Where PROJECT ID is the actual value or a local bash environment variable and IMAGE is the Docker image name and/or the registry path and name you gave it when you built it.
Install Google Cloud SDK
Add the following instruction to your Dockerfile to install Google Cloud SDK .
RUN curl -sSL https://sdk.cloud.google.com | bash
I found this one liner as a Stack Overflow answer. It will ensure that Google Cloud SDK is installed when you build a Docker image and run a Docker container.
For more information on SDK images, especially ones that already exist, check out Installing the Cloud SDK Docker image.
Docker Debug
The biggest challenge I had working with Docker containers was getting insights into what was wrong when the code failed and didn’t work like I expected.
Apply the -D when running Docker to turn on the debug mode.
docker -D run [REGISTRY &/OR IMAGE NAME]
Using debug enabled logging results in my terminal and allowed me to get messaging that helped me pinpoint issues in my container. So definitely use this when you are just starting out working with a container.
Wrap Up
The main goal of this post was to share a couple things I learned from working with Docker containers. The main points covered how to import environment variables, how to install Google Cloud SDK and how to debug. To help provide context for these pointers, I gave an overview of the what is Docker.
It definitely made my life easier to use images to spin up and run the code on different servers as needed. There are images for so many types of setups that can help you get your server up and running quickly. If you don’t want to spend too much time configuring a server, it can be worth it to find an Docker image to use or create one.