Docker Basic Commands

WHAT TO KNOW - Sep 10 - - Dev Community

<!DOCTYPE html>





Docker Basic Commands: A Comprehensive Guide

<br> body {<br> font-family: sans-serif;<br> line-height: 1.6;<br> }<br> h1, h2, h3 {<br> color: #333;<br> }<br> pre {<br> background-color: #f0f0f0;<br> padding: 10px;<br> border-radius: 5px;<br> font-family: monospace;<br> overflow-x: auto;<br> }<br> img {<br> max-width: 100%;<br> height: auto;<br> }<br>



Docker Basic Commands: A Comprehensive Guide



Docker is a powerful platform for developing, deploying, and running applications in containers. It simplifies the process of creating and managing containerized applications, making them portable and scalable. Mastering basic Docker commands is essential for anyone working with Docker, whether you're a developer, DevOps engineer, or system administrator.



This article serves as a comprehensive guide to understanding and using essential Docker commands. We'll cover the key concepts behind Docker and then dive into practical examples of commonly used commands. By the end, you'll have a solid foundation for working with Docker and efficiently managing your containerized applications.



Understanding Docker Concepts



Before we delve into the commands, let's define some key concepts:


  • Container: A lightweight, self-contained package that includes everything an application needs to run: code, libraries, dependencies, and configuration. Think of it as a virtual environment for your application.
  • Image: A read-only template that contains the specifications for a container. It's like a blueprint that defines the components and configurations of the container.
  • Dockerfile: A text file that contains instructions for building a Docker image. It specifies the base image, dependencies, and configuration settings for your container.
  • Registry: A repository where Docker images are stored and shared. Docker Hub is a popular public registry, but you can also create private registries for your organization.

Docker Architecture Diagram


Essential Docker Commands


  1. Docker Version

This command displays the Docker client and server versions:

docker version

Client: Docker Engine - Community
Version: 20.10.12
API version: 1.41
Go version: go1.16.15
Git commit: a2c2808
Built: Wed Oct 27 15:07:24 2021
OS/Arch: linux/amd64
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: a2c2808
Built: Wed Oct 27 15:04:48 2021
OS/Arch: linux/amd64
Experimental: true


  1. Docker Search

To search for Docker images in the Docker Hub registry, use the following command:

docker search
  <image_name>

NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 14504 [OK]
wordpress Official WordPress Docker image. 3976 [OK]
mysql Official MySQL Docker image. 4833 [OK]
mongo Official MongoDB Docker image. 3913 [OK]
redis Official Redis Docker image. 3356 [OK]
node Official Node.js Docker image. 3078 [OK]
ubuntu Ubuntu is a Debian-based Linux distribution. 11004 [OK]
python Official Python Docker image. 3753 [OK]
memcached Official Memcached Docker image. 1502 [OK]
postgresql Official PostgreSQL Docker image. 2950 [OK]


3. Docker Pull



This command downloads a Docker image from a registry (e.g., Docker Hub):


docker pull
   <image_name>
    :
    <tag>
     ```
{% endraw %}

     <pre>
Using default tag: latest
latest: Pulling from library/nginx
...
Status: Downloaded newer image for nginx:latest
</pre>
     <h3>
      4. Docker Run
     </h3>
     <p>
      This command creates and starts a container from a specified image:
     </p>
{% raw %}

     ```bash
docker run -d -p 80:80 nginx:latest
 <p>
  Here's a breakdown of the flags:
 </p>
 <ul>
  <li>
   <code>
    -d
   </code>
   : Detached mode, runs the container in the background.
  </li>
  <li>
   <code>
    -p 80:80
   </code>
   : Port mapping, exposes container port 80 to the host's port 80.
  </li>
  <li>
   <code>
    nginx:latest
   </code>
   : Image name and tag.
  </li>
 </ul>
 <p>
  This command will create a container based on the `nginx:latest` image, run it in the background, and expose the container's port 80 to the host's port 80.
 </p>
 <h3>
  5. Docker Ps
 </h3>
 <p>
  This command lists all running containers:
 </p>
 ```bash

docker ps



     <pre>
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS         PORTS                    NAMES
48422912d802   nginx:latest               "nginx -g 'daemon of…"   4 seconds ago     Up 3 seconds    0.0.0.0:80-&gt;80/tcp           nginx-web
</pre>
     <h3>
      6. Docker Ps -a
     </h3>
     <p>
      This command lists all containers, both running and stopped:
     </p>


     ```bash
docker ps -a
 <pre>

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48422912d802 nginx:latest "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp nginx-web
4d758e12a45a wordpress:latest "docker-entrypoint.s…" 30 seconds ago Exited (0) 2 seconds ago wordpress-db


7. Docker Logs



This command displays the logs of a running container:


 ```bash

docker logs

```

  <pre>

172.17.0.2 - - [28/Sep/2023:14:35:10 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.5790.171 Safari/537.36"
172.17.0.2 - - [28/Sep/2023:14:35:11 +0000] "GET /favicon.ico HTTP/1.1" 404 184 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.5790.171 Safari/537.36"
172.17.0.2 - - [28/Sep/2023:14:35:12 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.5790.171 Safari/537.36"


8. Docker Exec



This command executes a command inside a running container:


  ```bash

docker exec -it

bash



       <p>
        This command will open a bash shell inside the container with the given ID.
       </p>
       <h3>
        9. Docker Stop
       </h3>
       <p>
        This command stops a running container:
       </p>


       ```bash
docker stop
       <container_id>
        ```


        <h3>
         10. Docker Start
        </h3>
        <p>
         This command starts a stopped container:
        </p>


        ```bash
docker start
        <container_id>
         ```


         <h3>
          11. Docker Restart
         </h3>
         <p>
          This command restarts a running container:
         </p>


         ```bash
docker restart
         <container_id>
          ```


          <h3>
           12. Docker Kill
          </h3>
          <p>
           This command forcefully stops a running container, unlike `docker stop` which sends a graceful shutdown signal:
          </p>


          ```bash
docker kill
          <container_id>
           ```


           <h3>
            13. Docker Rm
           </h3>
           <p>
            This command removes a stopped container:
           </p>


           ```bash
docker rm
           <container_id>
            ```


            <h3>
             14. Docker Rmi
            </h3>
            <p>
             This command removes a Docker image:
            </p>


            ```bash
docker rmi
            <image_id>
             ```


             <h3>
              15. Docker Images
             </h3>
             <p>
              This command lists all Docker images on your system:
             </p>


             ```bash
docker images
         <pre>

REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 838436d4a9cb 2 weeks ago 134MB
wordpress latest 30e7d5737967 2 weeks ago 1.38GB


16. Docker Build



This command builds a Docker image from a Dockerfile:


         ```bash

docker build -t

:

.



               <p>
                The
                <code>
                 .
                </code>
                at the end of the command specifies the current directory as the context for the build.
               </p>
               <h3>
                17. Docker Push
               </h3>
               <p>
                This command pushes a Docker image to a registry:
               </p>


               ```bash
docker push
               <image_name>
                :
                <tag>
                 ```


                 <h3>
                  18. Docker Pull (with Registry)
                 </h3>
                 <p>
                  To pull an image from a private registry, you need to provide the registry URL. For example:
                 </p>


                 ```bash
docker pull
                 <registry_url>
                  /
                  <image_name>
                   :
                   <tag>
                    ```


                    <h3>
                     19. Docker Compose
                    </h3>
                    <p>
                     Docker Compose is a tool for defining and managing multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes of your application. To use Docker Compose, you need to install it and define a
                     <code>
                      docker-compose.yml
                     </code>
                     file in your project directory. Here's a basic example:
                    </p>


                    ```yaml
version: "3.7"

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: "password"
                <p>
                 You can then use the following commands to manage your application:
                </p>
                <ul>
                 <li>
                  <code>
                   docker-compose up -d
                  </code>
                  : Start and run containers in detached mode.
                 </li>
                 <li>
                  <code>
                   docker-compose down
                  </code>
                  : Stop and remove containers, networks, and images.
                 </li>
                 <li>
                  <code>
                   docker-compose logs
                  </code>
                  : View logs from all containers.
                 </li>
                 <li>
                  <code>
                   docker-compose exec
                   <service_name>
                    <command/>
                   </service_name>
                  </code>
                  : Execute a command inside a container.
                 </li>
                </ul>
                <h2>
                 Conclusion
                </h2>
                <p>
                 This article covered the essential Docker commands for building, running, managing, and interacting with containerized applications. By mastering these commands, you gain the ability to efficiently work with Docker and leverage its benefits for developing, deploying, and scaling your applications.
                </p>
                <p>
                 Remember, these commands are just the foundation. As you delve deeper into Docker, you'll encounter more advanced commands and tools for specific use cases. Docker's documentation is a valuable resource for exploring further.
                </p>
                <p>
                 Happy containerizing!
                </p>
               </tag>
              </image_name>
             </registry_url>
            </tag>
           </image_name>
          </tag>
         </image_name>
        </image_id>
       </container_id>
      </container_id>
     </container_id>
    </container_id>
   </container_id>
  </container_id>
 </container_id>
</tag>
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player