In the previous blog Docker – Part 1, we introduced Docker like what is a docker and why a docker is important? That was enough for the introduction but not adequate for understanding other details. Nowadays docker has become a very useful tool for mobile & web app developers to build and run applications with a combination of all dependencies in one container. It also builds products that let you build and run images as containers on Linux, macOS and Windows.
Today we will see further details on Docker.
Key Points: Docker images, Docker Volumes, Docker Containers, Dockerfile and Docker registry.
Docker Images:
Docker images are used to run a container (similar to run a software). An image is like a blueprint of the project files and it’s dependencies to run a project.
Docker image is an entrenched file, meaning that images cannot be changed. However, containers start from an image, perform the operation within and save a fresh image based on the latest state of the container. Images are constructed with the “build” command and they’ll make a container when started with “run”.
To view images: $ docker images
Docker Containers:
Containers are a lightweight and portable pack of an environment to run applications further.
To transform an image into a container, docker engine takes the image and boots up settings like container name, ID, port, etc. A running container has a currently executing process but when a container has stopped it is known as ‘exited’ in docker. To see what is stopping an image to turn into a container or see a log, you need to run a container in “interactive mode: flag (-it)”.
A container is lightweight, separate, executable package of a software which packs together everything needed to run it: system libraries, settings, code, run-time, dependency, system tools.
To view running container: $ docker ps
To view all(Running + Stopped) container: $ docker ps -a
To view log: $ docker run -it <image>
Docker Volumes:
Containers are stateless, means it does not store any data persistently. After restarting a container or the system; any changes made during the last running session will be wiped out and it gives a clean start (Unless of course, you save it as an image first). But what about persist data practice?
Volume is the answer to the above question. When starting any container you can specify a directory as a mount point for volumes – saves changes into running a container. Any changes you made into the container will be written in the given directory path on your host system. The good thing is that the data will remain as it is if the container gets stopped or removed. When a container is exited, any volumes it had been using will persist — hence, if you start a second container it will be able to use all the data that was there in the previous one.
To view volumes: $ docker volume ls
Dockerfile:
“A Dockerfile is a text configured file that is written in a familiar, human-readable. It is a step-by-step script of all the commands that you will need to run to assemble a Docker Image. There are many more commands that you can run while you are building images from written Dockerfile.”
Dockerfile installs a configuration of what you need to build a project into an image. You can then execute commands like ADD, CMD, ENTRYPOINT, ENV, EXPOSE, MAINTAINER, RUN, USER, VOLUME, WORKDIR, etc.
To build an image from Dockerfile you need to run the command:
$ docker build -t <image name>
Docker Registry:
A Docker registry is a storage and distribution system for Docker images. The same image might have different versions which are identified by tags.
The registry, however, permits Docker users to extract images locally, as well as push new images to the registry.
By default, Docker engine interacts with DockerHub, Docker’s public registry instance. Though it is likely to run on-premise the open-source Docker registry/distribution, and also a commercially backed version called Docker Trusted Registry. There are other public registries available online.
To pull an image from Dockerhub:
$ docker pull <image name>
To pull an image from an on-premises registry
$ docker pull my-registry:0000/<image-name>
Conclusion
Above information and basic commands are that you need to use when you start playing with containers. Now you know how Docker images, Docker Volumes, Docker Containers, Dockerfile and Docker registry work for the developers. 9series is a well known mobile app development company that uses Docker for testing in most of its projects.