By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Create a Docker image from a base image and define instructions to build and run the image. FROM
2. Create a new file named `Dockerfile` in the current directory. ```bash touch Dockerfile
Dockerfile
4. ⚠️ This deletes all stopped containers. Run `docker system prune -af` to clean up. ```bash docker system prune -af
6. Run the Docker image using the following command. ```bash docker run -it my-python-image
8. Remove the Docker image and container using the following command. ```bash docker rmi my-python-image docker rm $(docker ps -aq)
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.
RUN pip install -r requirements.txt
pip install
-r
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.
COPY requirements.txt /app/requirements.txt
COPY
/app/requirements.txt
Purpose: Set environment variables for a Docker image.Command: ENV MY_VAR="Hello World" Brief explanation of flags: ENV sets environment variables.
ENV MY_VAR="Hello World"
ENV
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.
FROM
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.
docker build
-t
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.
docker run
WORKDIR
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.
docker images
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.
docker ps
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.
docker logs <container_id>
• 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.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.