Listen to this Post
Docker has revolutionized containerization, making it essential for developers and IT professionals to master Dockerfile instructions and run commands. Below is a detailed breakdown of key Docker components, followed by practical examples.
Dockerfile Instructions
- FROM: Sets the base image for the container.
FROM ubuntu:20.04
- RUN: Executes commands inside the container.
RUN apt-get update && apt-get install -y curl
- MAINTAINER (Deprecated): Identifies the image creator (use LABEL instead).
- LABEL: Adds metadata to the image.
LABEL maintainer="[email protected]"
- ADD: Copies files from a source (supports URLs and tar extraction).
ADD https://example.com/file.tar.gz /app/
- COPY: Copies files without URL or extraction support.
COPY ./src /app/src
- VOLUME: Creates a shared mount point.
VOLUME /data
- EXPOSE: Specifies the container’s listening port.
EXPOSE 8080
- WORKDIR: Sets the working directory.
WORKDIR /app
- USER: Defines the user for running processes.
USER appuser
- STOPSIGNAL: Specifies the stop signal (default: SIGTERM).
STOPSIGNAL SIGKILL
- ENTRYPOINT: Sets the primary command.
ENTRYPOINT ["python", "app.py"]
- CMD: Sets default arguments for ENTRYPOINT.
CMD ["--port=8080"]
- ENV: Sets environment variables.
ENV DB_HOST=localhost
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.
- -t, –tty: Allocates a pseudo-TTY.
docker run -it ubuntu /bin/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 restart policy.
docker run --restart=always nginx
Core Docker Concepts
- Docker Image: A read-only template (e.g.,
nginx:latest
). - Docker Container: A running instance of an image.
- Docker Client: CLI tool (
docker
command). - Docker Daemon: Background service managing containers.
- Docker Registry: Stores images (e.g., Docker Hub).
You Should Know:
1. Building and Running a Docker Container
docker build -t myapp:1.0 . docker run -d -p 8080:80 myapp:1.0
2. Managing Containers
docker ps -a List all containers docker stop <ID> Stop a container docker rm <ID> Remove a container docker logs <ID> View logs
3. Managing Images
docker images List images docker rmi <ID> Remove an image docker pull nginx Download an image
4. Networking in Docker
docker network ls List networks docker network create my_network docker run --network=my_network nginx
5. Docker Compose (Multi-Container Setup)
version: '3' services: web: image: nginx ports: - "80:80" db: image: mysql environment: MYSQL_ROOT_PASSWORD: password
Run with:
docker-compose up -d
What Undercode Say
Docker is a powerful tool for DevOps, cloud deployments, and microservices. Mastering `Dockerfile` instructions and `docker run` commands ensures efficient container management. Always follow best practices:
– Use `.dockerignore` to exclude unnecessary files.
– Prefer `COPY` over `ADD` unless URL/tar extraction is needed.
– Use multi-stage builds to reduce image size.
– Secure containers by running as non-root (USER
instruction).
Expected Output:
A fully functional Dockerized application with optimized images, proper networking, and automated deployments.
Further Reading:
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 ✅