📜  dockerfile run app cmd - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:30:32.649000             🧑  作者: Mango

Dockerfile RUN APP CMD - Shell/Bash

Introduction

Docker is a popular tool used by software developers and system administrators for building, deploying and running applications within a containerized environment. Dockerfile is a script used to automate the creation of Docker images by specifying the steps needed to build the image. In this article, we will explore the RUN, APP and CMD keywords in Dockerfile, specifically how to use them with Shell/Bash commands to build and run an application in a Docker container.

RUN

The RUN keyword executes a command in the Docker image during the build process. This command can be any Shell/Bash command or script needed to prepare the image for the application. For example, if the application requires the installation of certain packages, we can use the RUN keyword to install them. The command is executed in a new layer of the image and its results are committed, creating a new layer in the Docker image.

# Example Dockerfile using RUN to install Python packages
FROM python:3.8

RUN pip install pandas numpy

In this example, we start from the Python 3.8 image and use the RUN keyword to install the pandas and numpy libraries required for the application.

APP

The APP keyword specifies the default working directory for the application in the Docker image. This is useful for organizing the application files and specifying the location of the executable files. The following example shows how to use the APP keyword to set the working directory to /app for our application.

# Example Dockerfile using APP to set the working directory
FROM python:3.8

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

In this example, we use the WORKDIR keyword to set the working directory to /app, copy the requirements.txt file into the image and install the required packages. Next, we copy the application files into the image and set the default CMD to run the app.py file.

CMD

The CMD keyword specifies the command to run when the container starts. This command can be any Shell/Bash command, and it is executed in the context of the APP keyword specified in the Dockerfile. In the previous example, we specified the CMD to run the app.py file in the context of the /app directory.

# Example Dockerfile using CMD to specify the command to run
FROM python:3.8

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]
Conclusion

In this article, we have explored how to use the RUN, APP and CMD keywords in Dockerfile with Shell/Bash commands to build and run an application in a Docker container. Remember that Dockerfile is a powerful tool for automating the creation of Docker images, and it can be customized to meet the specific needs of your application.