{"id":48360,"date":"2025-02-08T08:12:19","date_gmt":"2025-02-08T08:12:19","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=48360"},"modified":"2025-07-12T05:38:46","modified_gmt":"2025-07-12T05:38:46","slug":"docker-exec","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/docker-exec\/","title":{"rendered":"Docker commands Guide &#8211; docker exec with examples"},"content":{"rendered":"\n<p>Here\u2019s a <strong>complete tutorial<\/strong> on the <code>docker exec<\/code> command, including its <strong>purpose<\/strong>, <strong>how to use it<\/strong>, and a <strong>comprehensive list of examples<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is <code>docker exec<\/code>?<\/strong><\/h2>\n\n\n\n<p><code>docker exec<\/code> is used to <strong>run a command inside a running Docker container<\/strong>. Unlike <code>docker attach<\/code>, which connects to the container\u2019s primary process, <code>docker exec<\/code> allows you to execute new commands in real-time within the container without affecting the main process.<\/p>\n\n\n\n<p><strong>Key Features of <code>docker exec<\/code>:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Run <strong>interactive commands<\/strong> (like a bash shell) in a running container.<\/li>\n\n\n\n<li>Useful for <strong>debugging<\/strong>, <strong>maintenance<\/strong>, and <strong>inspection<\/strong>.<\/li>\n\n\n\n<li>Supports running <strong>single commands<\/strong> or opening an <strong>interactive shell<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Syntax<\/strong><\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">docker<\/span> <span class=\"hljs-selector-tag\">exec<\/span> <span class=\"hljs-selector-attr\">&#91;OPTIONS]<\/span> <span class=\"hljs-selector-tag\">CONTAINER<\/span> <span class=\"hljs-selector-tag\">COMMAND<\/span> <span class=\"hljs-selector-attr\">&#91;ARG...]<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Common Options:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-i<\/code> \u2192 Keep <strong>STDIN open<\/strong> (interactive mode).<\/li>\n\n\n\n<li><code>-t<\/code> \u2192 Allocate a <strong>pseudo-TTY<\/strong> (useful for interactive commands like <code>bash<\/code>).<\/li>\n\n\n\n<li><code>-e<\/code> \u2192 Set environment variables inside the container.<\/li>\n\n\n\n<li><code>--privileged<\/code> \u2192 Run the command with elevated privileges inside the container.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Examples of <code>docker exec<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Run a Simple Command in a Container<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">docker exec my_container ls \/<span class=\"hljs-keyword\">var<\/span>\/log\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This command lists the contents of <code>\/var\/log<\/code> in <code>my_container<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Open an Interactive Shell (<code>bash<\/code>)<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec -it my_container bash\n<\/code><\/span><\/pre>\n\n\n<p>This opens an interactive bash shell inside <code>my_container<\/code>.<\/p>\n\n\n\n<p>If the container doesn\u2019t have <code>bash<\/code> installed, use <code>sh<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec -it my_container sh\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Run a Command with Elevated Privileges<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec --privileged -it my_container bash\n<\/code><\/span><\/pre>\n\n\n<p>This runs the bash shell with elevated privileges, allowing access to restricted parts of the filesystem.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Execute a Command with Environment Variables<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec -e ENV_VAR=value my_container printenv ENV_VAR\n<\/code><\/span><\/pre>\n\n\n<p>This sets <code>ENV_VAR<\/code> inside the container and prints its value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Run a Command in a Detached Mode<\/strong><\/h3>\n\n\n\n<p>You can run a command without waiting for it to finish:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec -d my_container touch \/tmp\/newfile.txt\n<\/code><\/span><\/pre>\n\n\n<p>This creates a file <code>\/tmp\/newfile.txt<\/code> inside the container and immediately detaches.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Inspect Running Processes<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container ps aux\n<\/code><\/span><\/pre>\n\n\n<p>This lists all running processes in the container.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Monitor Resource Usage<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container top\n<\/code><\/span><\/pre>\n\n\n<p>This displays resource usage (CPU, memory) inside the container.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Check the Container\u2019s Hostname<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container hostname\n<\/code><\/span><\/pre>\n\n\n<p>Returns the hostname of the container.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. Inspect Networking Information<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container ifconfig\n<\/code><\/span><\/pre>\n\n\n<p>Displays the network configuration of the container.<\/p>\n\n\n\n<p>If <code>ifconfig<\/code> isn\u2019t available, use <code>ip<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container ip a\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. Check Disk Space<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container df -h\n<\/code><\/span><\/pre>\n\n\n<p>Shows the container&#8217;s disk usage.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11. Debug an Application Log<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">docker exec my_container tail -f \/<span class=\"hljs-keyword\">var<\/span>\/log\/app.log\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This command follows the application log inside the container.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12. Interact with a Database in a Container<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>MySQL Example<\/strong>:<\/h4>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec -it my_mysql mysql -u root -p\n<\/code><\/span><\/pre>\n\n\n<p>This opens a MySQL client inside the <code>my_mysql<\/code> container.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>PostgreSQL Example<\/strong>:<\/h4>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec -it my_postgres psql -U postgres\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>13. Restart a Service Inside the Container<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container service nginx restart\n<\/code><\/span><\/pre>\n\n\n<p>Restarts the NGINX service inside the container.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>14. Copy a File from One Directory to Another<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container cp \/path\/to\/source \/path\/to\/destination\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>15. Run a Health Check<\/strong><\/h3>\n\n\n\n<p>If the container has a custom health check script:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker exec my_container \/usr\/local\/bin\/health_check.sh\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Combining <code>docker exec<\/code> with Other Commands<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>docker exec<\/code> + <code>docker logs<\/code>:<\/strong><br>Debug a service by checking its logs and running commands in the container. <code>docker logs my_container docker exec -it my_container bash<\/code><\/li>\n\n\n\n<li><strong><code>docker exec<\/code> + <code>docker cp<\/code>:<\/strong><br>Copy a file into the container and then run it. <code>docker cp .\/script.sh my_container:\/tmp\/script.sh docker exec my_container sh \/tmp\/script.sh<\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>List of Common <code>docker exec<\/code> Commands<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Command<\/strong><\/th><th><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>docker exec my_container ls \/var\/log<\/code><\/td><td>List files in <code>\/var\/log<\/code><\/td><\/tr><tr><td><code>docker exec -it my_container bash<\/code><\/td><td>Open an interactive bash shell<\/td><\/tr><tr><td><code>docker exec -it my_container sh<\/code><\/td><td>Open an interactive shell<\/td><\/tr><tr><td><code>docker exec my_container ps aux<\/code><\/td><td>List running processes<\/td><\/tr><tr><td><code>docker exec -e VAR=value my_container env<\/code><\/td><td>Set and print an environment variable<\/td><\/tr><tr><td><code>docker exec --privileged my_container bash<\/code><\/td><td>Run bash with elevated privileges<\/td><\/tr><tr><td><code>docker exec -d my_container touch \/tmp\/file<\/code><\/td><td>Run a command in detached mode<\/td><\/tr><tr><td><code>docker exec my_container tail -f \/var\/log\/app.log<\/code><\/td><td>Follow the log file inside the container<\/td><\/tr><tr><td><code>docker exec my_mysql mysql -u root -p<\/code><\/td><td>Open MySQL client inside a MySQL container<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices for Using <code>docker exec<\/code>:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use <code>docker exec<\/code> for debugging<\/strong> and interactive tasks.<\/li>\n\n\n\n<li><strong>Combine with <code>docker logs<\/code><\/strong> to troubleshoot container issues.<\/li>\n\n\n\n<li><strong>Avoid running critical commands<\/strong> unless you know the container\u2019s behavior.<\/li>\n\n\n\n<li><strong>Use <code>-it<\/code> for interactive tasks<\/strong> (like opening a shell).<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors and Solutions<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>&#8220;OCI runtime exec failed: exec failed: container not running&#8221;<\/strong><br>\u2192 Ensure the container is running by checking with <code>docker ps<\/code>. Start it with <code>docker start CONTAINER_NAME<\/code>.<\/li>\n\n\n\n<li><strong>&#8220;command not found&#8221;<\/strong><br>\u2192 The specified command may not be installed in the container. Use <code>sh<\/code> or <code>bash<\/code> to explore and confirm.<\/li>\n\n\n\n<li><strong>&#8220;permission denied&#8221;<\/strong><br>\u2192 Use <code>--privileged<\/code> or check file permissions inside the container.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a complete tutorial on the docker exec command, including its purpose, how to use it, and a comprehensive list of examples. What is docker exec? docker exec is used to run a command inside a running Docker container. Unlike docker attach, which connects to the container\u2019s primary process, docker exec allows you to execute&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[4862],"tags":[],"class_list":["post-48360","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=48360"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48360\/revisions"}],"predecessor-version":[{"id":48362,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48360\/revisions\/48362"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=48360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=48360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=48360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}