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! đŗ