Essential Docker Commands for Beginners: A Complete Guide
1. Installation and Setup
Before using Docker, you need to install and configure it properly. Below are the essential commands to get started.
1.1 Install Docker
curl -fsSL https://get.docker.com | sh
- Flags:
-f
: Fail silently on errors-s
: Silent mode-S
: Show errors-L
: Follow redirects
- Description: Installs Docker using an official installation script.
1.2 Check Docker Version
docker --version
- Description: Displays the installed Docker version.
1.3 Start Docker Service
systemctl start docker
- Description: Starts the Docker daemon.
1.4 Enable Docker to Start on Boot
systemctl enable docker
- Description: Ensures Docker starts automatically on system reboot.
2. Container Management
Containers are the core of Docker. Below are commands to create, manage, and remove containers.
2.1 Run a Container
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- Flags:
-d
: Run in detached mode-p
: Map ports (host:container)--name
: Assign a name to the container-e
: Set environment variables-v
: Mount a volume--rm
: Remove the container after it exits
- Description: Runs a container from an image.
2.2 List Running Containers
docker ps
- Flags:
-a
: Show all containers, including stopped ones-q
: Display only container IDs
- Description: Shows currently running containers.
2.3 Stop a Container
docker stop CONTAINER_ID/NAME
- Description: Stops a running container.
2.4 Remove a Container
docker rm CONTAINER_ID/NAME
- Flags:
-f
: Force removal
- Description: Deletes a container.
3. Image Management
Docker images are the templates for creating containers. These commands help manage images efficiently.
3.1 Pull an Image
docker pull IMAGE_NAME[:TAG]
- Description: Downloads a Docker image from a registry.
3.2 List Local Images
docker images
- Flags:
-q
: Show only image IDs--filter
: Filter output
- Description: Displays locally stored images.
3.3 Remove an Image
docker rmi IMAGE_ID/NAME
- Flags:
-f
: Force removal
- Description: Deletes a Docker image.
4. Network Management
Docker networks allow containers to communicate with each other securely.
4.1 List Networks
docker network ls
- Description: Shows available Docker networks.
4.2 Create a Network
docker network create NETWORK_NAME
- Flags:
--driver
: Specify the network driver
- Description: Creates a new Docker network.
4.3 Connect a Container to a Network
docker network connect NETWORK_NAME CONTAINER_NAME
- Description: Attaches a container to a network.
5. Volume Management
Docker volumes help persist data beyond container lifecycles.
5.1 List Volumes
docker volume ls
- Description: Shows existing Docker volumes.
5.2 Create a Volume
docker volume create VOLUME_NAME
- Description: Creates a new persistent volume.
5.3 Remove a Volume
docker volume rm VOLUME_NAME
- Description: Deletes a Docker volume.
Final Thoughts
Mastering these basic Docker commands will provide you with a strong foundation for containerization. Whether you are managing containers, images, networks, or volumes, these commands will streamline your workflow and improve efficiency.
If you are new to Docker, start practicing these commands in a local environment. In upcoming blogs, we will explore Docker Compose, Docker Swarm, and Kubernetes to take your container management skills to the next level.
Happy Dockering! 🚢🐳