Docker – Instruction Commands

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

To execute a command at runtime use by this command when execute by the container.

Syntax

CMD command param1 

Options

  • command − This is the Command to run when launch by the container.
  • param1 − The Command enter in to the parameter by this.

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.

Step1 − Build the Docker File with the following commands −

FROM ubuntu 
MAINTAINER [email protected] 
CMD [“echo” , “hello world”] 

Here, Hello World just use to print by the CMD.

cmd

Step2 − Build the image using the Docker build command.

build command

Step3 − Run a container from the image.

run a container

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 launch by the container.
  • param1 − The Command enter in to the parameter by this.

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.

Step1 − Build the Docker File with the following commands −

FROM ubuntu 
MAINTAINER [email protected] 
ENTRYPOINT [“echo”]
entry point image

Step2 − Build the image using the Docker build command.

docker build command

Step3 − Run a container from the image.

container from

ENV

The Container uses to set environment variables by this command.

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.

Step1 − Build the Docker File with the following commands −

FROM ubuntu 
MAINTAINER [email protected] 
ENV var1=Tutorial var2=point 
instruction commands

Step2 − Build the image using the Docker build command.

instruction commands

Step3 − Run a container from the image.

env run

Step4 − Finally, execute the env command to see the environment variables.

env command

WORKDIR

The Container uses to set the working directory by this command.

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.

Step1 − Build the Docker File with the following commands −

FROM ubuntu 
MAINTAINER [email protected] 
WORKDIR /newtemp 
CMD pwd
workdir

Step2 − Build the image using the Docker build command.

workdir build command

Step3 − Run a container from the image.

workdir run command

In This guide, we learned about Docker instruction commands. To know more click here.

This Post Has 2 Comments

Leave a Reply