March 7, 2017
What is Docker?
Docker packages the application into a container which can be used for software development. Docker is light-weight, open source and secure. As the official quote of docker says “Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.”
Why use docker?
The goal of docker is to speed up the deploy and delivery of application using containers. Docker containers allow bundling the application with all necessary and dependent components/libraries into a single package. Docker container is similar to VM but is not a complete OS. It has just the minimum set of OS software which needed for the application. Thus allowing the container to use less memory and start quickly. Filesystems inside the docker image are layered and share common files making the usage more efficient.
Assume, you are creating an application with all dependencies and you want to deploy into other machine and make it to work same as your machine. Docker actually does that. You don’t need to worry whether the target machine has all the dependencies available. You just deploy the container and run it and you’re in business.
Containers are not the new idea
The concept of containers are live at least from the early 2000’s and few were Free BSD Jails, Oracle Zones, OpenVZ, and LXC. Free BSD allow users to run the app in a sandbox separating them from one another.
With the docker, you can set up production/server like environments for local development. Docker containers can also be available in the cloud.
“My code works on my machine but not on the server.” Docker helps to solve this problem.
How to create docker container
Docker container can be created by following steps.
-Write DockerFile in your application
-Build image from the DockerFile.
-Start the docker container.
Docker file describes all the files, environment and commands to create a docker image.
Assume we want to setup PHP, Apache in a ubuntu environment. Below is the DockerFile contents.
//Getting the standard ubuntu OS image
// Update ubuntu packages and install apache, PHP
RUN apt-get update && apt-get install -y php7.0 apache2 libapache2-mod-php7.0
// Start apache in foreground
CMD /usr/sbin/apache2ctl -D FOREGROUND
However, DockerFile will allow us to build single image and start a single container. What if we want to start multiple containers in parallel ?. Docker does support that using docker-compose.
Docker compose is a tool to define and start multiple containers simultaneously. As the official doc saysUsing, Docker Compose is basically a three-step process.
-Using Docker Compose is basically a three-step process.
-Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
-Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
-Lastly, run docker-compose up and Compose will start and run your entire app.
Below is the sample docker-compose file for apache, php and mysql
So now running the docker-compose up will start all the three containers simultaneously.
The easiest way to deploy and maintain containers is to docker containers. Below are the service provider for docker engines.
-Amazon Web Services
-Google Compute Engine
-Install only necessary libraries.
-Using a .dockeringore to ignore file or directory to exclude when building the image.
-Splitting an application into multiple application making it easier to scale.
-Minimize the number of commands in DockerFile by clubbing it together.
-Always make use of docker cache when building the image.
-Use official repositories as the base of the image and pull using FROM command.
-It is recommended to use VOLUME for any user-serviceable parts of your image.
-Update and commit docker image.
-Don’t store data inside the containers.
-Make the image small and avoid creating large images.
-Mount the volume into the container for any live changes.
-Keep it simple.