All-Things-Docker-and-Kubernetes

Docker Objects

There three docker objects that you need to know:

In addition to these three, here’s some three useful topics:

Dockerfile

This is a set of step-by-step instructions on how to create the image:

The full list of instructions can be found in the official Docker website. Here are some of the widely used instructions:

Below is an example of a Dockerfile that targets to package a Python hello-world application:

# set the base image. A Python base image is used
FROM python:3.8

# set a key-value label for the Docker image
LABEL maintainer="Eden Jose"

# All the files in the current directory is copied
# to the  `/app` directory in the container
COPY . /app

#  defines the working directory within the container
WORKDIR /app

# run commands within the container. 
# Here we install dependencies defined in the requirements.txt file. 
RUN pip install -r requirements.txt

# provide a command to run on container start. 
# For example, start the `app.py` application.
CMD [ "python", "app.py" ]

Creating the Dockerfile

Before we “bake” the image, we first need to understand how the application is built. We could start by listing down how we might deploy an application manually.

As an example, if we are to deploy a web application, this would be the steps we’ll follow:

1.  Start with OS - CentOS
2.  Update repo
3.  Install dependencies
4.  Install python dependencies
5.  Copy source code to /opt folder
6.  Run the web server using the flask command

Once we have the steps laid down, we can begin containerizing our application:

1.  Create a DOCKERFILE
2.  Build your image and specify the file as input, as well as the tag name
3.  Push it to the dockerhub repository to make it publicly available 

To create a dockerfile, here’s a basic flow we can follow:

Docker Image

After we’ve created the dockerfile, we can now create the docker image. A docker image is

Overlay Filesystem

A docker image has different layers, with the first layer as the base image that the image will use and the layers on top as packages being installed.

The last layer is a writeable layer which applications will use. If a container is started without defining a specific storage option, any data written to the default storage by an application running in a container will be removed as soon as it is stopped.

Building the Docker Image

A Docker image can be built from an existing Dockerfile using the docker build command. Below is the syntax for this command:

$ docker build [OPTIONS] PATH

To find all valid options for the build command:

$ docker build --help

For example, to build the image of a Python “Hello-world “ application from the Dockerfile in the current directory, the following command can be used:

$ docker build -t python-helloworld .

Additionally, you can build the same app that’s located on a different directory, say a lesson1/python-app directory

$ docker build -t python-helloworld /another/directory/python-app

To list all available ../../imageS

$ docker ../../images

You can read more about the docker basic commands in the succeeding sections.

Docker Registry

Once you’ve package the application, tested it locally, and proved that it’s meeting the expected behavior, You are now ready to store and distribute it.

To do this, you can push the image to a public Docker image registry, such as

You can also store the image to a private registries and make it available to authorized parties. To be access the registry, you would need to be authenticated by setting up an account.

Authenticating to the Registry

You can login to Dockerhub in your terminal. dockerhub. Note that you need to set up an account..

$ docker login

Now that we’ve access our registry, we’re now ready to push our images. However, it is best practice to tag all your images before sharing them.


Back to first page