What is a Docker


Docker is a virtualization container engine that completely uses the sandbox isolation mechanism, which has great advantages over traditional VM VMS.

The difference between traditional virtual machine (VM) and virtual container (Docker) :

Traditional VMS cannot share resources. For example, if a Linux OS with a memory of 4GB is installed in the VMWare hypervisor, the Linux OS uses the fixed memory of 4GB in the host and cannot share this resource with the host or other virtual systems. Resources may be wasted or overflowed.

Docker makes up for the shortcomings of traditional VM. Each virtual operating system can share resources with each other, and solve a series of problems such as resource overflow or resource waste.

Main concepts of Docker


Docker Image

The operating system is divided into kernel and user space. For Linux, after the kernel starts, the root file system is mounted to provide user space support. A Docker Image is a root file system.

Docker image is a special file system. In addition to providing programs, libraries, resources, configuration and other files required by the container runtime, Docker image also contains some configuration parameters (such as environment variables, users, etc.) prepared for the runtime.

Docker Container

The relationship between Docker image and Docker container is like class and object in object-oriented programming. Image is a static definition.

Containers are entities that can be created, started, stopped, deleted, suspended, and so on when an image is running.

Processes in a container run in an isolated environment and are used as if they were operating on a separate system from the host. This feature makes container-wrapped applications safer than running directly on the host.

Docker Repository (Registry)

Once the image is built, a single command can be run on the current host, but if you want to use the image on other servers, you need a centralized repository to store the image — the Docker repository.

Public Docker Registry: The most commonly used Registry public service is the official Docker Hub, but it will be slow to access in China. Some domestic cloud service providers provide image services for Docker Hub, such as Aliyun, DaoCloud accelerator, etc. It will be much faster than downloading directly from Docker Hub.

Private Docker Registry: Private Docker Registry can be set up locally. Docker officially provides Docker Registry image, which can be directly used as a private Registry service.

Install the Docker


Automatic installation using a script

$ curl -fsSL get.docker.com -o get-docker.sh

$ sudo sh get-docker.sh --mirror Aliyun
Copy the code

After executing these two commands, the script will automatically prepare everything and install Docker CE on the system.

Install using APT

Install some necessary system tools
$ sudo apt-get update
$ sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

Install the GPG certificate
$ curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

# Write software source information
$ sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

Update the software source
$ sudo apt-get -y update

Install Docker CE
$ sudo apt-get -y install docker-ce
Copy the code

Docker user group


After installation, by default, Docker commands communicate with the Docker engine using Unix sockets, and only Root and Docker group users can access the Unix socket of the Docker engine. The Root user is not directly used on Linux systems. Therefore, it is better to add users who need to use Docker to the Docker user group.

Create Docker user group
$ sudo groupadd docker

Add user to Docker user group$sudo usermod -ag Docker User nameCopy the code

Mirror accelerator


Because downloading images from Docker Hub in China is sometimes very slow, so you can configure the image accelerator provided by the domestic cloud service provider:

  • Chinese Registry Mirror officially provided by Docker
  • Ali Cloud Accelerator
  • DaoCloud accelerator

Get Aliyun mirror accelerator

Add mirror accelerator

  • Log in aliyun
  • Go to the Container Mirroring Services Console > Mirror Accelerator
  • Add mirror accelerator
  • Copy the accelerator address as shown below

Mirror accelerator configuration

Edit the daemon.json configuration file

$ sudo nano /etc/docker/daemon.json
Copy the code

Add the following code

{
  "registry-mirrors": [
    "Mirror accelerator address"]}Copy the code

Restart the Docker service

$ sudo systemctl daemon-reload

$ sudo systemctl restart docker
Copy the code

Check that the accelerator is working

$ docker info
Copy the code

If the following information is displayed, the mirror accelerator is configured successfully

Registry Mirrors: Indicates the address of the mirror acceleratorCopy the code

Docker common commands


Docker image command

# List mirrors
$ docker images
$ docker image ls -a

# Run Docker image (daemon mode)$docker run -d Image nameDelete the Docker image$docker image RM Image name# Delete Docker suspended image
$ docker image prune
Copy the code

Docker container command

List the running containers
$ docker ps -a

List all containers (including stopped containers)
$ docker ps -l

Enter the running Docker container
$ docker exec-it container ID /bin/bash# Stop Docker container$docker stop ID of the container# Remove the Docker container$docker rm -f Specifies the container ID# Remove stopped Docker containers
$ docker container prune

View the Docker container history run logs$docker logs Specifies the container name# Monitor Docker run logs in real time$docker logs -f Specifies the container nameCopy the code

Docker Data volume command

# Create Docker data volume$docker Volume create Specifies the name of a data volumeList all Docker data volumes
$ docker volume ls

Delete the specified Docker data volume$Docker Volume Specifies the name of the rm data volume# Delete unassociated (invalid) Docker data volumes
$ docker volume prune
$ docker volume rm $(docker volume ls -qf dangling=true)
Copy the code

Docker File operation command

Copy files from host to Docker container$sudo docker cp File path in the host Container ID: file path in the container# Copy files from Docker container to host$sudo docker cp Container ID: File path in the container File path in the hostCopy the code

For more dry stuff, go to Antoniopeng.com