AWS Logo
Menu
Docker Commands - Part 3

Docker Commands - Part 3

Master Docker Commands - Part 3: Enhance your skills with advanced commands for troubleshooting, security, and performance optimization.

Published Sep 4, 2024
👉 docker container restart: To restart a Docker container, you can use the docker restart command. This command stops and then starts an already running container.
  • CONTAINER [CONTAINER...]: This is the name or ID of the container(s) you want to restart.
For example, to restart a container named my_container you would run:
If you have multiple containers you want to restart, you can specify their names or IDs separated by spaces:
This will stop and then start all specified containers. It’s worth noting that the docker restart command is not supported for Swarm services. If you're using Docker Swarm, you should use the docker service update command to update a service, which includes restarting its containers if necessary.
To restart all containers on your Docker host, you can use a combination of docker ps -q to get the list of running container IDs and docker restart to restart them.
Let’s break down what’s happening here:
  • docker ps -q: This command lists the IDs of all running containers. The -q option is used to only display the container IDs, without any headers or additional information.
  • $(...): This is command substitution in Bash. It allows the output of one command (in this case, docker ps -q) to be used as the input for another command (docker restart).
  • docker restart: This command restarts one or more containers. When used with command substitution, it restarts all containers whose IDs are obtained from docker ps -q.
So, when you run the command docker restart $(docker ps -q), Docker restarts all running containers on your host.
Restarting a container can be useful for applying configuration changes, refreshing the container’s environment, or troubleshooting issues. However, it’s essential to consider any impact on running services or applications when performing a restart.
👉 docker rename: To rename a Docker container, you can use the docker renamecommand.
Let’s say you have a container named old_container that you want to rename to new_container.
👉 docker container cp: The docker container cp command allows you to copy files or directories between a container and the local filesystem.
  • [OPTIONS]: This is where you can specify various options to customize the behavior of the docker container cp command. Common options include -L, --follow-link to follow symbolic links and -v, --verbose to display verbose output.
  • CONTAINER: This is the name or ID of the container you want to copy files to or from.
  • SRC_PATH: This is the path to the file or directory you want to copy. When copying from the container to the local filesystem, this path is relative to the root of the container's filesystem. When copying from the local filesystem to the container, this path is relative to the current directory on the local filesystem.
  • DEST_PATH: This is the destination path where the files or directories will be copied. When copying from the container to the local filesystem, this path is relative to the current directory on the local filesystem. When copying from the local filesystem to the container, this path is relative to the root of the container's filesystem.
For example, to copy a file named file.txt from a container named my_container to the current directory on the local filesystem, you would run:
Conversely, to copy a file named file.txt from the local filesystem to a container named my_container, you would run:
Copy files from container to local path:
Similarly, you can copy directories as well. Just specify the directory path instead of the file path.
It’s important to note that the docker container cp command only works with running containers. If you need to copy files to or from a stopped container, you can use the docker cp command instead.
👉 docker containe diff: The docker container diff command is used to inspect changes made to the filesystem of a running container compared to its base image or filesystem snapshot. It shows the differences in the form of added (A), deleted (D), or modified (C) files and directories.
  • CONTAINER: This is the name or ID of the running container you want to inspect.
For example, to see the changes made to the filesystem of a container named my_container, you would run:
This command will output a list of filesystem changes in the container.
For instance, if you’ve created a file named example.txt within the container, the output might look like this:
The output format typically consists of lines starting with A, C, or D, followed by the path to the affected file or directory. Here's what each prefix represents:
  • A: Added file or directory.
  • C: Changed file or directory (modified).
  • D: Deleted file or directory.
By using docker container diff, you can easily inspect the changes made to a running Docker container's filesystem compared to its base image. This can be helpful for debugging, troubleshooting, or understanding the effects of various commands or processes running inside the container.
👉 docker container export: The docker container export command allows you to export the contents of a container's filesystem as a tar archive. This command is useful when you want to save the state of a container's filesystem and share it with others or use it as a backup.
  • [OPTIONS]: This is where you can specify various options to customize the export process. Currently, there are no specific options for the docker container export command.
  • CONTAINER: This is the name or ID of the container you want to export.
For example, to export the contents of a container named my_container to a tar archive named my_container.tar, you would run:
This command creates a tar archive containing the entire filesystem of the my_container container and saves it as my_container.tar.
Once the export is complete, you can share the tar archive with others, transfer it to another system, or keep it as a backup.
It’s important to note that the docker container export command only exports the filesystem contents of the container. It does not include metadata such as container configuration, environment variables, or networking settings. Additionally, it does not include any running processes or container state. If you need to export the entire state of a container, including its configuration and state, you may want to consider using the docker export command instead.
👉 docker container import: Like export, we can import the container with a single command. You must have the exported file to the target server.
Once you have the tarball containing the container’s filesystem, you can import it as an image using the docker import command.
  • [OPTIONS]: This is where you can specify various options to customize the import process. Common options include -c to apply Dockerfile instructions during import and -m to add a description or annotation.
  • file|URL|-: This is the path to the tarball containing the exported filesystem. You can specify either a local file path, a URL, or - to read from standard input.
  • [REPOSITORY[:TAG]]: This is the name and optional tag you want to assign to the imported image.
For example:
This command imports the tarball container.tar as a Docker image tagged as my_image:latest.
After importing the image, you can verify that it was successfully imported by listing the Docker images on your system.
This command lists all Docker images, including the one you just imported.
👉 docker container pause: The docker container pause command is used to pause all processes within a running container. This can be useful for temporarily suspending the execution of processes within a container without stopping it completely.
  • CONTAINER [CONTAINER...]: This is the name or ID of one or more running containers that you want to pause.
For example, let’s say you have a running container named my_container and you want to pause it:
This command will pause all processes running within the my_container container. The processes will remain paused until you unpause them using the docker container unpause command.
To pause multiple containers at once, you can specify their names or IDs separated by spaces:
This will pause all specified containers.
Once paused, the container’s processes will stop executing, but the container itself will remain in the running state. You can confirm that the container is paused by checking its status with the docker ps command.
The paused containers will have their status shown as PAUSED.
It’s important to note that while a container is paused, it will not consume any CPU resources. However, memory usage and network connections will remain active. Additionally, any processes or applications running within the container will be temporarily halted.
👉 docker container unpause: The docker container unpause command is used to unpause all processes within a paused container, allowing them to resume execution.
  • CONTAINER [CONTAINER...]: This is the name or ID of one or more paused containers that you want to unpause.
For example, to unpause a paused container named my_container, you would run:
This command will resume execution of all processes within the my_container container.
To unpause multiple containers at once, you can specify their names or IDs separated by spaces:
This will unpause all specified containers.
Once unpause, the container’s processes will resume execution as usual.
It’s important to note that if a container was not previously paused, running docker container unpause on it will have no effect. Additionally, while a container is paused, its memory usage and network connections remain active, but its processes are halted.
👉 docker container kill: The docker container kill command is used to forcefully stop one or more running containers by sending a SIGKILL signal to their main process. This command is typically used when you need to immediately stop a container without allowing it to perform any cleanup tasks or gracefully shut down its processes.
  • [OPTIONS]: This is where you can specify various options to customize the kill operation. Some common options include -s, --signal to specify a different signal to send and -f, --force to force the kill operation even if the container is not running.
  • CONTAINER [CONTAINER...]: This is the name or ID of one or more running containers that you want to kill.
For example, to kill a container named my_container, you would run:
This command sends a SIGKILL signal to the main process of the my_container container, forcefully stopping it.
To kill multiple containers at once, you can specify their names or IDs separated by spaces:
This will kill all specified containers.
It’s important to note that using docker container kill is a forceful way to stop containers, and it should be used with caution. It does not allow containers to perform any cleanup tasks, so data loss or other undesirable consequences may occur if used indiscriminately. If possible, it's generally preferable to use docker container stop to gracefully stop containers first, allowing them to perform any necessary cleanup actions before stopping completely.
👉 docker container stats: The docker container stats command is used to display a live stream of resource usage statistics for one or more running containers. It provides real-time information about CPU usage, memory usage, network I/O, and block I/O of the specified container(s).
  • [OPTIONS]: This is where you can specify various options to customize the output of the docker container stats command. Some common options include --format to specify the output format and --no-stream to disable the live streaming and display a single snapshot of container stats.
  • [CONTAINER...]: This is the name or ID of one or more running containers that you want to display statistics for. If no containers are specified, statistics for all running containers will be displayed.
For example, to display live resource usage statistics for a container named my_container, you would run:
This command will continuously stream resource usage statistics for the my_container container until you manually stop it (e.g., by pressing Ctrl+C).
To display statistics for multiple containers at once, you can specify their names or IDs separated by spaces:
This will display live resource usage statistics for all specified containers.
The output of docker container stats typically includes columns such as CONTAINER ID, NAME, CPU %, MEM USAGE / LIMIT, MEM %, and more. It provides valuable insights into the resource consumption of containers, which can be useful for monitoring and troubleshooting purposes.
👉 docker container wait: The docker container wait command is used to block until one or more containers stop, and then it outputs the exit status of those containers. It waits for the specified container(s) to exit and then returns the exit code of the container's main process.
  • CONTAINER [CONTAINER...]: This is the name or ID of one or more containers that you want to wait for.
For example, to wait for a container named my_container to exit and then output its exit status, you would run:
This command will block until the my_container container stops, and then it will output the exit status of its main process.
You can also wait for multiple containers at once by specifying their names or IDs separated by spaces:
This will wait for all specified containers to exit and then output their exit statuses.
The docker container wait command is often used in scripting or automation workflows where you need to wait for certain containers to finish their tasks before proceeding with further actions. It's particularly useful in scenarios where you need to coordinate actions across multiple containers or wait for a specific container to complete a task before continuing.
👉 docker container update: The docker container update command is used to update the configuration of one or more containers. It allows you to modify various container settings, such as CPU shares, memory limits, environment variables, and more, for containers that are already running.
  • [OPTIONS]: This is where you can specify various options to customize the update process. Some common options include --cpu-shares, --memory, --restart, --env, and more.
  • CONTAINER [CONTAINER...]: This is the name or ID of one or more containers that you want to update.
For example, let’s say you have a running container named my_container, and you want to update its CPU shares to give it more CPU resources:
This command updates the CPU shares of the my_container container to 512.
You can also update multiple containers at once by specifying their names or IDs separated by spaces:
This command updates the restart policy of container1, container2, and container3 to always restart if they exit.
The docker container update command allows you to dynamically adjust container configurations without needing to stop and recreate the containers. It can be useful for fine-tuning container resource allocations, applying new environment variables, or changing other container settings on-the-fly.
 

Comments