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.
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.
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.
docker run hello-world
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.
docker search hello-world
Download Docker Images
The pull command is used to download images.
docker pull hello-world
List Docker Images
The images command is used to list images.
Delete Docker Images
The rmi command is used to delete the image.
docker rmi hello-world
Docker Image History
The history command is used for image history.
docker history hello-world
Run Docker Images
The run command is used to run the image.
docker run hello-world
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.
docker run --name my-image hello-world
The -it parameter is used to run the image interactively.
docker run -it centos /bin/bash
The -d parameter is used to run the image in the background.
docker run -d hello-world
The -p parameter is used to export the image port.
docker run -it -p 1453:80 nginx
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.
docker run -v "File-directory-path":/usr/share/nginx/html:ro -it -p 1453:80 nginx
Various operating system commands can be used to pass the current directory to image.
docker run -v %cd%:/usr/share/nginx/html:ro -it -p 1453:80 nginx
docker run -v $(pwd):/usr/share/nginx/html:ro -it -p 1453:80 nginx
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.
docker ps -a
The top command is used for container operations.
docker top container-id
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.
docker stop container-id
Force Stop Running Container
The kill command is used to force stop the running container.
docker kill container-id
Start Stopped Container
The start command is used to run the stopped container.
docker start container-id
Which Docker command lets us attach to a running container?
The attach command is used to make the running container interactive.
docker attach container-id
Run Container Interactively
The ai parameter is used to interactively run the stopped container.
docker start -ai container-id
Get Information About Docker Container
The inspect command is used to get information about the container.
docker inspect container-id
Check Docker Resource Usage
The stats command is used for resource usage information of the running container.
docker stats container-id
Docker Log Information
The logs command is used for the log information of the container.
docker logs container-id
Remove Docker Container
The rm command is used to remove docker container.
docker rm container-id
The –rm parameter is used to automatically delete the container after completing its operation.
docker run --rm hello-world
Remove All Docker Containers
The below commands can be used to remove all docker containers.
docker rm $(docker ps -aq)
docker sytem prune
NOTE: To remove the running container, it must first be stopped.
NOTE: There are multiple commands in the Docker that perform an operation.
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.
docker build -t image-name:version -f Dockerfile-File
If the dockerfile is in the same folder, just use a dot(.).
docker build -t image-name:version .
Create a Dockerfile
Create the following file as Dockerfile.
LABEL Vendor="SEZER" License=MIT Version=0.1
CMD ["echo", "Image created."]
Create the image with the command below.
docker build -t image:0.1 .
Run the images by taking the ID of the image.
docker run image-id
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.
COPY . /var/www/java
RUN javac file.java
CMD ["java", "file"]
NOTE: You can create images according to your need by using the instructions above.
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.