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 a

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 summary docker network ls - list available networks docker network create - create a network docker network rm - remove a network docker network inspect - inspect a network docker network connect - connect container to a network docker network disconnect - disconnect container from a network Docker network drivers bridge - 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

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 commands docker volume create - create a volume docker volume ls - list available volumes docker volume remove - remove a volume docker volume prune - remove all unused volumes docker volume inspect - inspect a volume Create a volume docker volume create my-volume List volumes docker volume ls Inspect a volume docker volume inspect my-volume Remove a volume docker volume rm my-volume Remove all unused volumes docker 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

Docker - Images and Containers

Greetings! Docker image to container(s) 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 nginx create 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 group

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 w

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 switch Java private 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"); } } Kotlin private fun dayOfWeek(dayOfWeek: I

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 bec