{"id":122565,"date":"2025-02-11T13:09:18","date_gmt":"2025-02-11T13:09:18","guid":{"rendered":"\/tutorials\/?p=122565"},"modified":"2025-12-02T15:04:35","modified_gmt":"2025-12-02T15:04:35","slug":"docker-exec-command","status":"publish","type":"post","link":"\/ca\/tutorials\/docker-exec-command\/","title":{"rendered":"How to use docker exec command in containers"},"content":{"rendered":"<p>The <strong>docker exec<\/strong> command lets you run new commands inside a Docker container that is already running.<\/p><p>Here&rsquo;s what you can do with the <strong>docker exec<\/strong> command:<\/p><ul class=\"wp-block-list\">\n<li><strong>Access an interactive shell<\/strong>. Gain direct, terminal-like access to a container&rsquo;s environment for real-time debugging.<\/li>\n\n\n\n<li><strong>Execute single commands<\/strong>. Quickly run one-off commands, like listing files or checking a process, without starting a full shell session.<\/li>\n\n\n\n<li><strong>Start background processes<\/strong>. Use the <strong>detach<\/strong> flag to kick off long-running scripts or tasks inside the container.<\/li>\n\n\n\n<li><strong>Set temporary environment variables<\/strong>. Pass new environment variables to a specific command for testing or temporary overrides.<\/li>\n\n\n\n<li><strong>Access logs<\/strong>. Read log files, like <strong>access.log<\/strong> or <strong>error.log<\/strong>, directly from within the container.<\/li>\n\n\n\n<li><strong>Manage internal services<\/strong>. Interact with services running inside the container, such as checking the status of a web server or restarting a process.<\/li>\n\n\n\n<li><strong>Gain administrative access<\/strong>. Run commands as the root user to perform tasks that require elevated privileges, like installing packages.<\/li>\n<\/ul><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-the-docker-exec-command\">What is the docker exec command?<\/h2><p>The <strong>docker exec <\/strong>command is a core part of the Docker command-line interface (CLI) that provides a way to execute processes in a running container.<\/p><p>Unlike commands that create or manage containers themselves, <strong>docker exec<\/strong> operates exclusively on containers that are already active, giving you direct access to their internal environment.<\/p><p>This command is essential for container management because it lets you debug a running application, perform administrative tasks, or inspect its environment without stopping and restarting the container.<\/p><p>In production environments, it&rsquo;s your primary tool for live diagnostics and on-the-fly adjustments.<\/p><p>When you run <strong>docker exec<\/strong>, Docker uses Linux namespaces and <strong>cgroups<\/strong> to execute your command within the container&rsquo;s context. This means it sees the container&rsquo;s filesystem, network, and process space, not the host&rsquo;s.<\/p><p>This isolation ensures that your actions stay contained within that specific container without affecting others or the host system.<\/p><p>You can use <strong>docker exec<\/strong> to open an interactive shell (<strong>bash<\/strong> or <strong>sh<\/strong>) to look at files, check configurations, or install new packages for testing.<\/p><p>Beyond shells, it also handles one-off administrative commands like checking disk usage, modifying configuration files, or verifying that a service is responding correctly.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-the-docker-exec-command-works\">How the docker exec command works<\/h3><p>The <strong>docker exec<\/strong> command works by targeting a running container and executing a specified command within its namespace. The basic syntax of <strong>docker exec<\/strong> is:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec [OPTIONS] CONTAINER_NAME_OR_ID COMMAND [ARG...]<\/pre><ul class=\"wp-block-list\">\n<li><strong>[OPTIONS]<\/strong>. Flags that modify how the command behaves, like running in interactive mode.<\/li>\n\n\n\n<li><strong>CONTAINER_NAME_OR_ID<\/strong>. The specific container you want to run the command in. You can get this by running <strong>docker ps<\/strong>.<\/li>\n\n\n\n<li><strong>COMMAND [ARG&hellip;]<\/strong>. The command you want to run inside the container, along with any arguments it needs.<\/li>\n<\/ul><p>Here are the most common flags you&rsquo;ll use:<\/p><ul class=\"wp-block-list\">\n<li><strong>-i (interactive)<\/strong>. Keeps the standard input (STDIN) open, so you can provide input to the command. This is essential for interactive sessions.<\/li>\n\n\n\n<li><strong>-t (tty)<\/strong>. Allocates a pseudo-TTY (terminal). This makes the terminal session inside the container behave like a real terminal and is almost always used with <strong>-i<\/strong> (as <strong>-it<\/strong>) for a stable, usable shell.<\/li>\n\n\n\n<li><strong>-d (detach)<\/strong>. Runs the command in the background (detached mode). The command starts, and your terminal is immediately freed up, rather than waiting for the command to finish.<\/li>\n\n\n\n<li><strong>-e, &ndash;env (environment variables)<\/strong>. Sets one or more environment variables just for this command. You can use this flag multiple times (for instance, <strong>&ndash;env VAR1=value1 &ndash;env VAR2=value2<\/strong>). <strong>-e<\/strong> is the shorthand for <strong>&ndash;env<\/strong>.<\/li>\n\n\n\n<li><strong>&ndash;privileged<\/strong>. Grants extended privileges to the executed command. This gives the command full access to the host&rsquo;s resources, similar to running as root on the host. Use this with extreme caution as it can compromise container isolation.<\/li>\n\n\n\n<li><strong>-u, &ndash;user<\/strong>. Runs the command as a specific user (by name or UID). This is useful for testing permissions or running commands as a non-root user for security.<\/li>\n\n\n\n<li><strong>-w, &ndash;workdir<\/strong>. Sets the working directory inside the container for the command, overriding the container&rsquo;s default.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-prerequisites-before-using-the-docker-exec-command\">Prerequisites before using the docker exec command<\/h3><p>Before you can use <strong>docker exec<\/strong>, you need a few things in place:<\/p><ul class=\"wp-block-list\">\n<li><a href=\"\/ca\/tutorials\/how-to-install-docker-on-ubuntu\/\">Docker installed and configured<\/a>. The Docker Engine (version <strong>1.3<\/strong> or later) must be installed on your system, and the Docker daemon must be running.<\/li>\n\n\n\n<li><a href=\"\/ca\/tutorials\/how-to-create-docker-container\/\">A running Docker container<\/a>. The command only works on containers that are currently running. You can&rsquo;t use <strong>docker exec<\/strong> on a stopped or paused container.<\/li>\n\n\n\n<li><strong>Container ID or name<\/strong>. You must know the ID or name of the container you want to target. You can find this by running the <strong>docker ps<\/strong> command.<\/li>\n\n\n\n<li><strong>A valid command<\/strong>. The command you want to run (like <strong>ls<\/strong>, <strong>bash<\/strong>, or <strong>apt<\/strong>) must exist within the container&rsquo;s environment.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-practical-examples-of-the-docker-exec-command\">Practical examples of the docker exec command<\/h2><p>Practical examples of the <strong>docker exec<\/strong> command include running a shell in a container, setting environment variables, and executing commands with root privileges. The following sections cover these common tasks.<\/p><p><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> Docker commands often require <strong>sudo<\/strong> privileges by default. To simplify this, you can add your user to the <strong>docker<\/strong> group to run commands without sudo. This guide omits <strong>sudo<\/strong> for clarity, but remember to add it to your commands if you haven&rsquo;t configured the <strong>docker<\/strong> group.\n            <\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-running-a-shell-in-a-container\">Running a shell in a container<\/h3><p>One of the most common uses of <strong>docker exec<\/strong> is to access a running container&rsquo;s shell. This lets you interact with the container&rsquo;s filesystem, execute commands directly, and perform troubleshooting.<\/p><p>To start an interactive shell inside a container, use the <strong>-it<\/strong> flags:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -it container_name_or_id \/bin\/bash<\/pre><p>Here&rsquo;s a breakdown:<\/p><ul class=\"wp-block-list\">\n<li><strong>-it<\/strong>. Combines the <strong>-i<\/strong> (interactive) and <strong>-t<\/strong> (tty) flags to give you a fully interactive terminal session.<\/li>\n\n\n\n<li><strong>\/bin\/bash<\/strong>. The command you&rsquo;re running, which in this case is the Bash shell.<\/li>\n<\/ul><p>If the container doesn&rsquo;t have Bash installed (common in minimal images like Alpine), you can use <strong>sh<\/strong> instead:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -it container_name_or_id \/bin\/sh<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e315187\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e315187\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"95\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/run-shell.png\/w=1024,h=1024,fit=scale-down\" alt=\"Terminal window showing docker exec command with \/bin\/sh\" class=\"wp-image-122575\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>This method is essential for debugging and exploring the internals of a container without stopping it.<\/p><h3 class=\"wp-block-heading\" id=\"h-running-a-single-command-inside-a-container\">Running a single command inside a container<\/h3><p>You can use <strong>docker exec<\/strong> to run a single command inside a running container without opening a full shell session.<\/p><p>For example, to list the files in a container&rsquo;s root filesystem, you can use:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec container_name_or_id ls \/<\/pre><p>This command executes<strong> ls \/<\/strong> inside the specified container, listing the files and directories in the root directory.<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e3174eb\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e3174eb\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"276\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/docker-ls.png\/w=1024,h=1024,fit=scale-down\" alt=\"Terminal window showing docker exec command to list files with ls \/\" class=\"wp-image-122576\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>If you want to view files in a specific directory, just include the path:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec container_name_or_id ls \/app<\/pre><p>This is a quick and efficient way to check file contents or configurations without starting an interactive session.<\/p><h3 class=\"wp-block-heading\" id=\"h-running-background-tasks\">Running background tasks<\/h3><p>The <strong>docker exec<\/strong> command can run tasks in the background using the <strong>-d<\/strong> (detach) flag. This runs the command and immediately returns control of your terminal.<\/p><p>For example, to run a maintenance script in the background:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -d container_name_or_id \/usr\/local\/bin\/maintenance-script.sh<\/pre><p>The <strong>-d<\/strong> flag runs the command in the background, so you can continue using your terminal. This approach is useful for starting long-running processes or non-interactive tasks inside a container.<\/p><h3 class=\"wp-block-heading\" id=\"h-passing-environment-variables\">Passing environment variables<\/h3><p>You can use the <strong>&ndash;env<\/strong> (or <strong>-e<\/strong> shorthand) flag to pass environment variables to a command. This is useful when a command needs specific configuration values.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec --env GREETING=\"Hello, World!\" container_name_or_id sh -c 'echo $GREETING'<\/pre><p>In this case:<\/p><ul class=\"wp-block-list\">\n<li><strong>&ndash;env GREETING=&rdquo;Hello, World!&rdquo;<\/strong>. Defines the environment variable <strong>GREETING<\/strong>.<\/li>\n\n\n\n<li><strong>sh -c &lsquo;echo $GREETING&rsquo;<\/strong>. Starts a shell to execute the <strong>echo<\/strong> command, which then accesses the variable.<\/li>\n<\/ul><p>When executed, the output will display &ldquo;Hello, World!&rdquo;.<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e31903f\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e31903f\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"130\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/exec-env-variable.png\/w=1024,h=1024,fit=scale-down\" alt='Terminal output of docker exec --env command showing \"Hello, World!\"' class=\"wp-image-122578\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You can pass multiple environment variables by repeating the <strong>&ndash;env<\/strong> flag:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -e VAR1=value1 -e VAR2=value2 container_name_or_id command<\/pre><p>    <div class=\"announcement-block announcement-block--warning\">\n        <span class=\"announcement-block__heading\">\n            <svg width=\"24\" height=\"26\" viewBox=\"0 0 24 26\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                d=\"M12 23.5C17.799 23.5 22.5 18.799 22.5 13C22.5 7.20101 17.799 2.5 12 2.5C6.20101 2.5 1.5 7.20101 1.5 13C1.5 18.799 6.20101 23.5 12 23.5ZM13.637 8.65198C13.637 7.74791 12.9041 7.01501 12 7.01501C11.0959 7.01501 10.363 7.74791 10.363 8.65198C10.5335 10.5375 10.875 14.383 10.875 14.383C10.875 15.0043 11.3787 15.508 12 15.508C12.6213 15.508 13.125 15.0043 13.125 14.383V14.38L13.637 8.65198ZM11.9927 16.714C11.3714 16.714 10.8677 17.2177 10.8677 17.839C10.8677 18.4603 11.3714 18.964 11.9927 18.964H12.0073C12.6286 18.964 13.1323 18.4603 13.1323 17.839C13.1323 17.2177 12.6286 16.714 12.0073 16.714H11.9927Z\"\n                fill=\"#BE1025\"\/>\n            <\/svg>\n            Warning        <\/span>\n        <p class=\"announcement-block__content\">\n            <strong>Warning!<\/strong> Be cautious when passing sensitive data with environment variables, as they may be visible to other users on the system through process lists.        <\/p>\n    <\/div>\n\n    \n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-accessing-a-specific-container-s-logs\">Accessing a specific container&rsquo;s logs<\/h3><p>You can use <strong>docker exec<\/strong> to access logs stored within a container by reading the log files directly. This is different from the <strong>docker logs<\/strong> command, which reads the container&rsquo;s standard output (STDOUT) and error (STDERR) streams.<\/p><p>        <div class=\"protip\">\n            <div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                    &#129300; When to use this method                <\/p>\n            <\/div>\n            <p class=\"protip__content\"> Only use <strong>docker exec<\/strong> to read log files when applications write to specific files (for example, <strong>\/var\/log\/app.log<\/strong>) instead of sending them to STDOUT. For standard container logging, always use the <strong>docker logs<\/strong> command instead, as it's more efficient and designed for this purpose.<\/p>\n                    <\/div>\n        \n\n\n\n<\/p><p>For instance, if a web server container stores logs at <strong>\/var\/log\/nginx\/access.log<\/strong>, you can access them with:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec container_name_or_id cat \/var\/log\/nginx\/access.log<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e31ab8b\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e31ab8b\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"132\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/access-logs.png\/w=1024,h=1024,fit=scale-down\" alt=\"Terminal output showing NGINX access log via docker exec\" class=\"wp-image-122579\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>If the log file is large, you can use other tools like <strong>tail<\/strong> to view only the most recent entries:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec container_name_or_id tail -n 50 \/var\/log\/nginx\/access.log<\/pre><h3 class=\"wp-block-heading\" id=\"h-interacting-with-services-inside-containers\">Interacting with services inside containers<\/h3><p><strong>docker exec<\/strong> lets you interact with services running inside a container, such as restarting a service or checking its status.<\/p><p><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> Most Docker containers run a single process and lack init systems, so commands like <strong>service<\/strong> or <strong>systemctl<\/strong> are often unavailable (especially in minimal or Alpine images). The examples below only work in full OS images (like Ubuntu or CentOS) that include these service management tools.\n            <\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-checking-a-service-s-status\"><strong>Checking a service&rsquo;s status<\/strong><\/h3><p>To check the status of a service (in containers that support it), you can use its service management command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec container_name_or_id service nginx status<\/pre><p>This command displays whether the nginx service is active, running, or stopped.<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e31c91a\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e31c91a\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"84\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/nginx-status.png\/w=1024,h=1024,fit=scale-down\" alt=\"Terminal output of docker exec service nginx status command\" class=\"wp-image-122580\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h4 class=\"wp-block-heading\" id=\"h-restarting-a-service\"><strong>Restarting a service<\/strong><\/h4><p>To restart a service inside a container (for example, after changing a config file):<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec container_name_or_id service nginx restart<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e31fc37\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e31fc37\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"79\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/nginx-restart.png\/w=1024,h=1024,fit=scale-down\" alt=\"Terminal output showing docker exec command to restart NGINX\" class=\"wp-image-122581\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>On containers based on modern Linux distributions with systemd, you might need to use <strong>systemctl<\/strong> instead:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec container_name_or_id systemctl restart service_name<\/pre><p>        <div class=\"protip\">\n            <div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                    &#128260; Production best practice                <\/p>\n            <\/div>\n            <p class=\"protip__content\"> Treat containers as immutable units. When you need to restart a service or change configurations, reboot the entire container with <strong>docker restart<\/strong> or, better yet, update your container image and redeploy. This approach maintains consistency, reproducibility, and aligns with modern containerization principles.<\/p>\n                    <\/div>\n        \n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-running-commands-with-root-privileges\">Running commands with root privileges<\/h3><p>By default, <strong>docker exec<\/strong> runs commands as the same user the container was started with.<\/p><p>If your container was built with a non-root USER instruction in the Dockerfile, commands will run as that user and may not have the permissions you need for administrative tasks.<\/p><p>To run commands with root (administrative) privileges when needed, use the <strong>-u root <\/strong>flag. For example, to update package list:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -u root container_name_or_id apt update<\/pre><p>Or to change file ownership:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -u root container_name_or_id chown user:user \/path\/to\/file<\/pre><p>You can also run commands as any specific user by specifying their username or UID:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -u www-data container_name_or_id \/bin\/bash<\/pre><p>Always exercise caution when running commands as root, as improper changes can break your container.<\/p><h2 class=\"wp-block-heading\" id=\"h-troubleshooting-common-issues-with-the-docker-exec-command\">Troubleshooting common issues with the docker exec command<\/h2><p>Troubleshooting common issues with the <strong>docker exec<\/strong> command usually involves checking the container&rsquo;s status or resolving permission errors.<\/p><h3 class=\"wp-block-heading\" id=\"h-container-not-running\"><strong>Container not running<\/strong><\/h3><p>If you receive an error like <strong>&ldquo;Error response from daemon: Container&hellip;is not running&rdquo;<\/strong>, it means the container is stopped. <strong>docker exec<\/strong> only works on running containers.<\/p><p>First, use the <strong>docker ps<\/strong> command to list all active containers. If your container isn&rsquo;t listed, it&rsquo;s not running.<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e323e43\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e323e43\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"82\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/docker-ps.png\/w=1024,h=1024,fit=scale-down\" alt=\"Terminal output showing docker ps command listing containers\" class=\"wp-image-122587\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You can start it using <strong>docker start<\/strong>:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker start container_name_or_id<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a58d1e326102\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a58d1e326102\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"361\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/02\/docker-start-1.png\/w=1024,h=1024,fit=scale-down\" alt=\"Terminal output showing a container started with docker start\" class=\"wp-image-122588\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>If the container is paused, you&rsquo;ll need to unpause it first with <strong>docker unpause container_name_or_id<\/strong>.<\/p><h3 class=\"wp-block-heading\" id=\"h-permission-issues\"><strong>Permission issues<\/strong><\/h3><p>You might encounter permission errors, especially if the container was started with a non-root user. If you try to run a command that requires administrative privileges, it will fail.<\/p><p>Try running the command with root privileges using the <strong>-u root<\/strong> flag:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker exec -u root container_name_or_id command<\/pre><p>Alternatively, you can run the command as a different specific user if you know one with the right access:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  docker exec -u username container_name_or_id command<\/pre><p>If you still encounter permission issues, our guide on <a href=\"\/ca\/tutorials\/how-to-fix-docker-permission-denied-error\/\">resolving Docker permission denied errors<\/a> can help.<\/p><h3 class=\"wp-block-heading\" id=\"h-incorrect-container-id-or-name\"><strong>Incorrect container ID or name<\/strong><\/h3><p>Make sure you&rsquo;re using the correct container ID or name in your command. It&rsquo;s easy to make a typo or reference a container that has been removed.<\/p><p>Use <strong>docker ps<\/strong> to get a fresh list of running containers and their names\/IDs.<\/p><h3 class=\"wp-block-heading\" id=\"h-command-not-found-errors\">Command not found errors<\/h3><p>If the command itself fails (for instance, &ldquo;executable file not found&rdquo; or &ldquo;OCI runtime exec failed&rdquo;), it means the command (like <strong>nano<\/strong> or <strong>vim<\/strong>) isn&rsquo;t installed inside the container.<\/p><p>This is particularly common with minimal images. You&rsquo;ll need to either:<\/p><ul class=\"wp-block-list\">\n<li>Install it using <strong>docker exec -u root container_name apt install package-name<\/strong>.<\/li>\n\n\n\n<li>Use an alternative command that is available, like <strong>vi<\/strong> instead of <strong>nano<\/strong>.<\/li>\n\n\n\n<li>Consider using <strong>docker debug<\/strong> for access to additional debugging tools.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-next-step-deepen-your-docker-knowledge\">Next step: Deepen your Docker knowledge<\/h2><p>Mastering the<strong> docker exec<\/strong> command is a key step in managing containerized applications effectively. It&rsquo;s your primary tool for real-time debugging and administration within running containers.<\/p><p>But it&rsquo;s just one part of the wider Docker ecosystem. To build on what you&rsquo;ve learned and understand how this command fits into the complete container lifecycle &ndash; from building images to managing data and orchestration &ndash; check out our <a href=\"\/ca\/tutorials\/docker-tutorial\/\">Docker tutorial<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The docker exec command lets you run new commands inside a Docker container that is already running. Here&rsquo;s what you can do with the docker exec command: What is the docker exec command? The docker exec command is a core part of the Docker command-line interface (CLI) that provides a way to execute processes in [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ca\/tutorials\/docker-exec-command\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":471,"featured_media":139211,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to use docker exec command in containers","rank_math_description":"Learn the docker exec command to run commands inside running containers. This guide covers syntax, practical examples, and troubleshooting.","rank_math_focus_keyword":"docker exec command","footnotes":""},"categories":[22701,22699],"tags":[],"class_list":["post-122565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/docker-exec-command","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/docker-exec-command","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/docker-exec-command","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/docker-exec-command","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/docker-exec-command","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/docker-exec-command","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/docker-exec-command","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/docker-exec-command","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/122565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/users\/471"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/comments?post=122565"}],"version-history":[{"count":24,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/122565\/revisions"}],"predecessor-version":[{"id":137276,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/122565\/revisions\/137276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media\/139211"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media?parent=122565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/categories?post=122565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/tags?post=122565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}