Skip to content

Dockerfile Documentation đŸ“ĻđŸŗ

Outline:

  • [x] What is a Dockerfile? 📝
  • [x] Dockerfile vs Docker Image vs Docker Container âš–ī¸
  • [x] How to Build an Image from a Dockerfile đŸ—ī¸
  • [x] How to Build a Container from a Docker Image đŸ“Ļ
  • [x] Common Commands for Running Containers 🚀
  • [x] Basic Operations on Docker đŸ› ī¸
  • [x] What Does It Mean for a Container to Be Isolated? 🚧
  • [x] How to Forward Ports from Local to Container 🔄

What is a Dockerfile? 📝

A Dockerfile is a text file that contains a series of instructions to build a Docker image for your project. It specifies the steps required to create an environment that can be used to run your application within a container. By using a Dockerfile, anyone can create a container from the image and run the project. 🌐


Dockerfile Vs Docker Image Vs Docker Container âš–ī¸

Term Description Analogy
Dockerfile A recipe that defines how to create an image. đŸŊī¸ Recipe
Docker Image A blueprint that describes the environment for running applications. đŸ—ī¸ Blueprint
Docker Container A running instance of an image, which contains everything needed to execute an application. đŸ“Ļ Container

How to Build an Image from a Dockerfile? đŸ—ī¸

To build an image from a Dockerfile, use the following command:

docker build -t <image_name>:<tag> .
  • -t <image_name>:<tag>: Specifies the name and tag of the image. đŸˇī¸
  • .: Indicates that the Dockerfile is in the current directory. 📂

How to Build a Container from a Docker Image? đŸ“Ļ

To create a container from a Docker image, use the following command:

docker run -d --name <container_name> <image_name>:<tag>
  • -d: Run the container in detached mode (in the background). 🌌
  • --name <container_name>: Assign a name to the container. đŸˇī¸
  • <image_name>:<tag>: Specifies the image from which to create the container. đŸ–ŧī¸

What Does It Mean for a Container to Be Isolated? 🚧

A Docker container runs in an isolated environment. This means that the processes running inside the container are separated from the host system and other containers. Isolation provides:

  • Security: Applications running in containers have limited access to the host system, reducing the risk of security breaches. 🔒
  • Resource Control: Containers can be limited in terms of CPU and memory usage, ensuring that no single container can consume all available resources. âš™ī¸
  • Consistent Environments: Each container can have its own dependencies and configurations, making it easier to manage and deploy applications across different environments. đŸ“Ļ

How to Forward Ports from Local to Container? 🔄

To forward a port from your local machine to a Docker container, use the -p option when running the container:

docker run -d -p <host_port>:<container_port> --name <container_name> <image_name>:<tag>
  • -p <host_port>:<container_port>: Maps the specified port on the host to the specified port in the container. 🌐

Example:

docker run -d -p 4002:4002 --name my_container my_image:latest

Common Commands for Running Containers 🚀

General Running Commands

Command Description Emoji
-d Run the container in detached mode (in the background). 🚀
--name <container_name> Assign a specific name to the container for easier reference. đŸˇī¸
-i Run the container in interactive mode, allowing you to interact with it. 🎮
-t Allocate a pseudo-TTY for the container, useful for interactive sessions. 🎭
--rm Automatically remove the container once it exits, helping to save disk space. đŸ—‘ī¸
--restart <policy> Specify a restart policy for the container (e.g., always, unless-stopped). 🔄
--user <user> Run the container with a specified user ID or username. 👤
--env <key=value> Set environment variables in the container. âš™ī¸

Networking and Port Mapping Commands

Command Description Emoji
-p <host_port>:<container_port> Map a port from the host to the container. 🔗
--network <network_name> Connect the container to a specific network. 🌐
--publish-all Publish all exposed ports to random ports on the host. 🌍

Resource Management Commands

Command Description Emoji
--memory <limit> Set a memory limit for the container. 🧠
--cpus <value> Limit the number of CPUs the container can use. âš™ī¸

Volume and File System Commands

Command Description Emoji
-v <host_path>:<container_path> Mount a volume from the host to the container. 📁
--mount type=bind,source=<host_path>,target=<container_path> Bind mount a host path to a container path. 🔗

Environment and Configuration Commands

Command Description Emoji
--env-file <file> Read environment variables from a file. 📄
--label <key=value> Set metadata for the container. đŸˇī¸

Basic Operations on Docker đŸ› ī¸

Container Management Commands

Command Description Emoji
docker ps List running containers. đŸ“Ļ
docker ps -a List all containers (including stopped ones). 📜
docker run Create and start a container. â–ļī¸
docker stop <container_name> Stop a running container. âšī¸
docker start <container_name> Start a stopped container. â–ļī¸
docker restart <container_name> Restart a running or stopped container. 🔄
docker rm <container_name> Remove a stopped container. đŸ—‘ī¸

Image Management Commands

Command Description Emoji
docker images List available images. đŸ–ŧī¸
docker pull <image_name> Download an image from a registry. âŦ‡ī¸
docker push <image_name> Upload an image to a registry. âŦ†ī¸
docker rmi <image_name> Remove an image. ❌
docker tag <source_image> <target_image> Tag an image for easier reference. đŸˇī¸

Logging and Inspection Commands

Command Description Emoji
docker logs <container_name> View logs for a container. 📜
docker inspect <container_name> Display detailed information about a container. 🧐

Network and Volume Management Commands

Command Description Emoji
docker network ls List all Docker networks. 🌐
docker volume ls List all Docker volumes. 💾
docker-compose up Start services defined in a docker-compose.yml file. 🚀
docker-compose down Stop and remove containers defined in a docker-compose.yml file. 🛑

Conclusion 🎉

This documentation provides a comprehensive overview of Dockerfile, its purpose, and how to work with Docker images and containers. By understanding these concepts and commands, you can efficiently manage your applications using Docker. Happy Dockering! đŸŗ