Docker Basic Commands

WHAT TO KNOW - Sep 9 - - Dev Community

<!DOCTYPE html>





Docker Basic Commands: A Comprehensive Guide

<br> body {<br> font-family: sans-serif;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3 { text-align: center; } pre { background-color: #f5f5f5; padding: 10px; border-radius: 5px; overflow-x: auto; } code { font-family: monospace; } img { display: block; margin: 20px auto; max-width: 100%; } </code></pre></div> <p>



Docker Basic Commands: A Comprehensive Guide



Docker has revolutionized the way we build, ship, and run applications. It provides a lightweight and portable environment for containerizing applications, ensuring consistency and ease of deployment across different systems. This guide will explore the essential Docker commands you need to master to leverage its power effectively.



Understanding Docker Basics



Before diving into commands, let's understand the core concepts:



  • Container:
    A lightweight, isolated environment for running applications. It packages everything needed for an application to run, including code, libraries, dependencies, and system tools.

  • Image:
    A template for creating containers. It defines the container's specifications, including operating system, libraries, and software.

  • Dockerfile:
    A text file containing instructions for building a Docker image. It describes each layer of the image, specifying the base image, dependencies, and configuration.

  • Docker Hub:
    A cloud-based registry for sharing and downloading Docker images.


Essential Docker Commands


  1. Docker Installation

Before using Docker commands, ensure you have Docker installed on your system. You can download and install Docker from the official website ( https://www.docker.com/products/docker-desktop ). Once installed, open a terminal or command prompt.

  • Listing Images: docker images
    
    docker images
    

    This command lists all the Docker images available on your system. It displays the image name, tag, image ID, image size, and the date created.

    Docker Images List

  • Searching for Images: docker search
    
    docker search nginx
    

    This command searches the Docker Hub registry for images matching the specified keyword. In this example, it searches for images related to "nginx".

  • Pulling Images: docker pull
    
    docker pull nginx:latest
    

    This command downloads an image from the Docker Hub registry to your local system. You need to specify the image name and tag (e.g., "nginx:latest").

  • Running Containers: docker run
    
    docker run -d -p 80:80 nginx:latest
    

    This command runs a container based on the specified image ("nginx:latest"). The -d flag runs the container in detached mode (background), and -p 80:80 maps port 80 of the container to port 80 of the host system.

  • Listing Running Containers: docker ps
    
    docker ps
    

    This command displays a list of all running containers, including container ID, image name, created time, status, and port mappings.

  • Listing All Containers (Running and Stopped): docker ps -a
    
    docker ps -a
    

    This command shows both running and stopped containers.

  • Stopping Containers: docker stop
    
    docker stop 
    

    This command gracefully stops a running container by sending a SIGTERM signal to the container process.

  • Starting Containers: docker start
    
    docker start 
    

    This command starts a stopped container.

  • Restarting Containers: docker restart
    
    docker restart 
    

    This command stops and then restarts a container.

  • Removing Containers: docker rm
    
    docker rm 
    

    This command removes a container from your system. Make sure the container is stopped before removing it.

  • Removing Images: docker rmi
    
    docker rmi 
    

    This command removes an image from your system. It will also delete any containers based on that image.

  • Viewing Logs: docker logs
    
    docker logs 
    

    This command displays the logs generated by a container.

  • Executing Commands: docker exec
    
    docker exec -it  bash
    

    This command executes a command inside a running container. The -it flags provide interactive access to the container's shell (in this case, bash).

  • Building Images: docker build
    
    docker build -t my-nginx .
    

    This command builds a Docker image based on a Dockerfile. The -t my-nginx flag assigns a tag to the image, and the dot (.) specifies the current directory as the build context.

  • Tagging Images: docker tag
    
    docker tag my-nginx:latest my-nginx:v1.0
    

    This command creates a new tag for an existing image. It allows you to have multiple tags for the same image, which can be useful for versioning.

  • Pushing Images: docker push
    
    docker push my-nginx:v1.0
    

    This command uploads an image to a Docker registry (like Docker Hub). You need to specify the image name and tag (e.g., "my-nginx:v1.0").

    Using Docker Compose

    Docker Compose is a powerful tool for defining and managing multi-container Docker applications. It allows you to define your application's services, networks, and volumes in a YAML file, making it easier to manage complex applications.

    Docker Compose Overview

  • Installing Docker Compose

    Install Docker Compose using the following command (replace "latest" with the appropriate version if necessary):

    
    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    

  • Writing a Docker Compose File

    Create a docker-compose.yml file in your project directory. For example:

    
    version: "3.9"
  • services:
    web:
    image: nginx:latest
    ports:
    - "80:80"

    db:
    image: mysql:latest
    environment:
    MYSQL_ROOT_PASSWORD: "password"
    ports:
    - "3306:3306"


    This file defines two services: "web" and "db". The "web" service uses the "nginx:latest" image and exposes port 80. The "db" service uses the "mysql:latest" image, sets the root password to "password", and exposes port 3306.


    1. Starting the Application

    
    docker-compose up -d
    

    This command starts the services defined in the docker-compose.yml file in detached mode (background).


  • Stopping the Application
    
    docker-compose down
    

    This command stops and removes all containers and networks created by Docker Compose.

    Best Practices for Using Docker

    • Keep Images Lean: Use multi-stage builds to minimize image size by removing unnecessary files and dependencies.
    • Use Official Images: Leverage official images from Docker Hub whenever possible for proven reliability and security.
    • Use Docker Compose: Organize your application's services and dependencies with Docker Compose for efficient development and deployment.
    • Follow Security Guidelines: Use Docker best practices for security, such as limiting user privileges and using trusted images.
    • Automate Deployment: Integrate Docker into your CI/CD pipeline for automated builds, testing, and deployment.

      Conclusion

      This article covered the fundamental Docker commands, providing you with a solid foundation for working with containers. By mastering these commands, you can build, ship, and run applications more efficiently and effectively. Remember to follow Docker best practices to ensure the reliability, security, and efficiency of your applications. As you delve deeper into Docker, explore advanced concepts like Docker networks, volumes, and orchestration tools like Docker Swarm or Kubernetes to enhance your containerization skills.
  • . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    Terabox Video Player