Skip to main content

Posts

Showing posts from April, 2019

Docker - Dockerfile

Greetings!

We used Docker images to create containers multiple times. We used images from Docker Hub to create those containers. Ever wondered how to create a Docker image? Docker can build images automatically by reading the instructions from a Dockerfile.

Dockerfile A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Think of it as a shellscript. It gathered multiple commands into a single document to fulfill a single task.
build command is used to create an image from the Dockerfile.

$ docker build . You can name your image as well.
$ docker build - my-image .
Let's first look at a Dockerfile and discuss what are those commands.
This is extracted from official MySQL Dockerfile.

FROM debian:stretch-slim # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r mysql && useradd -r -g mysql mysql RUN apt-get update &&…

Docker - Networking

Greetings!

When we talked about Docker, we said that containers are isolated. Then how do we communicate with our containers. Say we are using MySQL database. It is not useful if we can't access it.

Docker has a network concept. It has several network drivers to work with. Depending on how do we want out container to behave, we can select our network. This help us to communicate container with a container or container with host.


Network commands summarydocker network ls - list available networksdocker network create - create a networkdocker network rm - remove a networkdocker network inspect - inspect a networkdocker network connect - connect container to a networkdocker network disconnect - disconnect container from a network
Docker network driversbridge - This is the default network. When the Docker daemon service starts, it configures a virtual bridge names docker0. When we don't specify the network this is the one docker uses. Docker creates a private network inside the host…

Docker - Volumes

Greetings!

Sharing is caring. When the container is running/down or removed we need to access the data within it. Be it a database, web application logs it needs to share some form of data with host or with the other containers. Docker provides volume to achieve this.

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

Volume commandsdocker volume create - create a volumedocker volume ls - list available volumesdocker volume remove - remove a volumedocker volume prune - remove all unused volumesdocker volume inspect - inspect a volume
Create a volumedocker volume create my-volume
List volumesdocker volume ls
Inspect a volumedocker volume inspect my-volume
Remove a volumedocker volume rm my-volume
Remove all unused volumesdocker volume prune
Start a container with a volume We can start a container with a volume using --mount or -v flag. As in the docs, New users should try --mount syntax which is simpler than --volume syntax.
If the volume does …

Docker - Images and Containers

Greetings!


Image An image is a read-only template with instructions for creating a Docker container. It is a combination of file system and parameters. Often, an image is based on another image with some additional customization.
We can use existing images or create our own images.

Container A container is a runnable instance of an image. We can create as many as we want from an image. Container is isolated from the host by default. We can modify it's behavior using network, volume, etc.
When a container is created, we can stop, restart, remove it.

Download an Image We can download a Docker image using 2 methods.
pull - we can use pull command to get an image$ docker image pull nginxcreate a container - when we create a container from an image, it downloads the image from the repository if it is not available in the host.$ docker container run nginx
Docker command structure There are many commands. So Docker has grouped them together into a common format.
docker COMMAND SUBCOMMAND docke…

Docker - Introduction

Greetings!

Let's talk about Docker container ;)

The Problem Software packaging, distribution, installation is not that easy. It is true that there are easy to use software packages. Normally software depends on other libraries. To install a software it needs to install those dependencies first. What if those libraries have other dependencies? What if there are version conflicts?

Let's see a picture of a software installation.



It is a web of libraries. Now imagine we need to uninstall our software. Will it remove it's dependencies properly. Will that have an impact on other software? How do we install another version? What if you need to main multiple computers with this same setup?

There are many questions though we can somehow solve. Imagine time, energy we spend on these. Is it worth?

Lets say we want install MySQL as the database. Why do we need to spend lot of time for that when our main task is something else. These are reasons we need to find other way to software dis…

Kotlin - Control Flow - when

Greetings!

Just like if, when also an expression. It has 2 forms.
With a value - behave as a switch operator.Without a value - behave as if-else-if chain.
when as a switchJavaprivate void dayOfWeek(int dayOfWeek) { switch (dayOfWeek) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); break; default: System.out.println("Invalid day"); } }Kotlinprivate fun dayOfWeek(dayOfWeek: Int) { when (dayOfWe…

Kotiln - Control Flow - If

Greetings!

If is the most basic way to control flow in Kotlin. Unlike Java, in Kotlin if is an expression. That is it return a value.
Statement - is a program instruction that return no value. Can't be on right side of the equal sign.Expression - is a program instruction that return values. Can be assign to a variable.Java private void findMax(int a, int b) { int max; if (a > b) { max = a; } else { max = b; } System.out.println("Max value is " + max); }
Kotlin (traditional statement)private fun findMax(a: Int, b: Int) { val max: Int if (a > b) { max = a } else { max = b } println("Max value is $max") }
Kotlin (as an expression)private fun findMax(a: Int, b: Int) { val max: Int = if (a > b) { a } else { b } println("Max value is $max") }
Ternary Operator Kotlin doesn't have a ternary operator. It's because result of if, else can be assig…