{"id":719,"date":"2023-10-05T11:29:07","date_gmt":"2023-10-05T11:29:07","guid":{"rendered":"https:\/\/blog.vavensoft.com\/?p=719"},"modified":"2023-10-05T11:29:07","modified_gmt":"2023-10-05T11:29:07","slug":"docker-cli","status":"publish","type":"post","link":"https:\/\/blog.vavencloud.com\/?p=719","title":{"rendered":"Exploring Docker Architecture and Command Usage"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Component of docker architecture<\/h1>\n\n\n\n<p>Docker uses a client-server architecture, which allows you to interact with Docker through a command-line interface (CLI) or a graphical user interface (GUI) while the Docker daemon (server) does the actual work of managing containers. Here&#8217;s an overview of the Docker architecture:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/blog.vavensoft.com\/wp-content\/uploads\/2023\/10\/docker-3.png\" alt=\"\" class=\"wp-image-786\" style=\"width:730px;height:auto\"\/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Docker Client<\/strong><\/p>\n\n\n\n<p>This is the interface that a user interacts with. It&#8217;s usually a command-line tool (CLI) or a GUI. When you issue a command like &#8220;run a container,&#8221; the Docker Client translates it into a request to the Docker Daemon.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Docker Daemon<\/strong><\/p>\n\n\n\n<p>The Docker Daemon is a background process running on your system. It&#8217;s responsible for the heavy lifting of container management. It listens for API requests from the Docker Client and acts on them. It can create, run, and manage containers, as well as handle tasks like image management, containerizing, data volumes, and network configuration.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Docker Registry<\/strong><\/p>\n\n\n\n<p> Docker images, which are important snapshots of applications and their dependencies, are stored in Docker Registries. These are like online repositories where you can upload, share, and download container images. Docker Hub is a popular public registry, but you can also have private ones for your organization.<\/p>\n\n\n\n<p><strong>Default Registry:<\/strong>&nbsp;Docker comes with a default registry called the Docker Hub. It\u2019s like a public library that contains a vast collection of images for various programming languages and platforms. When you request an image in Docker, it first checks the Docker Hub to see if it\u2019s available there. This is like going to a public library to find a book.<\/p>\n\n\n\n<p><strong>Private Registry:<\/strong>&nbsp;Just as you can have your personal book collection at home, you can also have your private Docker registry. This is like having your own library where you store images that are specific to your needs, like custom applications or configurations. You can configure<\/p>\n\n\n\n<p>commands:<br>to log to the docker hub,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker login<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-verse\">Username:\nPassword:<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Docker <strong><strong>Images<\/strong><\/strong><\/h2>\n\n\n\n<p>Images in Docker serve as templates or snapshots of a file system with a specific set of files and configurations.<\/p>\n\n\n\n<p><strong>Application Dependencies:<\/strong>&nbsp;Images specify all the libraries, binaries, and resources required for an application to run. This includes everything from the base operating system to any additional software packages or libraries that an application needs to run.<\/p>\n\n\n\n<p><strong>Launch Processes:<\/strong>&nbsp;Images define the initial command or process that should be executed when a container is started. This command often kicks off the main application or service within the container.<\/p>\n\n\n\n<p><strong>Commands<\/strong><\/p>\n\n\n\n<p>To see all the images present in your local machine run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker images<\/code><\/pre>\n\n\n\n<p>To find out images in Docker Hub.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker search &lt;image_name&gt;<\/code><\/pre>\n\n\n\n<p>To download\/pull Images from Docker Hub to your local machine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker pull &lt;image_name&gt;<\/code><\/pre>\n\n\n\n<p>To push a Docker image to a registry i.e. Docker Hub<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker push &lt;image_name&gt;:&lt;tag&gt;<\/code><\/pre>\n\n\n\n<p>To add or change tags for a Docker image<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker tag &lt;source_image&gt;&#91;:&lt;source_tag&gt;] &lt;target_image&gt;&#91;:&lt;target_tag&gt;]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&lt;source_image&gt;- image id of that image which you want to tag.<\/li>\n\n\n\n<li>:&lt;source_tag&gt;- If the source image has a  tag, you can specify it here. If not leave it default to latest. used when you want to change the tag.<\/li>\n\n\n\n<li>&lt;target_image&gt;This is the new name you want to give to your image <\/li>\n\n\n\n<li>&lt;target_tag&gt;-tag you want to give to your image<\/li>\n<\/ul>\n\n\n\n<p>To get detailed information about a Docker image, including its layers and metadata.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker image inspect &lt;image_name\/id&gt;:&lt;tag&gt;<\/code><\/pre>\n\n\n\n<p>To remove a Docker Image you can use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker rmi &lt;image_name_or_id&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Docker Build<\/h2>\n\n\n\n<p>Docker Build is a core feature of Docker Engine used extensively in software development. At its core, it&#8217;s a command that facilitates the creation of Docker images. This process involves taking your application&#8217;s source code, along with necessary dependencies and configuration, and packaging it into a standalone, runnable image.<\/p>\n\n\n\n<p>Docker begins with a Dockerfile.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Docker File<\/h2>\n\n\n\n<p>A Dockerfile is like a set of instructions written in plain text. It tells a computer what to do step by step to create a special kind of package called an \u201cimage.\u201d This image can contain everything an application needs to run.<\/p>\n\n\n\n<p>So, with a Dockerfile, you can automate the process of building this package. It\u2019s like giving your computer a recipe to follow. It reads the Dockerfile and carries out each instruction to create the image you want. This makes it easier to create and manage these packages for your applications.<\/p>\n\n\n\n<p>Here are the most common types of instructions used to build a Docker File,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM base_image:tag  # A base image use to create the container \n\nMAINTAINER=\"Your Name\"  # author\/owner name \n\nWORKDIR \/app   # Set the working directory inside the container where instruction will be executed\n\nCOPY source_path destination_path   # Copy files from the host into the container\n\nRUN apt-get update &amp;&amp; apt-get install -y package1 package2   # Executes a command during the image build process. Commonly used for installing packages and dependencies.\n\nEXPOSE port_number   # Expose ports for the container (for runtime). Informs Docker that the container will listen on the specified port at runtime. It doesn't actually publish the port.\n\nENV VAR_NAME=value   # Sets environment variables inside the container.\n\nCMD &#91;\"executable\", \"arg1\", \"arg2\"]  # It defines the default command to run when a container is started. It can be overridden at runtime.only one CMD per Dockerfile.\n\nVOLUME &#91;\"\/data\"]  # Define a volume for data storage, it helps in creating a mount point and marks it for storing persistent data.\n\nSHELL &#91;\"\/bin\/bash\", \"-c\"]   # Execute a shell command during build..\n\nENTRYPOINT &#91;\"executable\", \"arg1\", \"arg2\"]   # Set the entry point to a script. The ENTRYPOINT instruction specifies the command that should be run when the container starts. It is often used for specifying the main executable or entry point of your application.<\/code><\/pre>\n\n\n\n<p><strong>Building images on your local machine<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>let&#8217;s create a docker file to run Hello World<\/li>\n\n\n\n<li> the first step is to create a docker file in a particular folder named Dockerfie <\/li>\n\n\n\n<li>Edit the docker file and write a code to build an image <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM ubuntu\nMAINTAINER &lt;owner_name&gt;\nRUN apt-get update\nCMD &#91;\"echo\",\"Hello World\"]<\/code><\/pre>\n\n\n\n<p>build your image from the docker file using the below command <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#when you are present in tne same directory where dockerfile present \ndocker build &lt;image_name&gt;:&lt;tag&gt; .\n#when you are in different directory yauneed to specify thepath of your dockerfile\ndocker build \/path\/to\/dockerfile\/ &lt;image_name&gt;:&lt;tag&gt; <\/code><\/pre>\n\n\n\n<p>Use the <strong>docker images<\/strong> command to see the image that you build. you will find your image with the name that you give and a unique Image ID.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Containers<\/strong><\/h2>\n\n\n\n<p>In Docker, containers are lightweight, self-contained packages for running software. They include everything an application needs, such as code, libraries, and settings. Act as an executable instance of Docker images.<\/p>\n\n\n\n<p><strong>Images:<\/strong>&nbsp;Docker images are like blueprints that contain all the instructions for creating a container. They include the application code, dependencies, and configuration.<\/p>\n\n\n\n<p><strong>Containers<\/strong>: Containers are the actual running instances of Docker images. When you start a Docker container, you\u2019re essentially using an image to create a live environment to run your application. Each container is isolated from others, so they don\u2019t interfere with each other.<\/p>\n\n\n\n<p><strong>Isolation<\/strong>: Docker containers are isolated from each other, much like how objects in code are encapsulated and don\u2019t affect each other\u2019s data.<\/p>\n\n\n\n<p><strong>Commands<\/strong><\/p>\n\n\n\n<p>To show all the docker processes actively running containers &#8220;ps stands for process status&#8221;,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker ps<\/code><\/pre>\n\n\n\n<p>To see the all containers running in the background<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker ps -a<\/code><\/pre>\n\n\n\n<p>This command is used to run a container with the help of an image.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --name &lt;container_name&gt; &lt;image_id\/name&gt; <\/code><\/pre>\n\n\n\n<p>This command is used to view detailed information about a container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker inspect &lt;container_name\/id&gt;<\/code><\/pre>\n\n\n\n<p>Start containers from an image <br>&#8211;name defines the name of the containers<br>-d define container to run in detach mode i.e. running in the background<br>-it defines the continuous running of the containers<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --name &lt;container_name&gt; -it -d &lt;imag_ID\/Name&gt;<\/code><\/pre>\n\n\n\n<p>Start, Stop, and restart Containers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker start &lt;container_ID\/Name&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stop &lt;container_ID\/Name&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>docker restart &lt;container_ID&gt;<\/code><\/pre>\n\n\n\n<p>Runs a command inside a running container.<br>To go inside the container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker exec -it &lt;container_ID&gt; &lt;command&gt;\ndocker attach &lt;container_ID&gt;<\/code><\/pre>\n\n\n\n<p>To view details of containers<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker inspect &lt;container_ID&gt;<\/code><\/pre>\n\n\n\n<p>To remove\/delete container.<br>-f for forcefully removing the container<br>Removes all stopped containers, freeing up disk space<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker rm &lt;container_ID&gt;\ndocker container prune<\/code><\/pre>\n\n\n\n<p>to display the logs of the container,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker logs &lt;container_ID&gt;<\/code><\/pre>\n\n\n\n<p>Copies files from your local machine to a container, or vice versa.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker cp &lt;local_path&gt; &lt;container_ID&gt;:&lt;container_path&gt;<\/code><\/pre>\n\n\n\n<p>To create a volume to store data, volume is basically used to store the data outside the container<br>-v used to specify a volume to be mounted to the container <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -v &lt;volume_name&gt;:&lt;container_path&gt; &lt;image&gt;<\/code><\/pre>\n\n\n\n<p>Shows real-time resource usage statistics of a container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats &lt;container_ID&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Registries<\/strong><\/h2>\n\n\n\n<p>A registry is a library or a storage place for images, which are the building blocks of containers.<\/p>\n\n\n\n<p><strong>Default Registry:<\/strong>&nbsp;Docker comes with a default registry called the Docker Hub. It\u2019s like a public library that contains a vast collection of images for various programming languages and platforms. When you request an image in Docker, it first checks the Docker Hub to see if it\u2019s available there. This is like going to a public library to find a book.<\/p>\n\n\n\n<p><strong>Private Registry:<\/strong>&nbsp;Just as you can have your personal book collection at home, you can also have your private Docker registry. This is like having your own library where you store images that are specific to your needs, like custom applications or configurations. You can configure<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Docker engine<\/strong><\/h2>\n\n\n\n<p>The Docker Engine is like the heart of Docker, where everything happens. It\u2019s the software that makes containers work on your computer.<\/p>\n\n\n\n<p><strong>Docker Daemon:<\/strong>&nbsp;It runs on your computer and does all the heavy lifting. It\u2019s responsible for creating, managing, and running containers.<\/p>\n\n\n\n<p><strong>Docker Client<\/strong>: It\u2019s a command-line tool you use to tell the Docker Daemon what to do. You give it special commands, and it communicates with the Daemon to make things happen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Docker compose<\/h2>\n\n\n\n<p>Docker Compose is a valuable tool for defining and running multi-container Docker applications. It simplifies the process of configuring and managing complex applications by utilizing a YAML file to specify the setup of your application\u2019s various services. This configuration file acts as a blueprint, and with a single command, you can create and launch all the services according to the defined configuration.<\/p>\n\n\n\n<p>The significant benefit of using Docker Compose is that you can specify your application&#8217;s entire setup in a single file, which you keep at the root of your project&#8217;s repository. This file can be version-controlled, making it easy for others to collaborate on your project. All someone needs to do is clone your repository and start the application using Docker Compose. As a result, you may find many projects on platforms like GitHub and GitLab adopting this approach.<\/p>\n\n\n\n<p>command to install docker on Ubuntu <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install docker-compose<\/code><\/pre>\n\n\n\n<p>To check the version of the docker <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose --version<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Docker-compose.yml File<\/h3>\n\n\n\n<p>Docker Compose is the tool we use for setting up our local development environments. To define how the containers work together, Docker Compose relies on a file called <code>docker-compose.yml<\/code>. This file specifies important details, such as which images are needed, which ports should be made available, whether the containers can access the host&#8217;s files, and what commands to execute when they begin running. In essence, it&#8217;s a blueprint that describes how everything should work together.<\/p>\n\n\n\n<p>Running docker-compose <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose up -d<\/code><\/pre>\n\n\n\n<p>To see running container and their state with ports.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose ps<\/code><\/pre>\n\n\n\n<p>To check the logs of the docker-compose use the command: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose logs<\/code><\/pre>\n\n\n\n<p>To stop, pause, and  unpause the running  docker-compose: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose stop\ndocker-compose pause\ndocker-compose unpause<\/code><\/pre>\n\n\n\n<p>To remove the containers, networks, and volumes associated with this containerized environment, use this\u00a0command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose down<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Component of docker architecture Docker uses a client-server architecture, which allows you to interact with Docker through a command-line interface (CLI) or a graphical user interface (GUI) while the Docker daemon (server) does the actual work of managing containers. Here&#8217;s an overview of the Docker architecture: Docker Client This is the interface that a user [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":765,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation-tools"],"_links":{"self":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts\/719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=719"}],"version-history":[{"count":0,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts\/719\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}