Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Dockerfile and Building Images Dockerfile Syntax FROM RUN COPY ADD CMD ENTRYPOINT ENV WORKDIR EXPOSE USER VOLUME ARG
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-dockerfile-and-building-images-dockerfile-syntax-from-run-copy-add-cmd-entrypoint-env-workdir-expose-user-volume-arg

Docker Docker Dockerfile and Building Images Dockerfile Syntax FROM RUN COPY ADD CMD ENTRYPOINT ENV WORKDIR EXPOSE USER VOLUME ARG

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Create a Docker image from a base image and define instructions to build and run the image. FROM

2. Step-by-Step

  1. Create a new directory for your Docker project and navigate to it.
    ```bash mkdir docker-project cd docker-project
2. Create a new file named `Dockerfile` in the current directory.
```bash touch Dockerfile
  1. Open the Dockerfile in your text editor and add the following line to specify the base image.
    ```dockerfile FROM python:3.9-slim
4. ⚠️ This deletes all stopped containers. Run `docker system prune -af` to clean up.
```bash docker system prune -af
  1. Build the Docker image using the following command.
    ```bash docker build -t my-python-image .
6. Run the Docker image using the following command.
```bash docker run -it my-python-image
  1. Check the Docker image and container using the following command.
    ```bash docker images docker ps
8. Remove the Docker image and container using the following command.
```bash docker rmi my-python-image docker rm $(docker ps -aq)

3. Real Commands with Examples


Example 1: Install Dependencies

Purpose: Install dependencies for a Python application.
Command: RUN pip install -r requirements.txt Brief explanation of flags: pip install installs packages, -r specifies a requirements file.

Example 2: Copy Files

Purpose: Copy a file from the current directory to the Docker image.
Command: COPY requirements.txt /app/requirements.txt Brief explanation of flags: COPY copies files, /app/requirements.txt specifies the destination.

Example 3: Set Environment Variables

Purpose: Set environment variables for a Docker image.
Command: ENV MY_VAR="Hello World" Brief explanation of flags: ENV sets environment variables.

4. Common Errors


Error 1: Missing FROM Statement

Error message or symptom: Dockerfile is empty or missing the FROM statement.
Why it happens: The FROM statement is required to specify the base image.
One-line fix: Add the FROM statement at the top of the Dockerfile.

Error 2: Invalid Image Name

Error message or symptom: docker build fails with an invalid image name.
Why it happens: The image name is not specified correctly.
One-line fix: Specify the image name correctly using the -t flag.

Error 3: Missing WORKDIR Statement

Error message or symptom: docker run fails with a missing working directory.
Why it happens: The WORKDIR statement is required to specify the working directory.
One-line fix: Add the WORKDIR statement to specify the working directory.

5. Quick Checks


Scenario 1: Check Docker Image

Situation: You want to check the Docker image and its tags.
Command to run: docker images What a correct response looks like: A list of Docker images with their tags.

Scenario 2: Check Docker Container

Situation: You want to check the Docker container and its status.
Command to run: docker ps What a correct response looks like: A list of Docker containers with their status.

Scenario 3: Check Docker Logs

Situation: You want to check the Docker logs for a container.
Command to run: docker logs <container_id> What a correct response looks like: The logs for the specified container.

6. Last-Minute Reference

FROM: Specify the base image.
RUN: Execute a command during build.
COPY: Copy files from the current directory to the Docker image.
ADD: Copy files from the current directory to the Docker image (deprecated).
CMD: Specify the default command to run when the container starts.
ENTRYPOINT: Specify the default command to run when the container starts (overwrites CMD).
ENV: Set environment variables.
WORKDIR: Specify the working directory.
EXPOSE: Expose a port.
USER: Specify the user to run the container as.
VOLUME: Mount a volume.
ARG: Define a build argument.
docker build: Build a Docker image.
docker run: Run a Docker container.
docker images: List Docker images.
docker ps: List Docker containers.
docker logs: Display logs for a container.
docker rm: Remove a Docker container.
docker rmi: Remove a Docker image.
docker system prune: Remove all stopped containers, dangling images, and unused networks.




ADVERTISEMENT