While the demand for new applications is rising, the pressure to keep up with this demand rises too. As a developer, it becomes easier when you have the right tools. Docker is one of such tool, especially for anyone who is into the DevOps path. It's a platform dedicated to the development, shipment, and running of any application.
In this article, we will take a look at the best way to get started with using Docker. You will learn how to create and deploy Docker apps. Then you can also update and share your containerized app.
What we will cover
- What is docker
- The problem Docker solves
- Docker Terminologies
- Docker installation
- Docker commands and how to use them
What Is Docker
In much-simplified terms, Docker is a platform that simplifies the process of building, running, managing, and distributing applications. It is designed to deliver your applications way faster as compared to when not using it. Its key claim is that you can swiftly provide software by separating your software applications from your regular infrastructure. And all of this is accomplished by simply virtualizing the operating system that is installed and functioning on your machine.
The Problem Docker Solves
The Problem
We can consider a scenario where we have 3 different Python applications each using a different version of Python as well as its associated libraries and dependencies. We want to deploy and host these applications on a single server. The problem becomes that we can't have different versions of Python installed on the same machine which prevents us from running the applications on the same computer.
Solution without docker
The solution would be hosting them on three different machines which is not the case we want or getting a machine powerful enough to host and run three virtual machines on it. These two solutions are very costly and have more work for procuring and maintaining the hardware. You also have to consider that you have to set up the Operating System on each machine and the Python environment required for each app manually which is quite cumbersome and repetitive. Now, let's look at docker, how it works and how it can help us solve this problem in a much simpler way.
To work with docker you just require one machine / operating system with Docker installed in it (Docker host). Instead of having to install a virtual machine for each application we have, we just package the application with its dependencies in one unit called a container. Let's look at some docker terminologies before diving even deeper
Docker Terminologies
- Docker Host - The machine on which docker is installed and running.
- Docker Engine - a containerization technology based on open-source for containerizing and building your applications. This is responsible for the overall functioning of the Docker platform.
- Docker Image - This is a template that contains the application, and all the dependencies required to run that application on Docker.
- Docker Container - This is the running instance of the Docker Image
- Dockerfile - Is a simple document that contains instructions on how to create Docker Images
- Docker Compose - This a tool for outlining and running multi-container Docker applications. (We will cover this in another article)
- Docker Hub - This is the official online repository where you can store, distribute custom images or find other Docker Images available for public use
Docker Installation
Before using docker locally or on a machine instance running on the cloud, you need to have Docker installed. Below are links to install docker on Ubuntu, Debian, and Windows. The steps are straightforward.
If you just want to practice using docker commands, You can use the Docker Playground
Docker Commands
Now that we have already installed docker (assuming you have followed the steps in the links above to install it), let's check some docker commands.
docker create
Allows you to create new containers. It has the following syntax
docker create [options] IMAGE [commands] [arguments]
NB: The things enclosed in brackets are optional. You can also run docker create --help
to learn more about each.
Example: docker create ubuntu -t -i ubuntu bash
This will create a container using the ubuntu image. The -t and -i options tell Docker to give the container a terminal so that the user can communicate with it. If the container is launched, the create command is also used to run the bash command. Docker will first check if you have the image locally and if not it will go ahead and download it from the Docker hub and use it to create the container.
docker ps
Allows you to view the currently running containers. To view all containers including those already exited, add -a to the command. docker ps -a
Let's explain some of the details returned by the command
- CONTAINER ID - A singular string consisting of alphanumeric characters, related to each container.
- IMAGE - Name of the Docker image used to create this container.
- COMMAND - Any application-specific command(s) that must be executed when the container is started.
- CREATED - It displays the time taken since this container has been created.
- STATUS - This shows the present status of the container.
- PORTS - This shows whether port mappings are defined for the container or not.
- NAMES - Each container is also given a unique name in addition to the CONTAINER ID. If you want to give the container a name, use the --name (double hyphen name) option in the docker create or docker run command.
docker images
To view all the Docker images on your computer.
Let's explain some of the details returned by the command
- REPOSITORY - This represents the unique name of the Docker Image.
- TAG - Each image is associated with a unique tag. A tag basically represents a version of the image.
- IMAGE ID - A unique string consisting of alpha-numeric characters, associated with each image.
- CREATED - This shows the time elapsed since this image has been created.
- SIZE - This shows the size of the image.
Once you have an image locally on your computer, you can create a container based on it using the docker run command. Eg docker run hello-world
. To explicitly name the container when running it, use the βname option, like this: docker run --name helloworld hello-world
docker rmi
This command allows us to remove an image from the Docker host.
docker rm
Once you are done with a container and no longer need it, you can delete it using the docker rm command. You need to first get the container id using the docker ps -a
command and specify the id together with the docker rm command.
docker rm 1d0284a0eae1
docker start and docker stop
If you just want to stop a container from running without deleting it, you can use the docker stop id
command to halt a container.
Then to start the halted container just use docker start id
.
docker restart
This command restarts any running container.
That's all from this article. I will prepare another article on how to create custom images using a Dockerfile. Until then, keep coding :)
Thank you.