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
A container is a running instance of an image. In Docker, containers are executed using the
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 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.
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.
To demonstrate Docker in action, run a sample Flask script within Docker.
1from flask import Flask 2server = Flask(__name__) 3 email@example.com("/") 5def hello(): 6 return "Hello World!" 7 8if __name__ == "__main__": 9 server.run(host='0.0.0.0')
Copy the above code block into a file and name it
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
Copy the code below and save it in a file named
1# set base image (host OS) 2FROM python:3.8 3# set the working directory in the container 4WORKDIR /code 5COPY . . 6RUN pip install flask 7# command to run on container start 8CMD [ "python", "./app.py" ]
With both files set up, you are ready to build and run your image. To build your image, run the command:
1docker 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.
1docker run -p 5000:5000 myimage
The app is now running at the localhost address http://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.
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.