30 May 2023
Here are some commonly used instructions in a Dockerfile:
- 1. FROM: Specifies the base image for your Docker image. It is typically the starting point for your Dockerfile. For example, `FROM php:7.4-fpm` selects the PHP 7.4 image with FPM (FastCGI Process Manager).
- 2. WORKDIR: Sets the working directory inside the container where subsequent instructions will be executed. It is recommended to use an absolute path. For example,` WORKDIR /var/www/html` sets the working directory to ` /var/www/html`.
-
3. ADD instruction copies new files, directories or remote file URLs from
and adds them to the filesystem of the image at the path < dest >. ADD has two forms:
(this form is required for paths containing whitespace)ADD [--chown=< user >:< group >] ["< src >",... "< dest >"]
ADD [--chown=< user >:< group >] < src >... < dest >
- 4. COPY: Copies files from the host machine to the container. It takes two arguments: the source path on the host machine and the destination path inside the container. For example, ` COPY . /var/www/html` copies all files from the current directory on the host to the ` /var/www/html` directory inside the container.
-
5. RUN: Executes commands inside the container at build time. It can be used to install packages, run scripts, or perform any necessary setup.
For example,
updates the package lists and installs the` curl` package inside the container.RUN apt-get update && apt-get install -y curl
- 6. ENV: Sets environment variables inside the container. These variables can be accessed during the build process or when the container is running. For example, ` ENV APP_ENV production` sets the ` APP_ENV` environment variable to` production`.
- 7. EXPOSE: Informs Docker that the container will listen on the specified network ports at runtime. It does not actually publish the ports. For example, ` EXPOSE 8000` indicates that the container will listen on port 8000.
-
8. CMD: Specifies the command to run when the container starts. It is usually the main process of the container. There can only be one CMD instruction in a Dockerfile.
For example,
runs the Laravel development server when the container starts.CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]