Docker has been around for a few years now, and if you haven’t got your hands on it yet, now is your chance! Containers and virtualization have been used for a long time but Docker does it in a unique way, allowing developers to set up identical and lightweight environments for development, staging and even for production. Let’s dive into the idea behind Docker and how it can be used in a project.
What is Docker?
Docker is a software that lets you run lightweight containers with any type of software running inside. It can be a MariaDB database, Apache, Nginx, PHP or all of these at the same time.
Containers are like virtual machines but more lightweight because they don’t contain a whole operating system inside, only the software you want to run. Therefore, they don’t come with the overhead of running complete operating systems, which allows them to run with lower CPU and RAM usage.
Docker uses features of the Linux kernel to isolate containers, which allows them to be run separately from each other like virtual machines. Although Docker uses the Linux kernel and is only natively supported on Linux, it can also be used on Windows and Mac.
Creating Custom Containers
We already talked about containers, which are running instances and comparable to virtual machines. These containers are built using images, which are text files and act as blueprints to build containers. An image is like a snapshot of a virtual machine: it tells what software will be installed and run inside a container.
Images can be build from scratch using any text editor or pre-built ones can be downloaded from hub.docker.com To create your own image, you need a Dockerfile which is the file in which you tell Docker what the container will run inside. Perhaps you want to run a web and database server. If so, you can set them up in the Dockerfile. You can also base your custom image on an existing image, let’s say a plain Ubuntu 17.04 image. If your image uses Ubuntu, you can install the software you want using apt, the package manager of Ubuntu. The installation commands go inside the Dockerfile.
Building an Image
When your Dockerfile contains all the software you need, you can tell Docker to build an image from it with the “docker build” command, such as “docker build -t my_image”.
Starting a Container
Now that you have a container ready, you can start it with the “docker run” command, for example: “docker run -i -t my_image”. This will create a container from your image and start it. If you want to set up an identical environment for your team members or on a staging server, you can distribute the Dockerfile and build as many identical containers as you want.
No comments yet (leave a comment)