NavigationContentFooter
Jump toSuggest an edit

Getting started with Kubernetes Part 1 - Containers & Docker

Reviewed on 08 October 2024Published on 07 April 2022
  • Kubernetes
  • 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.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • A valid API key

Key concepts: containers, Docker and Kubernetes

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:

  1. Build your application,

  2. Containerize it with Docker,

  3. 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 means 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.

Installing Docker

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.

  1. Remove any previous versions of Docker that might be installed:

    sudo apt-get remove docker docker-engine docker.io
  2. Update the APT package cache:

    sudo apt-get update
  3. 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
  4. Add Docker’s GPG key, used to authenticate the Docker content and updates we download to our machine:

    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
  5. 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:

    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
    $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  6. Update the APT package cache again:

    sudo apt-get update
  7. Install the latest version of the Docker engine using the following command:

    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  8. Check that the installation of Docker is successful by running the hello-world image:

    sudo docker run hello-world

    This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.

Creating and deploying containerized applications with Docker

We show two examples of making containerized applications with Docker and take you through the following process for each:

  1. Creating the Dockerfile
  2. Building an image from the Dockerfile
  3. Using the image to run the containerized application

Example 1 - a simple Hello World app

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.

  1. Create a folder called myfirstapp and navigate into it:

    mkdir myfirstapp && cd myfirstapp
  2. Create a python file called myapp.py:

    nano myapp.py
  3. Write the following line of code inside the file, then save and exit:

    print("Hello World")
  4. 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.py with the following command:

    nano Dockerfile
  5. Copy and paste the following code into the Dockerfile, then save and exit:

    FROM python:3
    COPY 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 myapp.py application.
  6. Run the following command to build a Docker image for the application, using the Dockerfile just created:

    sudo docker build -t mytestapp .

The -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.

  1. 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 on 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.

Example 2 - a more complex app

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.

  1. 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:

    • WORKDIR to define the working directory of the Docker container
    • RUN to specify a command to be executed inside the Docker container
    • ENV to set an environment variable
    • EXPOSE to tell the container to listen on a specified network port during its runtime
    • COPY, which is similar to ADD, and copies files to the Docker image.

    Return to the terminal of your local machine for the next steps.

  2. Clone the whoami project to your local machine:

    git clone https://github.com/jwilder/whoami.git
  3. Navigate into the whoami directory that has been created:

    cd whoami
  4. 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/amd64 argument to the above command. This is because the whoami application is not yet compatible with the ARM architecture.

  5. Run the container image with the following command. Note that the -d argument tells Docker to run the container in the background (detached mode), and the -p argument exposes the necessary ports for the application:

    sudo docker run -d -p 8000:8000 -t testwhoami
  6. Carry out the following command to show all the Docker processes currently running:

    sudo docker ps

    You should see the testwhoami image in the output, showing that the container is running.

  7. 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 docker ps command.

  8. 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>
  9. Run sudo docker ps and/or refresh the endpoint in your browser, to show that the container is no longer running.

Pushing to the Scaleway Container Registry

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.

  1. Open a browser and go to the Scaleway console’s Container Registry page.

  2. Follow these instructions to create a namespace. Make sure that you set your privacy policy to private.

    The following steps should be carried out in the terminal of your local machine:

  3. Log in to your Container Registry namespace:

    sudo docker login <address-of-your-namespace> -u nologin -p $SCW_SECRET_TOKEN
    Tip

    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 export SCW_SECRET_TOKEN=a4db08b7-5723-4ba9-8c18-f2de493465a2.

  4. Tag the image you want to push with the address of your Container Registry namespace:

    sudo docker tag testwhoami <address-of-your-namespace>/whoami
  5. Push the tagged image to the Container Registry namespace:

    sudo docker push <address-of-your-namespace>/whoami
  6. Refresh your Container Registry namespace in the browser to ensure your image has been successfully pushed.

Conclusion

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
Was this page helpful?
API DocsScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCareers
© 2023-2024 – Scaleway