Docker exec it linux

docker exec

Refer to the options section for an overview of available OPTIONS for this command.

Description

The docker exec command runs a new command in a running container.

The command started using docker exec only runs while the container’s primary process ( PID 1 ) is running, and it is not restarted if the container is restarted.

COMMAND will run in the default directory of the container. If the underlying image has a custom directory specified with the WORKDIR directive in its Dockerfile, this will be used instead.

COMMAND should be an executable, a chained or a quoted command will not work. Example: docker exec -ti my_container «echo a && echo b» will not work, but docker exec -ti my_container sh -c «echo a && echo b» will.

For example uses of this command, refer to the examples section below.

Options

Name, shorthand Default Description
—detach , -d Detached mode: run command in the background
—detach-keys Override the key sequence for detaching a container
—env , -e Set environment variables
—env-file Read in a file of environment variables
—interactive , -i Keep STDIN open even if not attached
—privileged Give extended privileges to the command
—tty , -t Allocate a pseudo-TTY
—user , -u Username or UID (format: [:])
—workdir , -w Working directory inside the container

Examples

Run docker exec on a running container

$ docker run --name ubuntu_bash --rm -i -t ubuntu bash 

This will create a container named ubuntu_bash and start a Bash session.

Next, execute a command on the container.

$ docker exec -d ubuntu_bash touch /tmp/execWorks 

This will create a new file /tmp/execWorks inside the running container ubuntu_bash , in the background.

Next, execute an interactive bash shell on the container.

$ docker exec -it ubuntu_bash bash 

This will create a new Bash session in the container ubuntu_bash .

Next, set an environment variable in the current bash session.

$ docker exec -it -e VAR=1 ubuntu_bash bash 

This will create a new Bash session in the container ubuntu_bash with environment variable $VAR set to “1”. Note that this environment variable will only be valid on the current Bash session.

By default docker exec command runs in the same working directory set when container was created.

$ docker exec -it ubuntu_bash pwd / 

You can select working directory for the command to execute into

$ docker exec -it -w /root ubuntu_bash pwd /root 

Try to run docker exec on a paused container

If the container is paused, then the docker exec command will fail with an error:

$ docker pause test test $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1ae3b36715d2 ubuntu:latest "bash" 17 seconds ago Up 16 seconds (Paused) test $ docker exec test ls FATA[0000] Error response from daemon: Container test is paused, unpause the container before exec $ echo $? 1 

Источник

docker exec

Refer to the options section for an overview of available OPTIONS for this command.

Description

The docker exec command runs a new command in a running container.

The command started using docker exec only runs while the container’s primary process ( PID 1 ) is running, and it is not restarted if the container is restarted.

COMMAND runs in the default directory of the container. If the underlying image has a custom directory specified with the WORKDIR directive in its Dockerfile, this directory is used instead.

COMMAND must be an executable. A chained or a quoted command does not work. For example, docker exec -it my_container sh -c «echo a && echo b» does work, but docker exec -it my_container «echo a && echo b» does not.

For example uses of this command, refer to the examples section below.

Options

Name, shorthand Default Description
—detach , -d Detached mode: run command in the background
—detach-keys Override the key sequence for detaching a container
—env , -e Set environment variables
—env-file Read in a file of environment variables
—interactive , -i Keep STDIN open even if not attached
—privileged Give extended privileges to the command
—tty , -t Allocate a pseudo-TTY
—user , -u Username or UID (format: [:] )
—workdir , -w Working directory inside the container

Examples

Run docker exec on a running container

$ docker run --name mycontainer -d -i -t alpine /bin/sh 

This creates and starts a container named mycontainer from an alpine image with an sh shell as its main process. The -d option (shorthand for —detach ) sets the container to run in the background, in detached mode, with a pseudo-TTY attached ( -t ). The -i option is set to keep STDIN attached ( -i ), which prevents the sh process from exiting immediately.

Next, execute a command on the container.

$ docker exec -d mycontainer touch /tmp/execWorks 

This creates a new file /tmp/execWorks inside the running container mycontainer , in the background.

Next, execute an interactive sh shell on the container.

$ docker exec -it mycontainer sh 

This starts a new shell session in the container mycontainer .

Set environment variables for the exec process (—env, -e)

Next, set environment variables in the current bash session.

By default, the docker exec command, inherits the environment variables that are set at the time the container is created. Use the —env (or the -e shorthand) to override global environment variables, or to set additional environment variables for the process started by docker exec .

The example below creates a new shell session in the container mycontainer with environment variables $VAR_A and $VAR_B set to “1” and “2” respectively. These environment variables are only valid for the sh process started by that docker exec command, and are not available to other processes running inside the container.

$ docker exec -e VAR_A=1 -e VAR_B=2 mycontainer env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=f64a4851eb71 VAR_A=1 VAR_B=2 HOME=/root 

Set the working directory for the exec process (—workdir, -w)

By default docker exec command runs in the same working directory set when the container was created.

$ docker exec -it mycontainer pwd / 

You can specify an alternative working directory for the command to execute using the —workdir option (or the -w shorthand):

$ docker exec -it -w /root mycontainer pwd /root 

Try to run docker exec on a paused container

If the container is paused, then the docker exec command fails with an error:

$ docker pause mycontainer mycontainer $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 482efdf39fac alpine "/bin/sh" 17 seconds ago Up 16 seconds (Paused) mycontainer $ docker exec mycontainer sh Error response from daemon: Container mycontainer is paused, unpause the container before exec $ echo $? 1 

Источник

Docker exec -it

Docker is a container management framework that is free and open source. It allows developers to bundle programs into containers which are standardized executable parts that combine source code with the OS modules and requirements needed to run that program on almost any platform.

Docker framework came up with a console platform that allows us to execute several commands on it to get data from containers. There are some execution commands of dockers as well run and exec. The exec command lets us execute the commands in a running container without opening a new console.

So, this article will be demonstrating the use of exec –it command of docker in Ubuntu 20.04 system.

To the opening of the shell application for the Ubuntu 20.04 system, we need to remove all the old versions of docker along with other packages installed from our system.

So, the apt-get instruction with 4“remove” keywords has been used for this purpose. It asks for our current account password to continue. We have added a password and pressed Enter to continue this process.

While executing, it will ask you to press “y” to continue this process as shown below.

Within a few seconds, the whole docker and its related packages will be removed successfully.

Now, you need to update your Linux system via the apt-get command of the update shown below. It is a necessary step to be followed to avoid any inconvenience.

Make sure to download the docker from its website if you don’t know any other way to install it. The curl instruction shown below must be utilized in this regard.

Now, to install the docker platform, try using the “apt-get” installation command followed by the name of a platform as “docker.io” shown below. It will start calculating all information.

While processing, it requires your affirmation to continue installing docker at your Ubuntu 20.04. You need to press “Y” as an answer to the question.

If the docker cannot be installed at your end using the above command due to some issues, try using the snap package to install it as per the shown below instruction.

You can find out the installed version of docker on your system via the below version command with the keyword “docker”. Now, the docker is installed and ready for use.

The docker “ps” command can be used to get the information regarding all the containers it has in your system. We have been using the sudo privileges to execute the “docker ps” instruction to get this information. We have found out that there is no container in our docker right now.

You need to find out all the available images of docker in your system using the “docker images” instruction demonstrated below. You can see that we have currently 4 images available to be used in our docker system along with their information.

You need to pull these images in your system utilizing the docker pull instruction along with the image name. So, we have pulled 3 images one after another i.e. Ubuntu, busy box, hello-world.

Now, all these images can be utilized at our end. So, we have to run the images to create containers. For this, try making use of the “docker run” instruction along with the name of an image. We have done it for all 3 images as below.

After checking the container information via the docker “ps” instruction, you can see that 3 container images have been created. Now, we can use them by their ID or Names.

Now, we will be looking at the “run” instruction first so that when we use the “exec” command, it will be more clear to you. We have been using the “docker run” instruction with the “—name” option to rename the image “busy box” to “Test1”. Along with that, -it option is used to open the interactive console on our shell.

Now, a container named “Test1” has been running for the image busybox and its console is opened. We have used the list “ls” instruction to display all the contents in a running container “Test1”. When the user exits the container, the container will automatically be removed from the docker queue and the “ps” command will now show it.

Using the docker “ps” instruction, we can see the running container with its status “up” and other information.

Now, we will be taking a look at the “exec –it” instruction in comparison to the “run” instruction. So, start the “Test1” container with a docker start instruction. Then, use the “docker exec –it” instruction along with the container name “Test1”. You have to put any instruction you want to execute after the container name. You will get similar results as you got in the “run” instruction. This means, the “exec –it” instruction allows you to run the instruction within the running container.

Conclusion

The introduction paragraph of this tutorial guides you about the working of docker and how it manages data within the containers. We have discussed the installation of docker in the Ubuntu 20.04 system along with pulling and running its images. After that, we have demonstrated the comparison between “run” and “exec –it” instructions to show how the exec command can run commands in the already running container without the console as the “run” command does. All the commands we have used are quite simple to understand and implement.

Источник

Читайте также:  Команда перенос файлов линукс
Оцените статью
Adblock
detector