Docker 101: Commands

PART IV

Docker 101: Commands

note: BEFORE RUNNING COMMAND ON CLI, ALWAYS RUN YOUR DOCKER DESKTOP

COMMANDS

docker run

Start a new container from an image. If the image is not present locally, it will be downloaded from the registry and run:

docker run IMAGE_NAME
  • IMAGE_NAME is the name of the Docker image you want to run. This command creates a new container based on the specified image and starts it.
  1. Interactive Mode: Start a Docker container with an interactive terminal.

    • -it: These options specify an interactive terminal. The -i option allows you to interact with the container, and the -t option allocates a pseudo-TTY, enabling interaction with the terminal.

    • Running this command results in Docker creating a new container from the specified image, and you'll be connected to an interactive shell within the container. This is useful for running commands and directly interacting with the container's environment.

        docker run -it myapp
      
    • To exit the container's interactive terminal, you can typically type exit or use the key combination Ctrl + D.

    • It will provide you with a terminal of that container to work with. Each container has its own CLI to interact with.

  2. Detached Mode: Running in the Background

    • If you want a container to run in the background for an extended period, which is a common use case for servers, you can use the -d flag:

        docker run -d apline ping www.civo.com
      

Listing Running Containers:

docker ps
docker container ls
  • These commands provide a list of all containers currently running on your system. They offer information about the container's ID, name, status, and more.

  • Adding the -a flag will also show all removed containers.

Listing Available Docker Images:

  • To list all Docker images downloaded or built on your system, use the following command:
docker images
  • This command displays information about the image's repository, tag, size, and creation date.

Downloading a Docker Image from a Registry:

  • To download a Docker image from a container registry, such as Docker Hub, use the docker pull command:
docker pull IMAGE_NAME
  • Replace IMAGE_NAME with the name of the image you want to download.

  • Download specific image version using:

docker pull IMAGE_NAME:16.0.0

Building a Docker Image from a Dockerfile:

  • To create a Docker image from a Dockerfile, use the docker build command:
docker build -t IMAGE_NAME:TAG PATH_TO_DOCKERFILE
  • This command allows you to specify the image name, an optional tag, and the path to the Dockerfile.

Stopping a Running Container:

  • To gracefully stop a running container, use the docker stop command with the container's ID or name of the container:
docker stop CONTAINER_ID

Starting a Stopped Container:

  • To restart a previously stopped container, use the docker start command with the container's ID or name of the container as an argument.:
docker start CONTAINER_ID

Running a Command in a Running Container:

  • The docker exec command is used to execute a command within a running Docker container

  • command is used to execute a command in a running Docker container. It allows you to run a specific command within a container's environment, providing more control and flexibility.

docker exec -it CONTAINER_ID COMMAND
  • Use this command to run a specific command within the container. The -it flags allow you to interact with the container.

  • Here are some common examples of using docker container exec:

    1. Running a shell session within a running container (bash in this case):

       docker container exec -it CONTAINER_NAME_OR_ID bash
      

      This command opens an interactive terminal session within the specified container using the Bash shell.

    2. Running a specific command within a running container:

       docker container exec CONTAINER_NAME_OR_ID ls -l /path/to/directory
      

      This command runs the ls -l /path/to/directory command within the container.

    3. Executing a command with environment variable(s):

       docker container exec -e ENV_VAR=VALUE CONTAINER_NAME_OR_ID some_command
      

      You can pass environment variables to the command using the -e option.

Using docker container exec is helpful when you need to perform tasks or execute commands within a running container without the need to start a new shell session. It's commonly used for debugging, maintenance, and interacting with specific containers in a Docker environment.

Viewing the Logs of a Container:

  • To view the logs of a specific container, use the docker logs command:
docker logs CONTAINER_ID
  • This command is helpful for troubleshooting and debugging. You can also specify a time range for log retrieval using the --since flag:
dockers logs --since [time] CONTAINER_ID
docker logs --since 5s 9694

Removing a Container:

  • To delete a stopped container, use the docker rm command with the container's ID:
docker rm CONTAINER_ID

Docker Inspect:

  • The docker inspect command is used to retrieve detailed information about Docker objects such as containers, images, volumes, networks, and more. This command provides a wealth of metadata and configuration details for the specified Docker object.

    Here is the basic syntax for the docker inspect command:

      docker inspect [OPTIONS] NAME|ID [NAME|ID...]
    
  • Here are some common uses of docker inspect:

    1. Inspect a Container:

      To retrieve detailed information about a running or stopped container, you can use the docker inspect command with the container's name or ID.

       docker inspect my_container
      

      This will provide a JSON-formatted output with information about the specified container.

    2. Inspect an Image:

       docker inspect my_image
      

      This will provide detailed information about the specified image, including its configuration and history.

    3. Inspect a Volume:

       docker inspect my_volume
      

      This will provide information about the specified volume, including its mount point and options.

    4. Inspect a Network:

       docker inspect my_network
      

      This will provide details about the specified network, including its configuration and connected containers.

The docker inspect command is a powerful tool for obtaining detailed information about Docker objects, which can be useful for troubleshooting, debugging, and understanding the configuration of your Docker resources. The output is in JSON format, so you can use tools like jq to parse and filter the information as needed.

Docker Prune:

  • The docker prune command is used to clean up and remove various Docker resources that are no longer in use. It helps reclaim disk space and maintain a tidy Docker environment by removing unused containers, networks, volumes, and images.

  • Here's the basic syntax:

      docker system prune [OPTIONS]
    
  • There are different subcommands for different types of resources you can prune, each with its specific options:

    1. Prune All Resources:

      To remove all types of unused Docker resources (containers, networks, volumes, and images), use the following command:

       docker system prune
      
    2. Prune All Images:

      To remove all unused images, use the -a option:

       docker image prune -a
      

      This will remove all images that are not currently used by any containers.

    3. Prune Containers:

      To remove all stopped containers, you can use:

       docker container prune
      

      This is particularly useful when you have many stopped containers that you no longer need.

    4. Prune Networks:

      To remove unused networks, use:

       docker network prune
      

      This will clean up networks that are not connected to any containers.

    5. Prune Volumes:

      To remove unused volumes, you can use:

       docker volume prune
      

      This will help free up space taken by volumes that are no longer associated with containers.

Be cautious when using docker prune commands, as they permanently delete resources. Ensure you understand the potential impact and have backups of important data.

Remember to review the confirmation prompt before proceeding with a prune operation. You can use the --force the option to bypass the confirmation prompt, but do so with caution.

SOME EXAMPLE

  • The steps of how Docker operates when running a container.

    1. Search for the Image Locally

    2. Image Not Found Locally

    3. Download from Docker Registry (Docker Hub)

    4. Running the Image

DOCKER COMMAND WITH LINUX COMMAND

  • Note: Since images are Linux-based, we can run Linux commands in them.

  • Therefore, in your Docker command, you can also pass Linux commands, which will run after the container is formed.

For example:

  1. docker run alpine ping www.google.com: This command runs a Docker container based on the Alpine Linux image and executes the Linux ping command to ping www.google.com.

  2. docker run -d alpine echo Dev: This command initiates a Docker container using the Alpine Linux image in detached mode (-d) and runs the Linux command echo Dev within the container.

RUN CONTAINER ON LOCAL PORT

used to run a Docker container with Nginx and map a port from your host to a port inside the container.

docker run -d -p 8080:80 nginx

-p OUTTER_PORT:INNER_PORT specifies the port mapping. In this case, -p 8080:80 maps port 8080 on your host to port 80 inside the container.

Without container also nginx will listen to port 80. Similarly we will configure this communication by defining

DOCKER COMMIT

The docker commit command is used to create a new Docker image from changes made to an existing container. It allows you to capture the current state of a container, including any modifications you've made, and save it as a new image. This can be useful when you want to share your changes with others or use a modified container as a base for new containers.

Here's the basic syntax of the docker commit command:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
  • OPTIONS: These are optional options that you can use to customize the commit process.

  • CONTAINER: This is the name or ID of the container you want to commit.

  • REPOSITORY[:TAG]: This specifies the name and optional tag for the new image you're creating.

For example, to commit changes made in a container named "my_container" to a new image named "my_image," you would use the following command:

docker commit my_container my_image

This command will create a new Docker image named "my_image" based on the changes you've made in "my_container."

Keep in mind a few things when using docker commit:

  1. Image Layering: Docker images are built in layers, and when you use docker commit, it creates a new image layer on top of the original image. This means that the resulting image includes all the layers from the original image, as well as the changes you've made.

  2. Reproducibility: It's generally better to define your changes and configuration in a Dockerfile so that others can reproduce the image creation process with certainty. docker commit can be less predictable, especially when changes are not well-documented.

  3. Sharing Images: Once you've created a new image using docker commit, you can share it with others by pushing it to a Docker registry (like Docker Hub). This allows others to pull and use the modified image.

Using Dockerfiles to define your image's configuration and changes is often considered a best practice as it makes the process more reproducible and maintainable. However, docker commit can be useful for ad-hoc changes or experimenting with containers.

EXAMPLE

WITHOUT COMMITING IF I RE-RUN: CHANGES WILL NOT BE SEEN

AFTER COMMITTING

REMOVING IMAGES (WITH SUBQUERY)

work on linux based terminal