Master Docker: Core Concepts, Installation, and Essential Commands
This guide explains Docker’s basic concepts, advantages, architecture, installation steps, common image and container commands, and provides a practical example of configuring SSH access inside an Ubuntu container for both local and remote connections.
1. Docker Basic Concepts
What is Docker?
Docker is an open‑source container platform that lets developers package an application and all its dependencies into a portable unit called a container. Containers run on any environment that supports Docker, ensuring portability and consistency.
Advantages of Docker
Consistency and portability : Containers run on any Docker‑enabled platform, keeping development and production environments identical.
Resource isolation and control : Containers share the host kernel, offering efficient resource usage with isolation.
Fast deployment and startup : Containers start in seconds.
Simplified dependency management : All required libraries are bundled, eliminating “works on my machine” issues.
Docker Architecture
Image : A read‑only template containing the application, libraries, and configuration.
Container : A runnable instance of an image, an isolated lightweight execution environment.
Dockerfile : A text file with instructions to build an image.
Docker Hub : A cloud registry for pulling public images or uploading private ones.
After installing Docker, the platform provides the ability to run containers, while specific applications are delivered as images, analogous to software running on an operating system.
2. Docker Installation
Refer to the linked article for detailed installation steps.
3. Common Docker Commands
In the following sections, CONTAINER denotes a container ID or name.
Image Management
Pull an image docker pull ubuntu:20.04 List images docker images Remove an image docker rmi ubuntu:20.04 Commit a container as a new image docker commit CONTAINER IMAGE_NAME:TAG Save an image to a local file docker save -o ubuntu-20.04.tar ubuntu:20.04 Load an image from a local file
docker load -i ubuntu-20.04.tarContainer Management
Create a container docker create -it ubuntu:20.04 List containers docker ps -a Start a container docker start CONTAINER Stop a container docker stop CONTAINER Restart a container docker restart CONTAINER Create and run a container
docker run -p 20000:22 --name mycontainer -itd ubuntu:20.04Attach to a container docker attach CONTAINER Execute a command inside a container docker exec CONTAINER COMMAND Remove a container docker rm CONTAINER Remove all stopped containers docker container prune Export a container to a file docker export -o xxx.tar CONTAINER Import a container from a file docker import xxx.tar image_name:tag View container processes docker top CONTAINER View container resource usage docker stats Copy files between host and container docker cp CONTAINER:src_path dest_path Rename a container docker rename CONTAINER NEW_NAME Update container resource limits
docker update CONTAINER --memory 500M --memory-swap 1GThese commands enable effective management of Docker images and containers for development and deployment.
4. Docker Application Example: SSH Access Inside a Container
The example demonstrates creating an Ubuntu container, installing OpenSSH, configuring root login, and accessing the container via SSH from the host and from a remote server.
Steps
Pull the Ubuntu image: docker pull ubuntu:20.04 Create and run a container with port 22 mapped to host port 20000:
docker run -p 20000:22 --name test1 -itd ubuntu:20.04Attach to the container: docker attach test1 Set the root password inside the container: passwd Install OpenSSH server:
apt update
apt install -y openssh-serverEdit /etc/ssh/sshd_config to enable root login and password authentication:
PermitRootLogin yes
PasswordAuthentication yesStart the SSH service: service ssh start From the host, SSH into the container: ssh root@localhost -p 20000 From a remote machine, ensure the server’s port 20000 is open (see image below) and connect:
ssh root@SERVER_IP -p 20000After completing these steps, you can log into the Docker container directly from the local machine or a remote server.
Signed-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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
