How to run a command in the Docker container from the host

To run a Bash command with parameters inside a Docker container using the docker run command, you can specify the command and its parameters after the image name. Here’s the general syntax:

docker run

Here’s an example command that runs a Bash command with parameters inside a container based on the ubuntu image:

docker run ubuntu bash -c "echo Hello, Docker!"

In this example, the Ubuntu image is used, and the bash -c “echo Hello, Docker!” command is executed inside the container. The -c flag is used to pass the command as a string. In this case, it prints “Hello, Docker!” using the echo command.


dev@codetryout:~$ docker run ubuntu bash -c "echo Hello, Docker!"
Hello, Docker!

You can replace the command and parameters with your specific requirements. Make sure to enclose the entire command and its parameters in quotes if they contain spaces or special characters.

Note that if the image you’re using doesn’t have Bash installed, you may need to use an alternative shell available, such as sh or ash, depending on the image’s configuration.

Related: 40+ BASH commands for everyday use

Additionally, the docker run command will create a new container each time it is executed. If you want to execute a command in an existing container, you can use docker exec instead, as mentioned in the previous response.