Getting Started with Docker
Docker is a product that offers hardware virtualization at the operating system level, allowing developers to package and ship software as containers.
Sep 17, 2020 • 6 Minute Read
Introduction
Containerization can be defined as virtualization at the operating system (OS) level to allow deploying and running of distributed applications without the need for entire virtual machines (VMs) per application. This is economical in terms of computing resources since multiple isolated applications can run on a single host. Containerization became popular among development teams because containerized applications always run the same way regardless of host infrastructure.
Docker is a product that offers hardware virtualization at the operating system (OS) level. This ability allows developers to package software and its dependencies in order to ship it as containers.
An image is a software package that includes the source code and all required dependencies for the software to run. In Docker, an image is generated by the build command.
A container is a running instance of an image. In Docker, containers are executed using the run command.
This guide introduces you to Docker and how to use it to package and run applications. It assumes you are familiar with the LINUX environment and can comfortably interact with the Linux bash environment.
Docker
Docker also offers a service called DockerHub that allows sharing and managing containers among developers. This can be referred to as a "GitHub" for container images because it shares some similarities with the code repository platform, such as uploading images via push commands and downloading images via pull commands.
In the event that a developer wishes to run more than one container in a certain order of dependency, Docker offers a service called docker-compose. The tool is used for defining and running multi-container Docker applications. An example is a Django application running behind an NGINX proxy on a Postgres database. In this example, there is the Postgres container, the Django app container, and the NGINX container.
Installation and Setup
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
Basic Docker Commands
Below is a list of common Docker commands. More commands and their functionalities and options can be found on the official Docker site.
docker --version: Used to identify the currently installed version of Docker on the host system
docker ps: Used to list all running containers
docker images: Used to list locally stored images
docker login: Used to login into your Dockerhub account
docker push <username/imagename>: Used to upload an image to your remote Dockerhub repository
docker pull <imagename>: Used to download images from Dockerhub
docker build <path/to/docker/file>: Used to generate an image from the specified Docker file
docker run -it -d <imagename>: Used to create a container from the built image
docker stop <container id>: Used to halt the running of the defined container
docker rm <container id>: Used to delete a stopped container
docker rmi <imageid>: Used to delete the specified image ID from local storage.
Sample Script
To demonstrate Docker in action, run a sample Flask script within Docker.
from flask import Flask
server = Flask(__name__)
@server.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
server.run(host='0.0.0.0')
Copy the above code block into a file and name it app.py.
The next step is to create the Dockerfile. The logic within the file should be as follows.
-
Set up base image
-
Set up working directory
-
Copy source code to Docker environment
-
Install dependencies
-
Run program
Copy the code below and save it in a file named Dockerfile.
# set base image (host OS)
FROM python:3.8
# set the working directory in the container
WORKDIR /code
COPY . .
RUN pip install flask
# command to run on container start
CMD [ "python", "./app.py" ]
With both files set up, you are ready to build and run your image. To build your image, run the command:
docker build -t myimage .
After the Docker image is built, it is time to run it. Since this is a Flask application that runs on a certain port, provide the port number via the ports(-p) flag.
docker run -p 5000:5000 myimage
The app is now running at the localhost address https://0.0.0.0:5000.
As a challenge, push the newly created image to your Dockerhub repository, download it via the pull command on a different machine, and run the image. You will notice that you will not need setup instructions. You only need to have Docker installed.
Conclusion
Containerization using Docker smoothens the deployment process. This skill is highly useful in positions such as DevOps, team lead, quality assurance (QA), or testing engineer. In team management, the team lead uses Docker scripts to set up a project, which ensures that new and onboarding team members spend less time setting up and more time doing actual development.
If not carefully managed, Docker images can grow abnormally large. To manage this, learn more about slim base images and the use of dockerignore.
To further build on this guide, learn more about docker-compose, Docker Swarm, and Kubernetes for multi-container applications, orchestration, and management.