Terminus
Override the Container Entrypoint With docker run

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:

$ docker run --entrypoint <command> <image>

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:

$ docker run -it --entrypoint /bin/bash ubuntu

And this command will execute the local [.inline-code]startup.sh[.inline-code] script instead of the image's default entrypoint:

$ docker run --entrypoint ./startup.sh my-image

[#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:

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:

$ docker run --entrypoint <command> <image> <argument ...>

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:

$ docker run --entrypoint /usr/bin/python3 ubuntu -m http.server

[#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:

$ docker run --entrypoint /bin/bash <image> -c "<command_1> && <command_2> ..."

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:

$ docker run --entrypoint "/bin/bash" ubuntu -c "npm install && npm run test"

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:

$ docker run --entrypoint "/bin/sleep" <image> <seconds>

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:

$ docker run --entrypoint "/bin/sleep" ubuntu 20

[#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:

$ docker run --entrypoint <command> --user <username> <image>

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:

$ docker run --entrypoint "/bin/bash" --user www-data ubuntu -c "whoami"

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:

$ docker run --entrypoint <command> --workdir <directory> <image> <argument ...>

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:

$ docker run --entrypoint "/bin/bash" --workdir /app ubuntu -c "pwd"

[#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:

$ docker run --entrypoint <command> -v <host_directory>:<container_directory> <image>

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:

$ docker run --entrypoint "npm run start" -v /home/johndoe/website:/app website

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:

$ docker run --entrypoint <command> -e <variable>=<value> <image>

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:

$ docker run --entrypoint ./startup.sh -e DB_NAME=development website