Introduction to Docker Tool
When perfectly configured, Docker can perform the functions of multiple servers, saving you the cost of purchasing additional servers. Furthermore, Docker operates on the principle of Continuous Integration and Continuous Deployment (CI/CD), a DevOps technology that helps deploy code quickly.
What is Docker?
Docker is a tool that allows you to create, deploy, and run applications in containers.
Containers are software units that help virtualize parts of an operating system and run an application by packaging all the code and dependencies together.
Consider this example: Mike, a developer, is asked to create a web application for his organization. To do this, he sets up an environment with an Apache Tomcat server. He completes the application and informs Andrew, the organization’s tester, to test it on his system. Andrew sets up a Tomcat environment on his system and completes the testing process. Andrew then informs Gary, who is responsible for deploying the application to the production environment, about the completion of the test. Gary also sets up a production Tomcat environment on his system.
So, what happened here is that an environment was set up three times, which is a complete waste of time. Additionally, there is a high chance of errors due to the different Tomcat versions installed by the three stakeholders. This is where Docker can help. Docker can create a single Tomcat environment image that can be used by all three stakeholders.
Simply put, an image is a file that can be used to run an application in Docker. You will learn more about images in the following posts. But for now, we will help you set up Docker Desktop.
How to install Docker Desktop
If you are using a Windows machine, installing Docker Desktop is as straightforward as installing any other software. Here are the steps needed to install it:
System Requirements
- Windows 10 64-bit (Pro, Enterprise, and Education)
- Enabled Hyper-V and Windows containers features
To run Hyper-V, you need:
- 4 GB RAM
- Hardware virtualization support enabled in the BIOS
- A 64-bit processor with SLAT (Second Level Address Translation)
Steps to install
- Download the Docker Desktop Installer.
- Once the file is downloaded, click on Docker Desktop Installer.exe to start the installation.
- Complete the installation by following all the instructions in the installation wizard after accepting the license and authorizing the installer to make changes to your system. The changes include installing network components tied to the Docker apps and managing the Hyper-V VMs.
- Click Finish to complete the installation.
Once the file is installed, click on the Docker Desktop file in the installed directory to launch the application.
Now that Docker Desktop is installed, you can start creating an image by adding instructions to a Dockerfile and executing it.
What is a Dockerfile?
A Dockerfile is a text file or script containing various commands used to build an image via the command line. Docker reads these instructions from the Dockerfile to create images. A Dockerfile is typically used to organize tasks and simplify the process. The best part about a Dockerfile is that it uses simple and clear syntax, making it extremely easy to create and execute. They even allow for comments, just like other programming languages.
Typically, Dockerfiles start with the FROM command, which defines the base image to use to start the build process. This is then followed by other methods, commands, and arguments. After executing the file in Docker, a new image is created that can then be used to create containers.
Dockerfile Commands
- ADD: This instruction is used to copy a new file from a source on the host and add it to the filesystem of the image at the set destination.
- CMD: This instruction is used to execute a specific command within the container. CMD specifies the default arguments for the executing container. There can be only one CMD instruction in the Dockerfile. If there is more than one, only the last one will be considered.
- ENTRYPOINT: This instruction helps set a default application that can be used every time a container is created. In other words, it helps configure a container to run as an executable.
- ENV: This instruction is used to set environment variables. It sets a variable to a value, which will be used in all subsequent instructions during the build stage.
- FROM: This instruction is the most crucial in the entire Dockerfile and defines the base image to use to start the build process. It initializes a new build stage and sets the base image for all subsequent instructions.
- MAINTAINER: This instruction is used to define the full name and email address of the image creator.
- RUN: This instruction executes all commands in a new layer. This will be on top of the current image. It will capture the results. The committed image will then be used for the next step in the Dockerfile.
- USER: This instruction is used to set the username of the user who will run the container.
These are just a few popular commands frequently used when creating an image using a Dockerfile. There is much more to explore and use in your next Dockerfile.
The below image is a simple snippet of a Dockerfile. The code is added to a text editor and saved without any extension.
FROM ubuntu
MAINTAINER example@example.com
RUN apt-get update
CMD ["echo", "Hello, Docker!"]
In this Dockerfile:
- An Ubuntu image is used as the base image to start the build process.
- The MAINTAINER specifies the name and email address of the creator of the new image.
- The RUN command here will execute apt-get update to obtain the latest package version of Ubuntu.
- The final instruction is CMD, which will execute echo with the provided parameter. This is only executed when we create a container from the image.
Conclusion
We have now read about the crucial role Docker plays in efficiently creating, deploying, and running applications within isolated containers. Additionally, we have been introduced to key Docker commands, including ADD, CMD, ENTRYPOINT, ENV, FROM, MAINTAINER, RUN, and USER.
For more information, please get in touch.