Docker containers are a tool that allows an application to run in its own environment with the ability of swapping out parts, like dependencies or software versions. This differs from virtual machines and offers more flexibility.
In this article we will show you how to run startup commands in Docker Containers
The “docker exec” is a command that allows you to execute commands in the context of a running Docker container. The “docker exec” command can be used with any other command that takes arguments.
This guide is for you if you’ve ever needed to execute a command or two in your Docker container when it first starts up. You may execute as many starting commands as you like using the Dockerfile ENTRYPOINT and CMD directives.
In this article, you’ll learn how to execute startup commands in a Dockerfile using the ENTRYPOINT and CMD instructions, as well as the distinctions between them.
Creating Your First Docker Windows Server Container is Related
Prerequisites
Because this is a hands-on lesson, make sure you have the following items ready:
- This training was conducted on a Windows 10 PC running version 10.0.19042.
- Docker Desktop — Docker Desktop v3.3.1 is used in this tutorial.
Docker for Windows: How to Deploy Your First Container
Dockerfile creation
You must first generate a Dockerfile before you can execute Docker container starting instructions. A Dockerfile is a text file containing a series of instructions for building containers and Docker images, as well as determining how a Docker image is built.
1. Run PowerShell as an administrator first.
2. Create a new folder and change to it to store the Dockerfile and any related files for this tutorial. /docker is used in this tutorial.
3. Run the following command to create a blank text file called Dockerfile.
If you’re using Linux or Mac OS, you may build a Dockerfile using the following command.
4. Finally, in the Dockerfile, add the following text.
You’ve just made your first Dockerfile!
Constructing a Docker Image
After you’ve built your Dockerfile, you’ll need to construct a Docker image to run the commands listed in the ENTRYPOINT and CMD sections. The build command is one technique to create an image.
Run the following command while in the /docker directory. By supplying the current working directory, the command below produces a Docker image named demo (-t demo) from the Dockerfile in /docker (.).
Constructing a Docker Image
Docker Container Management
You’ll need a container to run the Docker image once you’ve constructed it, which will execute the commands from the Dockerfile ENTRYPOINT and CMD instructions.
Use the run command to establish a writeable container layer atop the Docker image and execute it (demo). The -it argument is used in the example below to connect to the container interactively and see the sample output.
Docker Container Management
Shell Form vs. Exec
You may come across two alternative approaches of setting startup commands while working with a Dockerfile and figuring out how to execute them. Each technique will execute orders, albeit in a slightly different way.
Docker may either execute commands directly (exec) or via the container’s shell (shell) (/bin/sh -c on Linux or cmd /S /C on Windows).
As seen below, instructions performed through exec contain an instruction, followed by the executables to call, and one or more command-line arguments.
[“executables”, “parameter1”, “parameter2”,…] ENTRYPOINT [“executables”, “parameter1”, “parameter2:,…”] CMD
In contrast, writing commands in shell form does not need the use of square brackets, as demonstrated below.
ENTRYPOINT <command> “parameter1” CMD <command> “parameter1”
If you don’t specify a argument to CMD, Docker will always execute the command in exec form e.g. CMD <command>.
If you’re just getting started, the differences between these two command invocations won’t matter much, but as you gain experience, you’ll see the advantages and disadvantages of each.
Execution of Startup Commands
Let’s now get in the meat of this tutorial and get your hands dirty by walking through a few examples of Execution of Startup Commands within a Dockerfile ENTRYPOINT and CMD instructions.
1. In your favourite text editor, open the Dockerfile you produced previously.
2. Paste the contents of the sample Dockerfile into your Dockerfile and save it, as shown below.
This Dockerfile generates a layer using the base image ubuntu:20.04. It then instructs Docker to use exec and shell form to launch the echo command with the Hello world parameter for both the Dockerfile CMD and ENTRYPOINT commands.
DOWNLOAD FROM ubuntu:20.04 # CMD Command CMD [“echo”, “Hello world”] # Shell Form CMD echo “Hello world” # Exec Form CMD echo “Hello world” # INSTRUCTION FOR ENTRYPOINT [“echo”, “Hello world”] ENTRYPOINT # Shell Form ENTRYPOINT echo “Hello world” # Exec Form ENTRYPOINT echo “Hello world”
3. Create a new image in the /docker directory by executing docker build and naming it demo. The command below marks the image as demo and searches the current working directory for a Dockerfile (.).
Constructing a Docker Image with docker build
4. Now launch a container based on the image, followed by a Docker container based on the Docker image you prepared previously. The container now returns Hello world, as expected, thanks to the CMD directive in the Dockerfile.
Docker Container Management with docker run
In a Dockerfile, Using Variables
You may not always be aware of the appropriate command-line options to supply to command. Only at runtime are the parameters you’ll need to supply to a command available. Instead of statically allocating command parameters, variables may be used to capture and send such arguments to commands.
Dockerfile variables can only be used in shell form. Variables are not supported by Docker when using the exec form to run a command.
Replace everything within the Dockerfile with the following set of instructions and save it in your chosen text editor.
This time, the Dockerfile makes use of environment variables, which are shown via ENV. The Dockerfile defines an environment variable named name with the value friend in the example below. This environment variable is then accessed through $name once it has been generated.
When Docker launches a container based on this Dockerfile, it will execute echo with the parameter of Welcome, buddy.
# or ## ENTRYPOINT echo “Welcome, $name” FROM ubuntu:20.04 ENV name friend CMD echo “Welcome, $name”
Create the Docker image and run the container again, this time with a tag name of shellform as an option. You’ll note that Docker used the echo command and got the desired result.
Constructing a Docker Image (shellform) and Docker Container Management
ENTRYPOINT and CMD Instructions in a Dockerfile
Most of the time, you’ll be using the CMD or ENTRYPOINT commands to run startup tasks. After all, each approach allows you to run as many commands as you like. However, you may use both instructions to launch a single command and “build upon it.”
Perhaps you have a Dockerfile that looks like the one below, based on the previous examples. Docker would currently call the echo command and return Hello if you created an image and ran a container from it.
[“echo”, “Hello”] FROM ubuntu:20.04 ENTRYPOINT
Perhaps you want to give another parameter to the echo command, but not just now. Perhaps you should do it later in the Dockerfile. You may do so by using the CMD instruction without a command.
When you use the ENTRYPOINT instruction followed by the CMD instruction to execute a command, Docker considers the value supplied to CMD as an argument rather than a command.
Now, as seen below, add a CMD instruction reference without a command, simply an argument named world.
[“echo”, “Hello”] FROM ubuntu:20.04 ENTRYPOINT CMD [“world”]
Because of its “array-like” characteristic of supplying values separately separated by commas rather than all in one string, combining instructions should always be expressed in exec form.
When you create the image and run the container from it, you’ll see that instead of two lines of output (Hello and world), Docker just delivers one, implying only one echo command call.
Constructing a Docker Image (demo3) and Docker Container Management
Conclusion
You should now be able to use both the CMD and ENTRYPOINT Dockerfile directives to perform Docker container starting commands. Each instruction is unique, yet they all achieve the same goal and may even be combined.
Can you conceive of a situation where you’d rather execute a startup command using CMD rather than ENTRYPOINT?
The “docker entrypoint” is a command that allows users to run startup commands in Docker containers. This command ensures that the container starts up with the right settings, and it can be used in conjunction with other commands like “docker-compose”.
Related Tags
- docker run
- command to run container in docker
- docker run post command
- docker run multiple commands on startup
- docker run –entrypoint