In this lab, we’ll understand how to use shell environment variables. There are other ways to define variables in Docker such as defining through an environment file, a dockerfile, or a docker-compose file.
We’ll containerize an application which will display a simple website that will change colors based on the color we passed on the terminal.
Create the templates directory and the simple hello.html inside it.
$ mkdir templates
$ vim templates/hello.html
Here’s the app.py written in Python.
Next, create the dockerfile.
FROM python:3.6
RUN pip install flask
WORKDIR /opt
COPY ./ /opt
EXPOSE 8080
ENTRYPOINT ["python", "app.py"]
Our project directory should now look like this:
$ tree
├── app.py
├── dockerfile
└── templates
└── hello.html
1 directory, 3 files
Build the image from the dockerfile and give it the name “my-flask-app”. It will take a few minutes to build the image since it will pull down the Python container image from Dockerhub. It will then use the Python image as the base image. You should see the “Successful built” returned once it’s done.
$ docker build . -t my-flask-app
Successfully built d40b9ef9ada7
Successfully tagged my-flask-app:latest
Verify that the image is created. As mentioned, the Python image is pulled down to our local machine.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-flask-app latest d40b9ef9ada7 About a minute ago 913MB
python 3.6 54260638d07c 6 months ago 902MB
Before we run the container, get the IP of your machine.
$ curl ipecho.net/plain; echo
We’ll use the command below to run the container. Use the “-e” flag to define the APP_COLOR variable and specify a color. Make sure to append the image name/repository at the end.
$ docker run -p 82:8080 -e APP_COLOR=blue my-flask-app
It should return the following output:
</br>
Open a web browser and navigate to the IP address followed by the port number, like this:
54.151.154.238:82/
You should see the website displayed.
Back in your terminal, hit Ctrl-C to quit and run the container again. Specify “red” this time.
$ docker run -p 82:8080 -e APP_COLOR=red my-flask-app
The website color should now change to red.
Try running containers and use different colors. Note that the code only accepts eleven colors. If you specified a color that is not declared in the code, you’ll get the error message below:
$ docker run -p 82:8080 -e APP_COLOR=darkblue my-flask-app
No Command line argument. Color from environment variable =darkblue
Color not supported. Received 'darkblue' expected one of red,orange,yellow,green,blue,violet,indigo,pink,white,black,gray
What if we didn’t specify the environment variable?
$ docker run -p 82:8080 my-flask-app
As seen, the website will randomly choose a color from declared list of colors. Note that this catch-all is defined in the application code so that it won’t return an error if it doesn’t find any “color”.
In general, Docker will complain and return an error message if the environment variable doesn’t have a value assigned to it.
When you’re done with the lab, you can stop all running containers by running the command below.
$ docker stop $(docker ps)
Once all containers have “Exited” status, remove them.
$ docker ps -a
$ docker container prune -f
Finally, remove all images.
$ docker image prune -af