Essential Docker Commands
Docker has become an integral part of modern DevOps practices, offering a robust platform for containerizing applications, ensuring consistency across different environments, and simplifying deployment processes. If you're new to Docker or looking to brush up on your skills, here's a guide to the essential Docker commands you should know when working on a Virtual Private Server (VPS).
Running Containers
docker run
This command creates and starts a new container from an image.
docker run -it --name mycontainer image:name
-i: Keeps STDIN open even if not attached.
-t: Allocates a pseudo-TTY.
--name: Names your container for easier management.
For a detached container (running in the background):
docker run -d --name mycontainer image:name
Managing Containers
docker ps
Lists running containers. Add -a to see all containers, including stopped ones.
docker ps
docker ps -a
docker exec
Executes a command inside a running container. Useful for debugging or getting a shell inside a container:
docker exec -it mycontainer bash
docker logs
Views logs from a container:
docker logs mycontainer
docker stop
Stops a running container:
docker stop mycontainer
docker rm
Removes a container (must be stopped first):
docker rm mycontainer
Managing Images
docker build
Builds an image from a Dockerfile:
docker build -t myimage:v1 .
-t: Tags the image with a name and optionally a tag.
docker images
Lists all images on your system:
docker images
docker rmi
Removes one or more images:
docker rmi image:name
docker pull
Pulls an image from a registry (default is Docker Hub):
docker pull image:name
Additional Useful Commands
docker start
: Starts a stopped container.
docker restart
: Restarts a running container.
docker commit
: Creates a new image from a container's changes.
docker push
: Pushes an image to a Docker registry.
Advanced Use
For managing applications composed of multiple containers, docker-compose is invaluable. It allows you to define and run multi-container Docker applications with a single configuration file.
Conclusion These commands form the backbone of Docker usage for DevOps on a VPS. Mastering them will give you the flexibility to manage containers efficiently, whether you're deploying new applications or maintaining existing ones. Remember, Docker's strength lies in its ability to create consistent environments across different stages of development and deployment, making these commands not just useful but essential for any DevOps toolkit.