The difference between the docker run VS docker exec

screenshot

 


"Docker run" and "docker exec" are Docker containers for command execution. However, in different situations, their use has fundamentally different.


"Docker run" command

"Docker run" command is usually in the newly created container used. It is suitable for operation in the absence of other containers, you want to create a container, and to start it, and then run a process on it. Its format is as follows:

docker run [OPTIONS] IMAGE COMMAND [ARG...]

After "docker run" command, you must specify the image created by the container, but you can also specify the [OPTIONS] and [ARG ...], for example:

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

The above command will create a container named ubuntu_bash Bash and start a conversation. The example used [OPTIONS] and [ARG ...] Interpretation detail as follows:

  • --name specify a name for a container, the container names in the present example is ubuntu_bash;
  • --rm just bash the rm command, it will remove the container, but here it will be deleted when you exit the container;
  • -i is -interactive Abbreviation, which ensures that even if the container is not connected to a running, STDIN (standard input) will be in an open state;
  • -t -tty may also be used to reference, starting interactive bashshell in the container;
  • Mirror containers should follow [OPTIONS], where Ubuntu is a mirror image;
  • Immediately behind the mirror part is the command you want to run: bash;

To learn more about "docker run" the more [OPTIONS] information, please see the https://docs.docker.com/engine/reference/commandline/run/ .


"Docker exec" command

"Docker exec" applies to the case of an existing vessel command is run. If you already have a running container, and want to change the container or derive some content, then use the "docker exec" command is very appropriate. For example, if you use a docker-compose, then you may start a plurality of containers, and want to be able to access one or more containers which after creating them. Its format is as follows:

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

"Docker exec" also has a range that can be specified [OPTIONS] and [ARG ...], although you need to ensure that the container and the status of the command to execute. You can use the following command to start the interactive bash shell on the vessel named ubuntu_bash:

docker exec -it ubuntu_bash bash

[OPTIONS] -it and "docker run" command here is the same. More [OPTIONS] and [ARG ...] of the example is shown below:

docker exec -d -w /temp ubuntu_bash touch my_file.sh
  • -w followed by a directory or file path, you can explain to run the command in which the working directory;
  • -d or -detached represents a container will be isolated mode, so you can continue to be used with a terminal session and run in the background container. If you want to view the contents of the container sent to STDOUT, please do not use this option;
  • This command is used to create a file named my_file.sh, in / temp directory named ubuntu_bash run container;

For more more [OPTIONS] information about the "docker exec", check https://docs.docker.com/engine/reference/commandline/exec/ .


understand more

In addition to these two commands, there are other Docker command has nuances, such as the similarities run with the build and create. Learn more and try different docker command can help you become professionals use this powerful cloud technology.

Guess you like

Origin www.cnblogs.com/sddai/p/11032879.html