Override the Container Entrypoint With docker run
In Docker, the entrypoint refers to the default executable or command that runs when a container is launched.
The short answer
To override the entrypoint of a Docker container and run another command instead on container startup, you can use the [.inline-code]docker run[.inline-code] command with the [.inline-code]--entrypoint[.inline-code] flag as follows:
Where:
- [.inline-code]<command>[.inline-code] is the command or script you want to run instead of the default entrypoint.
- [.inline-code]<image>[.inline-code] is the name of the Docker image you want to launch the container from.
For example, this command will replace the [.inline-code]ubuntu[.inline-code] image’s default entrypoint with the [.inline-code]/bin/bash[.inline-code] command, which gives access to an interactive Bash shell within the container:
And this command will execute the local [.inline-code]startup.sh[.inline-code] script instead of the image's default entrypoint:
[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions[#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/6697e82ca0c313671257f88b_AD_4nXdWzYNqpnOsfyN7K14SXYDjyWsk5NPfztR-jZfaxgkpAw1EzOVKq6mWLYXgIwSbg2Spkgxn7zkpZWx7qPfktzyTk4CIUliHUJVdgXclB9ngHbxqFSESFQMRxuQMph-kXKF6NMx906Un0H1iSfGpPKZBswQ1.gif)
Entering [.inline-code]docker run entrypoint[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]docker[.inline-code] command that can be quickly inserted into your shell by pressing [.inline-code]CMD+ENTER[.inline-code].
[#pass-arguments-to-the-entrypoint] Passing arguments to the entrypoint command[#pass-arguments-to-the-entrypoint]
To pass one or more arguments to the specified entrypoint script or command, you can specify them after the image name in the [.inline-code]docker run[.inline-code] command as follows:
Where:
- [.inline-code]<argument ...>[.inline-code] is a list of command parameters separated by a space character.
For example, this command will launch a new container based on the [.inline-code]ubuntu[.inline-code] image and override the default entrypoint with the [.inline-code]/usr/bin/python3 -m http.server[.inline-code] command:
[#combine-multiple-commands] Combining multiple commands as an entrypoint [#combine-multiple-commands]
To combine and run multiple commands in a sequence as a single entrypoint, you can pass them as arguments of the [.inline-code]/bin/bash -c[.inline-code] command using the following syntax:
For example, this command will launch a new container based on the [.inline-code]ubuntu[.inline-code] image and execute the [.inline-code]npm install[.inline-code] and [.inline-code]npm run test[.inline-code] commands in a sub-shell through the [.inline-code]/bin/bash -c[.inline-code] command upon container startup:
You can learn more about executing commands in a Docker container with Bash by reading our other article on how to run Bash in Docker.
[#delay-the-execution-of-commands] Troubleshooting containers by delaying the execution of commands [#delay-the-execution-of-commands]
One way to troubleshoot Docker containers, for instance to monitor its startup logs or verify the proper execution of specific processes within the container, you can pause the container's execution for a certain amount of seconds using the [.inline-code]/bin/sleep[.inline-code] command follows:
Where:
- [.inline-code]<seconds>[.inline-code] is the number of seconds you want to pause the container for.
For example, this command will pause the container's execution for [.inline-code]20[.inline-code] seconds before exiting:
[#execute-commands-as-another-user] Executing commands as another user[#execute-commands-as-another-user]
By default, all the commands executed within a Docker container are executed with superuser privileges.
To execute commands as another user instead of the default root user, you can use the [.inline-code]docker run[.inline-code] command with the [.inline-code]--user[.inline-code] flag as follows:
Where:
- [.inline-code]username[.inline-code] is the name of the user to execute the specified command as.
For example, this command will override the default entrypoint and execute the [.inline-code]whoami[.inline-code] command as the [.inline-code]www-data[.inline-code] user on container startup:
However, note that in order to work, the specified user account must first be defined within the container's OS.
You can learn more about this by reading our other article on how to create a new user in Linux.
[#change-the-default-working-directory] Changing the default working directory[#change-the-default-working-directory]
To ensure that the specified entrypoint command is executed at the right location in the filesystem, you can use the [.inline-code]docker run[.inline-code] command with the [.inline-code]--workdir[.inline-code] flag as follows:
Where:
- [.inline-code]<directory>[.inline-code] is the absolute path to the directory the specified [.inline-code]command[.inline-code] will be executed in.
For example, this command will execute the [.inline-code]pwd[.inline-code] command within the [.inline-code]/app[.inline-code] directory upon container startup:
[#mount-a-local-directory] Mounting a local directory[#mount-a-local-directory]
It may sometimes happen that your entrypoint commands or scripts require specific files present on your local machine to run properly.
To allow the specified entrypoint to access the files located on your machine from within the container, you can create a bind mount using the [.inline-code]-v[.inline-code] flag as follows:
Where:
- [.inline-code]<host_directory>[.inline-code] is the absolute or relative path of the bind mount on your local machine.
- [.inline-code]<container_directory>[.inline-code] is the absolute path of the file or directory within the container.
For example, this command will map the local [.inline-code]/home/johndoe/website[.inline-code] directory to the container's [.inline-code]/app[.inline-code] directory and execute the [.inline-code]npm run start[.inline-code] command upon container startup:
You can learn more about bind mounts by reading our other article on how to mount files and directories in a Docker container.
[#override-environment-variables] Overriding environment variables [#override-environment-variables]
To set or override environment variables, you can use the [.inline-code]docker run[.inline-code] command with the [.inline-code]-e[.inline-code] flag as follows:
Where:
- [.inline-code]<variable>[.inline-code] is the name of the environment variable you want to set.
- [.inline-code]<value>[.inline-code] is the value of the environment variable.
Note that you can use the [.inline-code]-e[.inline-code] flag multiple times to set multiple variables.
For example, this command will set the [.inline-code]DB_NAME[.inline-code] environment variable to [.inline-code]development[.inline-code] and execute the [.inline-code]startup.sh[.inline-code] script upon container startup: