techtutorialsite.com /docker-exec-command/

Docker exec Command | Execute commands in Docker containers

Raunak Jain 7-8 minutes 2021/5/12

You can use the Docker Exec Command to execute commands inside running Docker containers. If you already have a Docker container running and you want to execute an executable command inside it, you can use the Docker exec command. However, the only constraint is that the target container’s primary process (PID = 1) should be running.

Suppose you have an Ubuntu container running in the background. And you want to create a file inside the container but you don’t have access to the bash of the container. In such a case, you can use the Docker exec command to run a touch command inside the container. This will create your new file.

In this article, we will discuss the ins and outs of the Docker exec command. By the end of this tutorial, you will be able to execute commands inside Docker containers easily. You can check out our complete and free Docker Tutorial.

As already discussed, the Docker exec command lets you run commands inside a running Docker container. The command that you want to execute will run in the default working directory of the container. You can also set the directory where you want to execute the command using the -w option.

Also, it’s important that the command that you mention is an executable command. You cannot run a chained or quoted command using the Docker exec command. Consider the example below.

The command docker exec -it container “echo x && echo y” will not work. However, docker exec -it container sh -c “echo x && echo y” will definitely work. This is so because sh -c “echo x && echo y” is treated as a single command.

Let’s check out the syntax of the Docker container exec command.

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

You can also omit the container keyword from the above command and it will still output the same result.

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

Let’s check out a few options that you can use along with this command.

NameDescription
--detach , -dYou can use this option to execute a command in the background mode.
--detach-keysIf you want to override a key sequence in order to detach a container, you can use this option.
--env , -eIt can be used to set the environment variables.
--env-fileYou can use it to read from a file of environment variables.
--interactive , -iYou can keep standard input open even if you don’t attach it.
--tty , -tIt allows you to allocate a pseudo-TTY.
--workdir , -wIf you want to specify the default working directory, you can use this option.

In the following example, we will list all the files inside a running container. Let’s use the Docker run command to run a container session first.

$ docker run -it --name=myubuntu ubuntu bash
Run a Docker Container
Run a Docker Container

Now, let’s open another terminal keeping this container session running and use the command below to list all the files.

$ docker exec myubuntu ls
Docker exec command
Docker exec command

This command will run the ls command inside the myubuntu running container.

Docker Exec Bash

One of the most important use-cases of the Docker exec command is to run a bash shell associated with the container.

To execute a bash associated with a container, you need to use the interactive and pseudo-TTY options (-it) along with the container name or ID. And you need to use the /bin/bash (path of the terminal) as the command. This will run a bash session of the container.

You can use any one of the following commands to start a bash session.

$ docker exec -it myubuntu /bin/bash
$ docker exec -it myubuntu bash
Docker exec it bash
Docker exec it bash

There are three important file descriptors in Linux. They are STDIN, STDOUT, and STDERR. When we use the -i (interactive) option, we bind the standard input of our host machine to the standard input of the container’s process.

Execute Commands as Root User

There might be some cases where you want to run commands inside a container as a root user. The Docker exec command allows you to do so by specifying the -u (user) option.

Hence, if you want to execute commands inside containers as a root user, you can use the user option along with the Docker exec command with a user value 0. This is for the root user.

$ docker exec -u 0 <container-name> <command>

Let’s execute the whoami command inside the container as a root user.

$ docker exec -u 0 myubuntu whoami
Execute Commands as a Root User
Execute Commands as a Root User

Execute Multiple Commands using Docker Exec

You can also execute multiple commands using the docker exec command by specifying the bash process along with the -c option. This will read the command as a string.

$ docker exec <container-name-or-ID> bash -c "first-command ; second-command ; third-command"

Please note that single-quotes might not work on your terminal. Hence, always prefer to use double quotes.

Let’s try to create a file and display all the files.

$ docker exec myubuntu bash -c "touch TechTutorialSite.txt ; ls"
Execute Multiple Commands using Docker exec
Execute Multiple Commands using Docker exec

Specify Directory to Execute Commands

You can use the -w (workdir) option to specify the working directory where you want your commands to be executed. By default, the commands that you execute using the Docker exec command get executed in the root directory or the default working directory that you have set in the Dockerfile. But if you want to change the directory, you can use the -w option.

$ docker exec -w /target/directory <name-of-container> <command>

Let’s try to create a file inside a folder called /usr.

$ docker exec -w /usr myubuntu bash -c "touch TechTutorialSite.txt ; ls"
Execute commands in a specific directory
Execute commands in a specific directory

Difference between Docker Run and Exec Commands

The Docker run command is used to create a container, start it, run commands inside it, and stop it when the execution is completed. However, the Docker exec command is used to run commands inside a running container.

When you create and run a container using the Docker run command and execute a command using it, the container gets stopped once the execution is completed. Let’s see what happens when we use an echo command along with the Docker run command.

$ docker run --name=myubuntu ubuntu echo Hello
Docker run
Docker run

You can see that the container stopped once the execution is completed. But on the otehr hand, when you have a container running, you can use the Docker exec command to run a command inside the container without having the container get stopped.

$ docker ps
$ docker exec myubuntu echo hello
$ docker ps
Docker Run Vs Docker Exec
Docker Run Vs Docker Exec

You can see that the container is still running.

Final Thoughts!

In this Dokcer tutorial, you learned about the Docker exec command which is used to execute commands inside a running Docker container.

If you want to learn more about Docker, check out our complete and free Docker Tutorial.

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::