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,
CONTAINERdenotes a container ID or name.
Image Management
Pull an image
<code>docker pull ubuntu:20.04</code>List images
<code>docker images</code>Remove an image
<code>docker rmi ubuntu:20.04</code>Commit a container as a new image
<code>docker commit CONTAINER IMAGE_NAME:TAG</code>Save an image to a local file
<code>docker save -o ubuntu-20.04.tar ubuntu:20.04</code>Load an image from a local file
<code>docker load -i ubuntu-20.04.tar</code>Container Management
Create a container
<code>docker create -it ubuntu:20.04</code>List containers
<code>docker ps -a</code>Start a container
<code>docker start CONTAINER</code>Stop a container
<code>docker stop CONTAINER</code>Restart a container
<code>docker restart CONTAINER</code>Create and run a container
<code>docker run -p 20000:22 --name mycontainer -itd ubuntu:20.04</code>Attach to a container
<code>docker attach CONTAINER</code>Execute a command inside a container
<code>docker exec CONTAINER COMMAND</code>Remove a container
<code>docker rm CONTAINER</code>Remove all stopped containers
<code>docker container prune</code>Export a container to a file
<code>docker export -o xxx.tar CONTAINER</code>Import a container from a file
<code>docker import xxx.tar image_name:tag</code>View container processes
<code>docker top CONTAINER</code>View container resource usage
<code>docker stats</code>Copy files between host and container
<code>docker cp CONTAINER:src_path dest_path</code>Rename a container
<code>docker rename CONTAINER NEW_NAME</code>Update container resource limits
<code>docker update CONTAINER --memory 500M --memory-swap 1G</code>These 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:
<code>docker pull ubuntu:20.04</code>Create and run a container with port 22 mapped to host port 20000:
<code>docker run -p 20000:22 --name test1 -itd ubuntu:20.04</code>Attach to the container:
<code>docker attach test1</code>Set the root password inside the container:
<code>passwd</code>Install OpenSSH server:
<code>apt update
apt install -y openssh-server</code>Edit
/etc/ssh/sshd_configto enable root login and password authentication:
<code>PermitRootLogin yes
PasswordAuthentication yes</code>Start the SSH service:
<code>service ssh start</code>From the host, SSH into the container:
<code>ssh root@localhost -p 20000</code>From a remote machine, ensure the server’s port 20000 is open (see image below) and connect:
<code>ssh root@SERVER_IP -p 20000</code>After completing these steps, you can log into the Docker container directly from the local machine or a remote server.
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.