Docker – ‘A better way to build apps’, as stated on its website, is an open-source platform for building apps and microservices. The catch here is the automated deployment of your app in a container, by OS level virtualization provided by Dockers. Dockers are better than VMs as you can do away with the additional costs for maintaining and starting the latter. By deploying your app and its dependencies (i.e. the pre-requisite apps for its proper functioning) in a container, your app becomes portable during all the phases of development and testing. Moreover, the isolated apps eliminate conflicts, enable team collaboration, and reduce the time-to-market.
When Do You Need to Use a Docker?
- For replicating the environment on your server, while running your code locally on your laptop
- For Docker CI/CD during numerous development phases (dev/test/QA)
- For distributing your app’s OS with a team, and as a version control system.
rpm -iUvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum update -y
yum -y install docker-io
service docker start
chkconfig docker on
yum update -y
yum -y install docker-io
yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum list docker-ce.x86_64 --showduplicates | sort -r
yum install docker-ce-<VERSION>
usermod -aG docker root
systemctl start docker
systemctl enable docker
How Do You Use a Docker?
The biggest advantage of VMs is that they create snapshots which can be revisited instantly later. Docker containers further enhance the lightweight process virtualization by being OS independent and using the Linux Kernel’s functionality. They are created from Docker images – like snapshots. Docker images are created using a Docker file which can be customized or used as is. The default execution driver for creating a docker container is ‘libcontainer’. Docker Hub can be used for searching docker images and seeing the way they have been built.
- To create a Docker container, download the ‘hello world’ image, by typing the following command in the terminal –
$ docker run hello world
- For checking the number of images on your system, use the following command –
$ docker images
- For searching an image in the Docker Hub –
$ docker search <image>
Docker Commands and Best Practices
Before we get into the best practices for using Docker, here’s a quick overview of the vocabulary you should know:
- Layer: a set of read-only files or commands that describe how to set up the underlying system beneath the container. Layers are built on top of each other, and each one represents a change to the filesystem.
- Image: an immutable layer that forms the base of the container.
- Container: an instance of the image that can be executed as an independent application. The container has a mutable layer that lies on top of the image and that is separate from the underlying layers.
- Registry: a storage and content delivery system used for distributing Docker images.
- Repository: a collection of related Docker images, often different versions of the same application.
With that refresher in mind, here are some quick tips for building applications with Docker:
- Try to keep your images as small as possible. This will make them easier to transfer and faster to load into memory when starting a new container. Don’t include libraries and dependencies unless they’re an absolute requirement for the application to run.
- If your application needs to be scalable, consider using Docker Swarm, a tool for managing a cluster of nodes as a single virtual system.
- For maximum efficiency, use Docker in combination with continuous integration and continuous deployment practices. You can use services such as Docker Cloud to automatically build images from source code and push them to a Docker repository.
Below, you’ll find all of the basic Docker commands that you need to start working with containers:
Developing with Docker Containers:
- docker create [image]: Create a new container from a particular image.
- docker login: Log into the Docker Hub repository.
- docker pull [image]: Pull an image from the Docker Hub repository.
- docker push [username/image]: Push an image to the Docker Hub repository.
- docker search [term]: Search the Docker Hub repository for a particular term.
- docker tag [source] [target]: Create a target tag or alias that refers to a source image.
Running Docker Containers
- docker start [container]: Start a particular container.
- docker stop [container]: Stop a particular container.
- docker exec -ti [container] [command]: Run a shell command inside a particular container.
- docker run -ti — image [image] [container] [command]: Create and start a container at the same time, and then run a command inside it.
- docker run -ti — rm — image [image] [container] [command]: Create and start a container at the same time, run a command inside it, and then remove the container after executing the command.
- docker pause [container]: Pause all processes running within a particular container.
Using Docker Utilities:
- docker history [image]: Display the history of a particular image.
- docker images: List all of the images that are currently stored on the system.
- docker inspect [object]: Display low-level information about a particular Docker object.
- docker ps: List all of the containers that are currently running.
- docker version: Display the version of Docker that is currently installed on the system.
Cleaning Up Your Docker Environment:
- docker kill [container]: Kill a particular container.
- docker kill $(docker ps -q): Kill all containers that are currently running.
- docker rm [container]: Delete a particular container that is not currently running.
- docker rm $(docker ps -a -q): Delete all containers that are not currently running.
Check out the complete list of commands in the Docker documentation