Master Docker: Install the Environment and Essential Commands for Containers
This guide walks you through installing Docker on Linux, adding the Docker repository, installing the engine, and mastering the most common Docker commands for managing images and containers, including searching, pulling, running, inspecting, and cleaning up resources.
This article explains Docker environment installation and common commands, which greatly helps deploying applications in Docker.
Docker Overview
Docker is an open‑source container engine that lets developers package applications and their dependencies into portable images, which can be deployed on any popular Linux or Windows host. Using Docker simplifies packaging, testing, and deployment of applications.
Docker Environment Installation
1. Install yum‑utils:
yum install -y yum-utils device-mapper-persistent-data lvm22. Add Docker repository to yum:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo3. Install Docker CE: yum install docker-ce 4. Start Docker service:
systemctl start dockerCommon Docker Image Commands
Search Images
docker search javaPull Images
docker pull java:8Find Supported Versions
Since docker search only shows whether an image exists, you need to visit Docker Hub to see supported tags.
Open Docker Hub: https://hub.docker.com
Search for the image and view its tags.
Example pull command:
docker pull nginx:1.17.0List Images
docker imagesDelete Images
Delete by name and tag: docker rmi java:8 Force delete a specific image: docker rmi -f java:8 Force delete all images:
docker rmi -f $(docker images -a -q)Common Docker Container Commands
Create and Start a Container
docker run -p 80:80 --name nginx -d nginx:1.17.0-d runs the container in background.
--name assigns a name to the container.
-p maps host port 80 to container port 80.
List Containers
Running containers:
docker psAll containers (including stopped):
docker ps -aStop a Container
# $ContainerName or $ContainerId can be obtained via docker ps
docker stop $ContainerNameExample:
docker stop nginxForce Stop a Container
docker kill $ContainerNameStart a Stopped Container
docker start $ContainerNameEnter a Container's Shell
docker exec -it $ContainerName /bin/bashDelete a Container
Delete a specific container: docker rm $ContainerName Force delete all containers:
docker rm -f $(docker ps -a -q)View Container Logs
docker logs $ContainerNameGet Container IP Address
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $ContainerNameSync Host Time to Container
docker cp /etc/localtime $ContainerName:/etc/localtimeView Resource Usage (CPU, Memory, Network, I/O)
Specific container: docker stats $ContainerName All containers:
docker stats -aChange Docker Image Storage Location
Check current Docker root directory: docker info | grep "Docker Root Dir" Stop Docker service: systemctl stop docker Move the directory to a new path (e.g., /mydata/docker): mv /var/lib/docker /mydata/docker Create a symbolic link:
ln -s /mydata/docker /var/lib/dockerSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
