What is Docker? Docker Installation and Usage

This article contains information about what docker is and how to install and use it.

What is Docker?

It is a program that allows the creation of containers to sharing the resources of the operating system efficiently.

What is Container?

The Container is basically a structure that allows the efficient use of operating system resources.

Processors and ram in computers are not always fully utilized by the operating system.

Since resources are not fully used, computer resources are divided so that they can be used efficiently by different programs.

For this purpose, various virtualization software such as VMWare and Virtualbox are used.

Virtualization software needs to install new operating system while sharing resources.

A new operating system may cause unnecessary use of resources that applications do not need.

For example; An application used to store user information may not use screen resources.

However, since operating system programs require a video card, video card resources that are not required for the application are also used.

The new operating system for virtualization must be rebooted and installed.

In the container structure, the resources needed by the application are shared softwareatically and unnecessary resource usage resulting from virtualization is prevented.

Because the existing operating system resources are divided in the container structure, applications can be run quickly.

In addition, the working environment required for the application can be prepared with the image in the container structure to ensure that the application is portable.

Docker Installation

The files required for installation are downloaded from docker.com and installed according to the operating system. Installation is done like an application installation on the operating system.

Docker Usage

The installation is checked by typing the following commands into the operating system command prompt(CMD, Powershell, Bash, etc.).

The following command is used for detailed information about image, container and container count in the system.

Create Container From Image

The following command can be used for sample container usage.

The command will create a container using the hello-world image. Before using Docker, it is useful to know how Docker works. Docker needs files called images to create containers.

What is Docker Image?

The image basically contains the files and settings that will be executed when the container is started or after it is started.

Docker creates and runs containers using the commands in the image. In the example of hello-world above, first the hello-world image is searched on the computer, if it is not found, it is downloaded from “hub.docker.com” and a container is created.

Once the container is created, the commands are executed using the Dockerfile settings in the image. A new image can be created when working with Docker. However, it will be useful to use the images at hub.docker.com before creating an image.

Search Docker Images in hub.docker.com

The search command is used to search for images.

Download Docker Images

The pull command is used to download images.

List Docker Images

The images command is used to list images.

Delete Docker Images

The rmi command is used to delete the image.

Docker Image History

The history command is used for image history.

Run Docker Images

The run command is used to run the image.

NOTE: Images not listed are downloaded from hub.docker.com.

There are different uses of image running according to the image content.

The -name parameter is used to run the image with a custom name.

The -it parameter is used to run the image interactively.

The -d parameter is used to run the image in the background.

The -p parameter is used to export the image port.

From the browser, enter localhost:1453.

NOTE: We have accessed port 80 that the image uses, through the 1453.

In order for the application or other files to be used by the image, it must be passed to the image with the volume property.

The -v parameter is used to passing files to image.

Various operating system commands can be used to pass the current directory to image.

NOTE: The directory path indication differs depending on the operating system.

List Running Containers

The ps command is used to list the running containers.

List All Containers

The ps -a command is used to list the running containers.

The top command is used for container operations.

Difference between docker kill and stop

You can learn the difference between stop and kill commad below.

Stop Running Container

The stop command is used to stop the running container.

Force Stop Running Container

The kill command is used to force stop the running container.

Start Stopped Container

The start command is used to run the stopped container.

Which Docker command lets us attach to a running container?

The attach command is used to make the running container interactive.

Run Container Interactively

The ai parameter is used to interactively run the stopped container.

Get Information About Docker Container

The inspect command is used to get information about the container.

Check Docker Resource Usage

The stats command is used for resource usage information of the running container.

Docker Log Information

The logs command is used for the log information of the container.

Remove Docker Container

The rm command is used to remove docker container.

The –rm parameter is used to automatically delete the container after completing its operation.

Remove All Docker Containers

The below commands can be used to remove all docker containers.

NOTE: To remove the running container, it must first be stopped.

NOTE: There are multiple commands in the Docker that perform an operation.

Dockerfile

What is a Dockerfile?

The Dockerfile file is used to create a custom image. The file contains various instructions on how to create a container.

FROM-> Refers to the image to be used.
LABEL-> Refers to the image label.
RUN-> Refers to the commands to be executed. (used for required installations.)
CMD-> Refers to the commands to be executed. (Used for commands to be executed after necessary installations are made.)
ENTRYPOINT-> Refers to the path of the commands to be executed.
COPY-> Specifies the files to be copied to the image.
WORKDIR-> Refers to the working directory.
ENV-> Refers to theenvironment variables.
EXPOSE-> Refers to the image port number.
VOLUME-> Refers to the files to be passed to the image.

The build command is used to create images with Dockerfile.

If the dockerfile is in the same folder, just use a dot(.).

Create a Dockerfile

Create the following file as Dockerfile.

Create the image with the command below.

List images;

Run the images by taking the ID of the image.

You will see “Image created.” on the screen. Similarly, you can create images according to your wishes. Here is an example Dockerfile file that runs Java commands.

NOTE: You can create images according to your need by using the instructions above.

Docker Compose

What is a docker compose?

An application developed can use multiple services.

For example, an application developed using a programming language such as Node.js, PHP, C # or Java may require a database such as MySQL, SQL Server, or MongoDB.

According to the application, services such as “log, http server” can be used.

In this case, instead of gathering all the services under one image, it would be beneficial to use all the services under a different image.

It will be difficult to create and manage each image separately.

The application running in one image may depend on the image contained within another image.

In this case, multiple images can be managed with Docker compose.

Docker compose stores the settings in the docker-compose.yml file.

In the example above, mysql service port and environment variables are set.

In php service;

The Dockerfile file in the php folder was used.

With volumes, the files to be used are determined.

With depends_on, the image to which the image is depended is determined.

As these settings will vary according to the structure to be used, it will be useful to prepare the appropriate structure according to the need.
Have a good day.

Yusuf SEZER
Author: Yusuf SEZER

Leave a Reply

Your email address will not be published. Required fields are marked *