Docker containers have now become a standard in software development. Ever since it was launched some seven years ago, it has kept gaining popularity and capturing the development ecosystem. Docker has now become a standard way of building and deploying large applications.
Docker has also eliminated the problem of infrastructures and version conflicts by providing a single portable platform for building and running the applications. No more problem with this code working on my machine but not on the server! As long as the Docker is running on both the machines you can sit back and focus on development without having to worry about the underlying infrastructure.
So, to leverage the power of Docker let’s look at how we can start working with Docker.
Docker is available free of charge for all operating systems. Whether you are working with macOS, Linux-based distributions, or Windows, Docker is there for you to download. Installing Docker on Windows and Mac is a straightforward process. When installing for MacOS, Docker is available for both Apple and Intel chips. For Linux, you have to download the correct version for your distribution.
On Linux, you can also install by setting up the Docker repository. Next, install the packages using the appropriate command. For instance, on Ubuntu, you would use the apt-get command to download and install the packages.
Installing Docker is not a challenging task. Once installed you can get all the information about the installation using the docker info command.
Using the Docker
To effectively use Docker, you have to understand the basics of Docker.
Docker engine is the underlying platform that is responsible for downloading the images, building and running applications, and managing infrastructure.
Docker Daemon can be thought of as the core of the Docker engine. It is a background service running that takes in the commands, to build and run containers, and hands them over to the Docker engine.
Docker Command Line Interface provides a way of interacting with Docker via command-line tools. It connects the user with the Docker engine which can either be on the same host or some remote host.
Docker Image is the heart of Docker applications. A Docker image can be considered as the blueprint of the Docker container since it carries all the needed instructions and information on Docker to build a Docker container from the image file, dependencies, starting point, date volumes, network configuration, and more. From one image, multiple containers can be built.
So, let’s suppose you want to deploy your ReactJS application. Where do you get a server from? The answer is Docker Hub. Docker Hub is a remote repository of all the Docker images available to download. Images are built and maintained by the community for the community. All you need to do is to pull the image you intend to use, and you are good to go.
A Dockerfile is a YAML-based file for building custom images. These files specify how to configure the network, dependencies, container’s OS, environment variables if needed, and also commands needed to run the container.
Working with Docker
Now that you know the basics of Docker, it’s time to start working with Docker. For this you need to follow the steps below:
- First, you need to identify the container image you have to use. On the Docker Hub, container images are present with versions so that you can pick up the correct version to use. Docker Hub also lists important information such as which ports are exposed or data volumes so that you can conveniently work.
- Once you have identified the Docker image, you can pull the Docker image to your machine using the docker pull This command fetches the image for you to run locally. Once you have the local copy of the image, no further pulling is needed.
- You can run the image after copying your application into the working directory by running the docker run command. This command creates a container from the Docker image file. From one image file, you can create as many containers as you wish. In other words, you can create as many Docker applications as needed using just one single source. This is very helpful when building the microservices application for horizontal scaling. When you create a container from a Docker image, each container is specified as an identifier and name.
- Finally, once you are done with the application you need to stop the container. This will release the resources associated with the Docker application. First, you need to grab the identifier of the Docker container mentioned in point 3. Next use the docker stop command to stop the container. This will stop the container. To kill the container run the docker kill Alternatively, you can also pause the container if you do not want to kill them. Run the docker pause command to pause the container. All these commands: docker stop, docker kill, and docker pause command need the identifier of the container upon which to act on.