Getting started with Kubernetes Part 1 - Containers & Docker
This is the first in a series of tutorials accompanying our video demonstrations on getting started with Kubernetes. In this first tutorial, we focus on one of the fundamental concepts behind Kubernetes: the container. We use the popular containerization platform Docker to create and deploy a simple containerized “Hello World” application, before moving on to look at a slightly more complex application example. We finish by pushing the container images we have created to the Scaleway Container Registry. Future videos and tutorials in this series will show you how to then deploy your containerized application with Kubernetes, specifically with the Scaleway Kubernetes Kapsule.
You may need certain IAM permissions to carry out some actions described on this page. This means:
Before starting the practical steps of this tutorial, we review a few key concepts that must be understood first:
Container: A portable package of software that includes its own environment and dependencies (code, runtime, configuration, system libraries) so that it can run on any host system.
Docker: An open source platform to package applications into containers. As well as building containerized application images, Docker also lets you run them and much more. Alternatives to Docker include Podman and LXC, though Docker is the market leader and the one we’ll be using in this tutorial.
Kubernetes: An open source container orchestration platform, designed to automate the deployment, scaling, and management of containerized applications.
While these concepts can seem quite abstract and overwhelming, in reality if you do everything in order it is not difficult or scary. The general order of events is:
Build your application
Containerize it with Docker
Use Kubernetes to orchestrate the deployment of your containerized app at the appropriate scale.
There are many advantages to building and deploying applications in this way. Here are just a few advantages of this type of deployment, with containers and Kubernetes:
- Portability: Containerized applications can be run on any cloud, platform or machine.
- Efficiency: Containerization uses fewer resources than Virtual Machines.
- Agility: With containerization, development, and delivery is faster, more agile and more flexible.
- Scalability: Kubernetes is ideal for quickly scaling your application deployment up or down adding and removing containers depending on the need.
- Reliability: Kubernetes keeps containerized workloads up and running at all times And encompassing all of the above:
- Cloud Native: Building apps for Kubernetes mean embracing Cloud-native design: the building and running of scalable applications in modern, dynamic Cloud environments.
To learn more about containers, Docker and Kubernetes, check out the useful links at the end of this tutorial.
The following steps show how to install Docker on an Ubuntu Linux operating system, from the command line. For instructions on installations for macOS or Windows, see the official Docker documentation.
Remove any previous versions of Docker that might be installed:sudo apt-get remove docker docker-engine docker.io
Update the APT package cache:sudo apt-get update
Install some required packages for Docker. Docker uses HTTPS for its repository, so we make sure we have everything needed to download Docker with a secure connection:sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
Add Docker’s GPG key, used to authenticate the Docker content and updates we download to our machine:curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the reference to Docker’s remote repository. This tells our local machine the remote storage location from which it should get and install Docker and any future updates:sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Update the APT package cache again:sudo apt-get update
Install the Docker engine itself:sudo apt-get install docker-ce
Check that Docker is installed:docker -v
If Docker is successfully installed, you should see an output confirming your version number, for example:Docker version 20.10.12, build e91ed57
We show two examples of making containerized applications with Docker and take you through the following process for each:
- Creating the Dockerfile
- Building an image from the Dockerfile
- Using the image to run the containerized application
We start by creating a simple, one-line Python application that prints “Hello World” to the command line, then show how to use Docker to containerize this application.
Create a folder called
myfirstappand navigate into it:mkdir myfirstapp && cd myfirstapp
Create a python file called
Write the following line of code inside the file, then save and exit:print("Hello World")
Create the Dockerfile. This is a text file that gives instructions for building the Docker image for the application. Create it in the same directory as
myapp.pywith the following commandnano Dockerfile
Copy and paste the following code into the Dockerfile, then save and exit:FROM python:3COPY myapp.py /CMD ["python", "./myapp.py"]
- FROM: this instruction specifies the base image for the container. In our case, we take care of our python requirement.
- COPY: this instruction adds files from our local machine (
myapp.py) to the container image.
- CMD: this instruction contains the command to be executed by default when the container is launched. In this case, we tell it to use python to run the
Run the following command to build a Docker image for the application, using the Dockerfile just created:sudo docker build -t mytestapp .
-t mytestapp argument tells Docker to tag the image with the name **mytestapp. The
. at the end of the command tells Docker to use the current directory as build context.
- Run the following command to run the image. This tells Docker to instantiate and execute the image, launching a container instance of our application:
sudo docker run mytestapp
As the image instantiates and runs, you should see
Hello World printed to your command line. Thanks to Docker, the application runs from within the container, regardless of whether any given user who runs the image has python installed on their machine or not.
In this example, we look at a pre-built application called whoami, an HTTP service that prints its own Docker container ID. The application is hosted on a public GitHub repository: https://github.com/jwilder/whoami.
Take a look at the files in the repository to familiarize yourself with the application, particularly the Dockerfile. You will notice that this Dockerfile introduces some new commands, including:
WORKDIRto define the working directory of the Docker container
RUNto specify a command to be executed inside the Docker container
ENVto set an environment variable
EXPOSEto tell the container to listen on a specified network port during its runtime
COPY, which is similar to
ADDand copies files to the Docker image.
Return to the terminal of your local machine for the next steps.
Clone the whoami project to your local machine:git clone https://github.com/jwilder/whoami.git
Navigate into the
whoamidirectory that has been created:cd whoami
Build the Docker image for the application:sudo docker build -t testwhoami .Tip:
If you are using a Mac with an Apple Silicon chip, you need to add the
--platform linux/amd64argument to the above command. This is because the whoami application is not yet compatible with the ARM architecture.
Run the container image with the following command. Note that the
-dargument tells Docker to run the container in the background (detached mode), and the
-pargument exposes the necessary ports for the application:sudo docker run -d -p 8000:8000 -t testwhoami
Carry out the following command to show all the Docker processes currently running:sudo docker ps
You should see the
testwhoamiimage in the output, showing that the container is running.
Open up a browser and go to the application’s endpoint, which is the IP address of your local machine with the port 8000:http://localhost:8000
You should see that the container image is printing out its container ID at the endpoint, with an output similar to the following:I'm 41ed60d6177a
The container ID should match that which you saw with the
Run the following command in your terminal to shut down the container. Replace
<container-ID>with the ID for your container.sudo docker kill <container-ID>
sudo docker psand/or refresh the endpoint in your browser, to show that the container is no longer running.
To finish, we push the Docker images we have created to a container registry. Container registries are designed to store container images, and make them accessible to those who need them. There are many different container registry platforms, but here we use the Scaleway Container Registry.
Open a browser and go to the Scaleway console’s Container Registry page.
The following steps should be carried out in the terminal of your local machine:
Log in to your Container Registry namespace:sudo docker login <address-of-your-namespace> -u nologin -p $SCW_SECRET_TOKENTip:
Ensure that you replace
<address-of-your-namespace>with the address of your own Container Registry namespace (e.g.
rg.fr-par.scw.cloud/mynamespace) and that you have created your API key and that you have saved the secret part of your API key as an environment variable e.g. with the command
Tag the image you want to push with the address of your Container Registry namespace:sudo docker tag testwhoami <address-of-your-namespace>/whoami
Push the tagged image to the Container Registry namespace:sudo docker push <address-of-your-namespace>/whoami
Refresh your Container Registry namespace in the browser to ensure your image has been successfully pushed.
You have seen how to containerize an application by creating a Dockerfile, using the Dockerfile to build a Docker image, and then running the containerized application from the image. As well as this, you can then push the image to a container registry, so that it can be pulled by other users who want to run the application. Future tutorials in this series will show you how to create a Kubernetes Kapsule in the Scaleway console, and use that to deploy your containerized application at scale. We’ll then go on to cover more advanced topics such as load balancing your application and managing its storage. Do not hesitate to check out some of the links below in the meantime.
- Video tutorial: Getting started with Kubernetes: Part 1 - Containers & Docker
- Kubernetes Concepts
- Introduction to Kubernetes
- What is a container? [Docker documentation]
- What is a container? [VMware video]
- Containerization Explained [IBM video]
- Understanding Docker & Kubernetes Visually [Aurelie Vache videos, FR]
- Official Docker documentation
- Docker Cheat Sheet
- Scaleway Container Registry documentation