Introduction
Docker, the transformative containerization platform, empowers developers to orchestrate applications seamlessly. In this guide, we'll dive into essential Docker lifecycle commands, exploring their applications and real-world use cases.
1. docker create
The docker create
command initiates a new container but doesn't start it. This is useful when you want to configure settings before running the container.
Example:
docker create --name my-container -p 8080:80 nginx
This command creates an NGINX container named "my-container" but keeps it in a stopped state. It also maps port 8080 on the host to port 80 on the container.
2. docker run
The docker run
command creates and starts a container based on a specified image.
Example:
docker run -d --name web-app -p 8080:80 nginx
This command starts an NGINX container named "web-app" in detached mode, mapping port 8080 on the host to port 80 on the container.
3. docker pause
The docker pause
command suspends all processes in a running container, freezing its state.
Example:
docker pause web-app
This command pauses the execution of the "web-app" container, allowing you to temporarily halt its processes.
4. docker unpause
The docker unpause
command resumes the processes within a paused container.
Example:
docker unpause web-app
This command restores the execution of processes in the "web-app" container after it has been paused.
5. docker stop
The docker stop
command halts a running container, giving it time to gracefully shut down.
Example:
docker stop web-app
This command gracefully stops the "web-app" container, allowing processes to complete before termination.
6. docker start
The docker start
command initiates a previously stopped container.
Example:
docker start web-app
This command restarts the "web-app" container, resuming its execution from the previous state.
7. docker restart
The docker restart
command stops and then starts a container.
Example:
docker restart web-app
This command gracefully stops and then restarts the "web-app" container, ensuring a clean reboot.
8. docker attach
The docker attach
command attaches to a running container, allowing you to interact with its processes.
Example:
docker attach web-app
This command attaches to the terminal of the "web-app" container, enabling direct interaction with its processes.
9. docker wait
The docker wait
command blocks until a container stops, then prints the container's exit code.
Example:
docker wait web-app
This command waits for the "web-app" container to stop and prints its exit code, useful in scripting and automation.
10. docker rm
The docker rm
command removes one or more stopped containers.
Example:
docker rm web-app
This command removes the stopped "web-app" container, freeing up resources.
11. docker kill
The docker kill
command sends a signal to a running container, forcefully terminating it.
Example:
docker kill web-app
This command forcefully terminates the "web-app" container, useful in urgent situations.
Conclusion ✨
Mastering these Docker lifecycle commands equips you with the tools to efficiently manage containers throughout their lifecycle. From creating and running to pausing, stopping, and restarting, Docker commands offer flexibility and control, enabling seamless integration into your development and deployment workflows. 🐳