Dockerfile Instructions and Docker Run Commands: A Comprehensive Guide

Featured Image

Dockerfile Instructions

  • FROM: Sets the base image for subsequent instructions.
    FROM ubuntu:20.04 
    

  • RUN: Executes commands in a new layer on top of the current image.

    RUN apt-get update && apt-get install -y curl 
    

  • MAINTAINER (Deprecated): Identifies the image creator. Use LABEL instead.

    LABEL maintainer="[email protected]" 
    

  • LABEL: Adds metadata to the image.

    LABEL version="1.0" description="My Docker Image" 
    

  • ADD: Copies files from a source (supports URLs and tar extraction).

    ADD https://example.com/file.tar.gz /tmp/ 
    

  • COPY: Copies files from host to container (no URL support).

    COPY ./app /usr/src/app 
    

  • VOLUME: Creates a mount point for external storage.

    VOLUME /data 
    

  • EXPOSE: Declares a container’s listening port.

    EXPOSE 8080 
    

  • WORKDIR: Sets the working directory for subsequent commands.

    WORKDIR /app 
    

  • USER: Specifies the user for running processes.

    USER appuser 
    

  • STOPSIGNAL: Sets the system call signal for stopping the container.

    STOPSIGNAL SIGTERM 
    

  • ENTRYPOINT: Configures the container’s main executable.

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

  • CMD: Provides default arguments for the ENTRYPOINT or runs a command.

    CMD ["--help"] 
    

  • ENV: Sets environment variables.

    ENV API_KEY=12345 
    

Docker Run Commands

  • –name: Assigns a name to the container.

    docker run --name my_container nginx 
    

  • -v, –volume: Mounts a host directory into the container.

    docker run -v /host/path:/container/path nginx 
    

  • –network: Connects the container to a network.

    docker run --network=bridge nginx 
    

  • -d, –detach: Runs the container in the background.

    docker run -d nginx 
    

  • -i, –interactive: Keeps STDIN open for interaction.

    docker run -i ubuntu bash 
    

  • -t, –tty: Allocates a pseudo-TTY.

    docker run -it ubuntu bash 
    

  • –rm: Automatically removes the container after exit.

    docker run --rm alpine echo "Hello" 
    

  • -e, –env: Sets environment variables.

    docker run -e "ENV=prod" nginx 
    

  • –restart: Defines the container’s restart policy.

    docker run --restart=always nginx 
    

Core Docker Concepts

  • Docker Image: A read-only template with instructions for creating a container.
  • Docker Container: A runnable instance of an image.
  • Docker Client: CLI tool for interacting with Docker.
  • Docker Daemon: Background service managing Docker objects.
  • Docker Registry: Hosts Docker images (e.g., Docker Hub).

You Should Know:

Essential Docker Commands

1. List Running Containers

docker ps 

2. List All Containers (Including Stopped Ones)

docker ps -a 

3. Build a Docker Image

docker build -t my-image:1.0 . 

4. Remove a Container

docker rm container_name 

5. Remove All Stopped Containers

docker container prune 

6. Pull an Image from Docker Hub

docker pull nginx:latest 

7. Execute a Command Inside a Running Container

docker exec -it container_name bash 

8. View Logs of a Container

docker logs container_name 

9. Inspect a Container’s Details

docker inspect container_name 

10. Stop a Running Container

docker stop container_name 

What Undercode Say

Docker is a powerful tool for containerization, enabling consistent environments across development, testing, and production. Mastering Dockerfile instructions and docker run commands is essential for DevOps engineers, cloud architects, and developers.

Additional Linux & Windows Commands for Docker Management

  • Linux Disk Space Cleanup (Remove Unused Docker Objects)
    docker system prune -a 
    

  • Windows PowerShell Docker Cleanup

    docker system prune --volumes 
    

  • Check Docker Disk Usage

    docker system df 
    

  • Run a Temporary Alpine Linux Container

    docker run --rm -it alpine sh 
    

  • Export a Docker Container to a Tar File

    docker export container_name > backup.tar 
    

  • Import a Docker Image from a Tar File

    docker import backup.tar my-image:restored 
    

  • Tag and Push an Image to Docker Hub

    docker tag local-image:tag username/repo:tag 
    docker push username/repo:tag 
    

  • Run a Docker Container with Resource Limits

    docker run -it --memory="1g" --cpus="1.5" ubuntu 
    

  • Create a Docker Network for Multi-Container Apps

    docker network create my-network 
    

  • Connect a Container to a Network

    docker network connect my-network container_name 
    

Expected Output:

A structured, detailed guide on Dockerfile instructions, Docker run commands, and essential Docker management techniques for IT professionals.

Prediction:

Docker will continue to dominate containerization, with tighter Kubernetes integration and improved security features in future releases.

References:

Reported By: Satya619 %F0%9D%90%83%F0%9D%90%A8%F0%9D%90%9C%F0%9D%90%A4%F0%9D%90%9E%F0%9D%90%AB%F0%9D%90%9F%F0%9D%90%A2%F0%9D%90%A5%F0%9D%90%9E – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram