Docker has a host of instruction commands. These are commands that are put in the Docker File. Let’s look at the ones which are available.
CMD Instruction
This command is used to execute a command at runtime when the container is executed.
Syntax
CMD command param1
Options
- command − This is the command to run when the container is launched.
- param1 − This is the parameter entered to the command.
Return Value
The command will execute accordingly.
Example
In our example, we will enter a simple Hello World echo in our Docker File and create an image and launch a container from it.
Step 1 − Build the Docker File with the following commands −
FROM ubuntu MAINTAINER demousr@gmail.com CMD [“echo” , “hello world”]
Here, the CMD is just used to print hello world.
![CMD](https://www.tutorialspoint.com/docker/images/cmd.jpg)
Step 2 − Build the image using the Docker build command.
![Build Command](https://www.tutorialspoint.com/docker/images/build_command.jpg)
Step 3 − Run a container from the image.
![Run a container](https://www.tutorialspoint.com/docker/images/run_a_container.jpg)
ENTRYPOINT
This command can also be used to execute commands at runtime for the container. But we can be more flexible with the ENTRYPOINT command.
Syntax
ENTRYPOINT command param1
Options
- command − This is the command to run when the container is launched.
- param1 − This is the parameter entered into the command.
Return Value
The command will execute accordingly.
Example
Let’s take a look at an example to understand more about ENTRYPOINT. In our example, we will enter a simple echo command in our Docker File and create an image and launch a container from it.
Step 1 − Build the Docker File with the following commands −
FROM ubuntu MAINTAINER demousr@gmail.com ENTRYPOINT [“echo”]
![ENTRYPOINT](https://www.tutorialspoint.com/docker/images/entry_point.jpg)
Step 2 − Build the image using the Docker build command.
![Docker Build Command](https://www.tutorialspoint.com/docker/images/docker_build_command.jpg)
Step 3 − Run a container from the image.
![Container from Image](https://www.tutorialspoint.com/docker/images/container_from_image.jpg)
ENV
This command is used to set environment variables in the container.
Syntax
ENV key value
Options
- Key − This is the key for the environment variable.
- value − This is the value for the environment variable.
Return Value
The command will execute accordingly.
Example
In our example, we will enter a simple echo command in our Docker File and create an image and launch a container from it.
Step 1 − Build the Docker File with the following commands −
FROM ubuntu MAINTAINER demousr@gmail.com ENV var1=Tutorial var2=point
![ENV](https://www.tutorialspoint.com/docker/images/env.jpg)
Step 2 − Build the image using the Docker build command.
![ENV Build Docker Command](https://www.tutorialspoint.com/docker/images/env_docker_build_command.jpg)
Step 3 − Run a container from the image.
![ENV Run a Container](https://www.tutorialspoint.com/docker/images/env_run_a_container.jpg)
Step 4 − Finally, execute the env command to see the environment variables.
![ENV Command](https://www.tutorialspoint.com/docker/images/env_command.jpg)
WORKDIR
This command is used to set the working directory of the container.
Syntax
WORKDIR dirname
Options
- dirname − The new working directory. If the directory does not exist, it will be added.
Return Value
The command will execute accordingly.
Example
In our example, we will enter a simple echo command in our Docker File and create an image and launch a container from it.
Step 1 − Build the Docker File with the following commands −
FROM ubuntu MAINTAINER demousr@gmail.com WORKDIR /newtemp CMD pwd
![WORKDIR](https://www.tutorialspoint.com/docker/images/workdir.jpg)
Step 2 − Build the image using the Docker build command.
![Workdir Build Command](https://www.tutorialspoint.com/docker/images/workdir_build_command.jpg)
Step 3 − Run a container from the image.
![WORKDIR Run Command](https://www.tutorialspoint.com/docker/images/workdir_run_command.jpg)
No comments:
Post a Comment