Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker exec command

The docker exec command allows you to run commands inside a running Docker container. This is useful for various tasks such as debugging, checking logs, or modifying files within the container. This tutorial will guide you through the basics of using the docker exec command.

  1. Prerequisites:

    • Install Docker on your system
    • Have a running container that you want to execute commands in. If you don't have one, you can use the following example command to run a container with an interactive shell:
    docker run -it --name example-container ubuntu:20.04 /bin/bash
    

    This command will start a new container based on the ubuntu:20.04 image and open an interactive shell session. The --name flag is used to assign a name to the container for easier reference.

  2. Run a command inside the running container:

    To execute a command inside the running container, use the following syntax:

    docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
    

    For example, to run the ls command inside the example-container, use:

    docker exec example-container ls
    

    This will display the list of files and directories in the container's working directory.

  3. Run an interactive command inside the running container:

    Some commands require user input or an interactive session. To run an interactive command, use the -i (interactive) and -t (TTY) options:

    docker exec -it CONTAINER COMMAND [ARG...]
    

    For example, to open an interactive shell session inside the example-container, use:

    docker exec -it example-container /bin/bash
    

    This will allow you to interact with the container's file system and run commands as if you were working directly inside the container.

  4. Run a command as a different user:

    By default, docker exec runs commands as the root user. If you want to run a command as a different user, use the -u or --user option followed by the desired username or UID:

    docker exec -u USER CONTAINER COMMAND [ARG...]
    

    For example, to run the ls command as the nobody user inside the example-container, use:

    docker exec -u nobody example-container ls
    
  5. Detach from the exec process:

    If you want to run a command in the background and detach from the process, use the -d or --detach option:

    docker exec -d CONTAINER COMMAND [ARG...]
    

    For example, to run a script named background_script.sh in the background inside the example-container, use:

    docker exec -d example-container /bin/bash background_script.sh
    

    This will start the script in the background and return control to the host's terminal.

In this tutorial, we covered the basics of using the docker exec command to run commands inside a running Docker container. The docker exec command is a powerful tool for managing and interacting with containers, allowing you to perform tasks such as debugging, checking logs, or modifying files.

  1. How to Use Docker Exec Command:

    • Description: The docker exec command allows you to run commands inside a running Docker container. It provides a way to interact with a container's filesystem and execute processes within the container.
    • Code Example:
      docker exec <container_id_or_name> <command>
      
  2. Running Commands in Docker Containers with Exec:

    • Description: Docker exec enables you to execute commands as if you were inside the container, providing a way to troubleshoot, inspect, or modify the container's state.
    • Code Example:
      docker exec -it <container_id_or_name> ls /app
      
  3. Docker Exec Command Examples:

    • Description: Various examples showcasing different use cases of the docker exec command.
    • Code Examples:
      docker exec -it <container_id_or_name> bash
      docker exec -it <container_id_or_name> ps aux
      
  4. Interactive Mode with Docker Exec:

    • Description: The -it flag allows you to interact with the container in real-time, providing an interactive shell.
    • Code Example:
      docker exec -it <container_id_or_name> sh
      
  5. Executing Commands in Running Docker Containers:

    • Description: Docker exec works with running containers, allowing you to execute commands without stopping or restarting the container.
    • Code Example:
      docker exec <container_id_or_name> echo "Hello, Docker!"
      
  6. Docker Exec and Detached Mode:

    • Description: While usually used interactively, Docker exec can also run commands in the background (detached mode) with the -d flag.
    • Code Example:
      docker exec -d <container_id_or_name> my_background_process
      
  7. Passing Environment Variables with Docker Exec:

    • Description: You can pass environment variables to the command executed inside the container using the -e flag.
    • Code Example:
      docker exec -e MY_VAR=value <container_id_or_name> my_command
      
  8. Executing Scripts with Docker Exec:

    • Description: Docker exec allows you to run scripts within the container, providing a way to automate tasks.
    • Code Example:
      docker exec <container_id_or_name> /path/to/script.sh
      
  9. Docker Exec and Multiple Commands:

    • Description: You can execute multiple commands in a single docker exec invocation.
    • Code Example:
      docker exec <container_id_or_name> sh -c "echo 'Command 1' && echo 'Command 2'"