Operations 7 min read

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.

macrozheng
macrozheng
macrozheng
Master Docker: Install the Environment and Essential Commands for Containers
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 lvm2

2. Add Docker repository to yum:

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

3. Install Docker CE: yum install docker-ce 4. Start Docker service:

systemctl start docker

Common Docker Image Commands

Search Images

docker search java

Pull Images

docker pull java:8

Find 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.0

List Images

docker images

Delete 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 ps

All containers (including stopped):

docker ps -a

Stop a Container

# $ContainerName or $ContainerId can be obtained via docker ps
docker stop $ContainerName

Example:

docker stop nginx

Force Stop a Container

docker kill $ContainerName

Start a Stopped Container

docker start $ContainerName

Enter a Container's Shell

docker exec -it $ContainerName /bin/bash

Delete 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 $ContainerName

Get Container IP Address

docker inspect --format '{{ .NetworkSettings.IPAddress }}' $ContainerName

Sync Host Time to Container

docker cp /etc/localtime $ContainerName:/etc/localtime

View Resource Usage (CPU, Memory, Network, I/O)

Specific container: docker stats $ContainerName All containers:

docker stats -a

Change 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/docker
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsLinuxcommand-lineInstallationContainers
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.