• Docker CLI is a command-line tool that allows you to interact with the Docker daemon.
  • To use the Docker CLI, you need to have Docker installed on your system.
  • To view the list of available Docker CLI commands, you can run docker --help or docker -h.

Docker prebuild sample container

  • You can run a prebuilt sample container using the following command:

    docker run -d -p 80:80 docker/getting-started:latest
    • -d: Run the container in detached mode.
    • -p 80:80: Map port 80 of the host machine to port 80 of the container.
    • docker/getting-started:latest: The name of the image to run.
  • To view the running containers, you can run docker ps.

  • To stop a running container, you can run docker stop <container_id>. Issuing the ‘SIGTERM’ signal to the main process of the container.

  • To kill a running container, you can run docker kill <container_id>. Issuing the ‘SIGKILL’ signal to the main process of the container.

  • To remove a container, you can run docker rm <container_id>.

Docker Images VS Containers

  • Docker Images: A Docker image is a read-only template that contains a set of instructions for creating a container. It is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files.

  • Docker Containers: A Docker container is a runnable instance(read and write) of a Docker image. It is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files.

  • Container is an instance of an image.

  • Image defines the environment inside the container.

Docker Commands Execution

  • List the running containers:

    docker ps
  • Run a container in detached mode:

    docker run -d <image_name>
  • To execute a command inside a running container, you can use the docker exec command:

    docker exec -it <container_id> <command>
    • -it: Interactive mode.
    • <container_id>: The ID of the container.
    • <command>: The command to execute.
  • Example:

    docker exec <container_id> netstat -ltnp

‘netstat -ltnp’ command is used to display all listening ports.

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1/nginx: master pro
tcp        0      0 :::80                   :::*                    LISTEN      1/nginx: master pro
  • To open shell session inside a running container, you can use the docker exec command:

    docker exec -it <container_id> /bin/sh

Docker Logs

  • To view the logs of a container, you can use the docker logs command:

    docker logs <container_id>

Multiple Containers

  • To run multiple containers, you can use the docker run command multiple times with different images.

  • Remember, Docker is very lightweight. It’s normal to run many containers on a single host machine.

  • Each container is isolated from the others, and they can communicate with each other if needed.

  • Each container should be on different ports.

  • Example:

    docker run -d -p 80:80 docker/getting-started:latest
    docker run -d -p 81:80 docker/getting-started:latest
    docker run -d -p 8080:80 docker/getting-started:latest

Docker Stats

  • To view all containers stats, you can use the docker stats command:

    docker stats
  • To view the resource usage of a container, you can use the docker stats command:

    docker stats <container_id>