How to Run Docker? [A Complete Step by Step Guide]

Photo of author

By admin

Are you a developer and looking to start with Docker? Or are you a beginner at Docker? If so, then you are at the right place. This article is just for you where you can learn how to run Docker.

Docker is the leading open (containerization) platform to create, ship, and run applications. It also allows you to separate your app from its required infrastructure, thus, making it highly portable and platform-independent. The process of leveraging Docker containers helps you to offer very fast software services.

With the help of Docker, managing infrastructure and applications becomes easy. By exploring the benefits of Docker’s methodologies to test, ship, and deploy code faster, you can lower the delay occurring between writing your entire code and running/deploying it.

How to Run Docker?

This blog post will cover an introduction to Docker, the creation of Docker applications, and some of the commonly-used commands.

Introduction to Docker – What is it?

As we mentioned earlier, Docker is the free and open-source platform available to build applications that leverage containers. These are, usually, small and lightweight execution environments that create shared usage of the kernel, but run in isolation.

Docker was developed and launched in the year 2013 and became popular ever since. It is a containerization platform that allows developers to create packaged applications in the form of containers. A container comprises standard executable elements and the source code of the application combined with all the required OS libraries and dependencies.

Although developers can develop containers without using Docker, the platform is usually the go-to option as it is very easy-to-use and safe, and simple for building, managing, and deploying containers. Docker is a necessary toolkit that allows developers to build, deploy, update, stop, and run applications by just using certain commands.

Docker can run and package applications in an isolated environment, which is known as a container. Both the security and isolation aspects of the same enable you to run many containers simultaneously.

As we said, containers are lightweight as they do not require any extra load of a hypervisor (like a VM does) and run within the operating system – Kernel directly – running on the host machine.

It means that you can run more than one container on a hardware combo if you were already using virtual machines. Also, Docker does not depend on anyone’s system. You can also run Docker containers within host machines that are known as virtual machines.

Docker offers you a platform and the toolkit for managing the containers’ lifecycle. Some of its highlights are:

  • Create your application along with its elements by using containers.
  • Containers are the unit to test and distribute your app.
  • When you get ready, start deploying your app in your environments like an orchestrated or container service. It works the same no matter whether your environment is a cloud provider, a hybrid, or a local data center.

Why Use Docker and How is it Useful?

Docker is a tool that can greatly benefit a developer. It is quicker. Your application starts within a few seconds and stops even faster, which is not the case with a virtual machine. This is why the work of the user becomes easy and quick. So below are some more great reasons to use Docker:

  • Docker can run on all platforms, so it is a multi-platform application. You can launch your container on any of the systems using Docker.
  • Containers can be developed as well as destroyed quicker compared to virtual machines.
  • Once you configure Docker, you will never be required to reinstall your dependencies manually again. Suppose, you took the decision to change your computers, or if a new employee joins your firm, then you just need to provide them with your configuration.
  • You can also keep your workspace clear because each of your environments is isolated, which can be deleted anytime without impacting the remaining ones.
  • It is easy to deploy your project on the server.

The above are some of the reasons why you must go for Docker. Now, let us check out some advantages of using Docker:

1. Reproducibility

Like Java applications, Docker also runs on any of the machines having the ability to run a JVM. Docker containers are recognized and identified by any system having the ability to run Docker.

By distributing files among all team members, a firm can ensure that all images developed from the same Docker file of Docker will work the same. A constant and well-documented environment makes things easy for tracking an application and recognizing issues.

2. Isolation

Settings or dependencies inside a container will never affect any of the configurations or installations on your PC, or any other containers which might be running.

Using separate containers for every element of an app, you need to prevent conflict in dependencies. You can also go for many projects on one individual server without any conflicts on your system while using Docker.

3. Security

Having important caveats to separate different elements of the big app in many containers can have advantages of security. Thus, if one container is affected, the rest stays safe and secure without any impact.

4. Docker Hub

For simple or common use cases, like the LAMP stack, saving and pushing images to the Docker Hub means that there are many images available there. Having the capability to pull a premade image or develop from the maintained file of Docker can set up the process quicker as well as in a simple way.

In a nutshell, Docker is now:

  • Improved
  • Seamless and portable
  • Lightweight along with more granular updates
  • Available for automatic creation of containers
  • Tracking container versions
  • Allowing reuse of containers
  • Enriched with many shared libraries

How to Create your First Application on Docker? [Installing and Running]

Now you are clear about what Docker is. So it is the right time to get set go for creating your first application using the popular containerization platform.

But wait, do you know how to run Docker and install it on your PC? The main aim of this guide is to develop a complete python program that shows you the output of one sentence.

This program will be launched via Dockerfile. But before that, you must know about the steps to install and run Docker on your computer or laptop. You need not worry because it is a no-brainer when you already understand the complete process.

Please note that you do not need to install python on your workspace. It is totally up to the Docker environment for containing python for executing your code.

1. First, you need to install Docker

If you are an Ubuntu user, update all your packages first with the statement:

$ Sudo apt update

Then, install Docker with the command:

$ Sudo apt install Docker.io

Then just do the verification if your Docker is installed properly.

Then comes ‘how to run Docker?’ For that, you need to enter the following command:

$ Sudo docker run hello-world

2. Time to develop your project

Since you know how to run Docker, you are ready to create your debut project. For creating your first application on Docker, you need to create a folder on your PC. It comprises two files:

  1. A main.py file -> The python file containing the code.
  2. A Dockerfile -> The Docker file containing the required instructions for creating the environment.

Generally, you must have the folder design like the below:

|- Dockerfile

|_ main.py

0 directories, 2 files

3. Next, you must edit your Python File

For this, you need to add the following code to your main.py file:

#!/usr/bin/env python3

print (“Docker is the best tool!”)

With this, you can understand the working of Docker. It must give the following output:

“Docker is the best tool!”

4. Editing the Dockerfile

The primary thing you need to do whenever you wish to create any Dockerfile is to ask yourself what you want to do. Here you aim to launch the python code. For doing this, the Docker container must consist of all the dependencies which are necessary for launching python. Linux or Ubuntu along with python installed on it must be ok to go.

The first step you need to take when you develop a Dockerfile is to have access to Docker Hub. This website consists of several pre-designed Docker images for saving you time. For example, if you search for ‘python,’ the first result will be the official Docker image created for executing python.

5. Create the image

When you are ready with your code and Dockerfile is also written, then you only need to create an image for your application using the command:

$ Docker build -t python-test

-t is the option that allows you to define the image name. In this case, we have selected ‘python-test’, but you can put anything.

6. Run the image

Whenever your Docker image is completely created, then your code is ready for launching. Enter the following command to run it:

$ Docker run python-test

You have to put the name of your image after the Docker run. Afterward, you will be able to see the output.

Useful Docker Commands to Keep in Mind

Following are some of the most common Docker commands that you must keep in mind:

  • $ Docker image ls – To list all the Docker images.
  • $ Docker image rm [image name] – To delete any particular Docker image.
  • $ Docker image rm $(Docker images -a -q) – To delete all of the occurring images.
  • $ Docker ps -a – To list out all of the already available containers (whether running or not).
  • $ Docker stop [container name] – To stop the execution of a particular Docker container.
  • $ Docker stop $(Docker ps -a -q) – To stop the execution of all running containers.
  • $ Docker rm [container name] – To delete any particular container (if it is stopped).
  • $ Docker rm $(Docker ps -a -q) – To delete all containers (if they are stopped).
  • $ Docker logs [container name] – To show up all logs of a Docker container.
  • docker-compose build: It is a Docker compose command to build images in the docker-compose. yml file.

Conclusion

So we have successfully covered:

  • An introduction to Docker,
  • Its benefits, and
  • How to run Docker on Windows.

You need a very simple and concrete understanding of the process to develop your first application on Docker. And this article provides you with detailed information about the Docker platform. So, install Docker now!

People are also reading:

Frequently Asked Questions (FAQs)

1. How do I run a docker command in the terminal?

To run the docker exec command in the terminal, you need a running Docker container. If you don’t already have a container, start a test container with the following docker run command: docker run -d –name container-name alpine watch “date >> /var/log/date. log”

2. What is Docker Compose?

Docker Compose is used to run multiple containers as a single service.

3. What is the Docker run command?

The docker run command is used to create a writeable container layer over the specified image, and then start it using the specified command. This docker command is one of the first commands that you should know when starting to work with Docker.

4. What is Docker Desktop vs Docker?

Docker Desktop is a GUI (Graphical User Interface) that helps you control and manage all settings. It is only available for Windows and Mac. Whereas Docker Engine is an open source containerization technology for building and containerizing your applications.

5. How do I open a docker image in the terminal?

Firstly, obtain the container ID by running the following command: docker ps. You will get an output similar to the following: CONTAINER ID IMAGE NAMES …….. ……. …

Now access the Docker container by running the following command: docker exec -it <container_id> /bin/bash

6. How to run a command in the docker terminal?

Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the –workdir flag to specify the directory: docker exec –workdir /tmp container-name pwd.

 

Leave a Comment