Basic usage of Docker
Docker architecture description
Docker uses the C/S architecture. The docker client communicates with the docker daemon, which is responsible for building, running and distributing docker containers. Docker client and daemon can run on the same system, or connect docker client to remote docker daemon. Docker clients and daemons communicate through unix sockets or network interfaces using rest APIs.
The following is the description of Docker core components and elements:
-
Docker Daemon: dockerd, which is used to listen to Docker API requests and manage docker objects, such as images, containers, networks, and volumes.
-
Docker client: docker. Docker client is the main way for us to interact with docker. For example, we can run a container through the docker run command, and then our docker client will send the command to Dockerd above, which is responsible for processing.
-
Docker Registry: a repository for storing docker images. Docker hub is a public repository officially provided, and docker also finds images from docker hub by default. Of course, you can also easily run a private warehouse. When we use the docker pull or docker run command, we will pull the image from the docker image warehouse we configured. When we use docker push, the image we built will be pushed to the corresponding image warehouse.
-
Images: image. Image is a read-only template with instructions for creating a docker container. Generally speaking, the image will be built based on other basic images and added with some additional customization functions. For example, you can build a centos based image, and then install an Nginx application on the basic image, so that we can build our own image.
-
Containers: container, which is a mirror running instance. You can use Docker REST API or CLI to operate containers. The essence of a container is a process, but different from the process executed directly on the host, the container runs in its own independent namespace. Therefore, the container can have its own root file system, its own network configuration, its own process space, and even its own user id space. The processes in the container run in an isolated environment and are used as if they were operating on a host independent system. This feature allows container encapsulated applications to run more cases than directly on the host.
docker installation
Configure network source (rhel red hat system)
[root@docker ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
Configure docker CE source
[root@docker ~]# cd /etc/yum.repos.d/ [root@docker yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
Install docker CE and dependent packages and tools
[root@docker ~]# dnf -y install yum-utils device-mapper-persistent-data lvm2 [root@docker ~]# yum -y install docker-ce --allowerasing
After installation, use the docker version command to view the docker version information
[root@docker ~]# docker version Client: Docker Engine - Community Version: 20.10.11 API version: 1.41 Go version: go1.16.9 Git commit: dea9396 Built: Thu Nov 18 00:36:58 2021 OS/Arch: linux/amd64 Context: default Experimental: true
After the docker configuration is completed, we also need to configure an accelerator on the host
When using docker, we often pull images from docker hub, but most of its servers are set up overseas, so downloading is very slow, and adding an accelerator will greatly improve the speed of pulling images.
The configuration file of docker CE is / etc/docker/daemon.json, which does not exist by default. We need to create and configure it manually, and the acceleration of docker is realized by configuring this file.
docker can be accelerated in many ways:
- docker cn
- Accelerator of China University of science and technology
- Alicloud accelerator (you need to register an account through alicloud Developer Platform and use your own accelerator for free)
[root@docker ~]# mkdir -p /etc/docker [root@docker ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://a74l47xi.mirror.aliyuncs.com "] / / the website here is assigned by personal account } [root@docker ~]# systemctl daemon-reload [root@docker ~]# systemctl restart docker
docker common operations
command | function |
---|---|
docker search | Search the Docker Hub for images |
docker pull | Pull an image or a repository from a registry |
docker images | List images |
docker create | Create a new conntainer |
docker start | Start one or more stopped containers |
docker run | Run a command in a new container |
docker attach | Attach to a runninng container |
docker ps | List containers |
docker logs | Fetch the logs of a container |
docker restart | Restart a container |
docker stop | Stop one or more running containers |
docker kill | Kill one or more running containers |
docker rm | Remove onne or more containers |
docker exec | Run a command in a running container |
docker info | Display system-wide information |
docker inspect | Return low-level information on Docker objects |
Command case
docker search
Search the docker hub repository for the specified image
[root@docker ~]# docker search httpd NAME DESCRIPTION STARS OFFICIAL AUTOMATED httpd The Apache HTTP Server Project 3783 [OK] centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui... 40 centos/httpd 34 [OK] arm32v7/httpd The Apache HTTP Server Project 10 polinux/httpd-php Apache with PHP in Docker (Supervisor, CentO... 5 [OK] solsson/httpd-openidc mod_auth_openidc on official httpd image, ve... 2 [OK] hypoport/httpd-cgi httpd-cgi 2 [OK] inanimate/httpd-ssl A play container with httpd, ssl enabled, an... 1 [OK] publici/httpd httpd:latest 1 [OK] jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1 [OK] lead4good/httpd-fpm httpd server which connects via fcgi proxy h... 1 [OK] dockerpinata/httpd 1 manageiq/httpd Container with httpd, built on CentOS for Ma... 1 [OK] dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent... 1 [OK] clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser... 1 centos/httpd-24-centos8 1 appertly/httpd Customized Apache HTTPD that uses a PHP-FPM ... 0 [OK] amd64/httpd The Apache HTTP Server Project 0 interlutions/httpd httpd docker image with debian-based config ... 0 [OK] manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK] manasip/httpd 0 itsziget/httpd24 Extended HTTPD Docker image based on the off... 0 [OK] ysli/httpd Httpd for DeepWeb 0 [OK] trollin/httpd 0 e2eteam/httpd 0
docker pull
Pull the mirror from the specified location
[root@docker ~]# docker pull httpd Using default tag: latest latest: Pulling from library/httpd eff15d958d66: Pull complete ba1caf8ba86c: Pull complete ab86dc02235d: Pull complete 0d58b11d2867: Pull complete e88da7cb925c: Pull complete Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02 Status: Downloaded newer image for httpd:latest docker.io/library/httpd:latest
docker images
View mirror list
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest ad17c88403e2 12 days ago 143MB
docker create
Create the specified container (not running)
[root@docker ~]# docker create httpd eab77da51c8f2188580d59786267d0b9abc5269ec4f10532ba44645c85a14c55 //View with docker ps [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker start
Starts the specified container
//Note: the start is not followed by the name of the container, but the id of the container [root@docker ~]# docker start eab77da51c8f2188580d59786267d0b9abc5269ec4f10532ba44645c85a14c55 [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eab77da51c8f httpd "httpd-foreground" 2 minutes ago Up 21 seconds 80/tcp kind_yonath
docker run
Create and allow the specified container (if there is no image in the image list when using this command, it will automatically pull, create and run it)
View the image list first [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest ad17c88403e2 12 days ago 143MB //There is only one mirror at this time [root@docker ~]# docker run nginx Unable to find image 'nginx:latest' locally //Pull the latest image latest: Pulling from library/nginx eff15d958d66: Already exists 1e5351450a59: Pull complete 2df63e6ce2be: Pull complete 9171c7ae368c: Pull complete 020f975acd28: Pull complete 266f639b35ad: Pull complete Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e Status: Downloaded newer image for nginx:latest /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/12/01 09:01:18 [notice] 1#1: using the "epoll" event method 2021/12/01 09:01:18 [notice] 1#1: nginx/1.21.4 2021/12/01 09:01:18 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2021/12/01 09:01:18 [notice] 1#1: OS: Linux 4.18.0-193.el8.x86_64 2021/12/01 09:01:18 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/12/01 09:01:18 [notice] 1#1: start worker processes 2021/12/01 09:01:18 [notice] 1#1: start worker process 31 2021/12/01 09:01:18 [notice] 1#1: start worker process 32 //View the mirror list again [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest ad17c88403e2 12 days ago 143MB nginx latest ea335eea17ab 13 days ago 141MB //Use the docker ps -a command to see if it is running [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 10 minutes ago Up 10 minutes 80/tcp unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 19 minutes ago Up 16 minutes 80/tcp kind_yonath
docker stop
Stop the specified container
// View running containers [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 10 minutes ago Up 10 minutes 80/tcp unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 19 minutes ago Up 16 minutes 80/tcp kind_yonath [root@docker ~]# docker stop de6d7ac190f9 / / stop nginx container de6d7ac190f9 [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 13 minutes ago Exited (0) 8 seconds ago unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 21 minutes ago Up 18 minutes 80/tcp kind_yonath //The nginx container was found to have stopped (Exited (0) 8 seconds) and exited eight seconds ago
docker restart
Restart the specified container
[root@docker ~]# Docker PS - A / / status is off CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 13 minutes ago Exited (0) 8 seconds ago unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 21 minutes ago Up 18 minutes 80/tcp kind_yonath [root@docker ~]# docker restart de6d7ac190f9 de6d7ac190f9 //restart [root@docker ~]# docker ps -a / / check again and find the status (status) up CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 16 minutes ago Up 5 seconds 80/tcp unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 24 minutes ago Up 22 minutes 80/tcp kind_yonath ## Up 5 seconds started five seconds ago
docker logs
View the log of the specified container
[root@docker ~]# docker logs de6d7ac190f9 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/12/01 09:01:18 [notice] 1#1: using the "epoll" event method 2021/12/01 09:01:18 [notice] 1#1: nginx/1.21.4 2021/12/01 09:01:18 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2021/12/01 09:01:18 [notice] 1#1: OS: Linux 4.18.0-193.el8.x86_64 2021/12/01 09:01:18 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/12/01 09:01:18 [notice] 1#1: start worker processes 2021/12/01 09:01:18 [notice] 1#1: start worker process 31 2021/12/01 09:01:18 [notice] 1#1: start worker process 32 2021/12/01 09:11:43 [notice] 1#1: signal 28 (SIGWINCH) received 2021/12/01 09:11:43 [notice] 1#1: signal 28 (SIGWINCH) received 2021/12/01 09:11:45 [notice] 1#1: signal 28 (SIGWINCH) received 2021/12/01 09:11:45 [notice] 1#1: signal 28 (SIGWINCH) received 2021/12/01 09:14:09 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down 2021/12/01 09:14:09 [notice] 31#31: gracefully shutting down 2021/12/01 09:14:09 [notice] 32#32: gracefully shutting down
docker kill
End (kill) the running process
[root@docker ~]# docker ps -a / / view the running status of the container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 22 minutes ago Up 5 minutes 80/tcp unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 30 minutes ago Up 27 minutes 80/tcp kind_yonath [root@docker ~]# docker kill eab77da51c8f eab77da51c8f //End the process of dropping the httpd container [root@docker ~]# docker ps -a / / view again, find and stop CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 22 minutes ago Up 5 minutes 80/tcp unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 30 minutes ago Exited (137) 10 seconds ago kind_yonath Exited (137) 10 seconds ago 10 Exit seconds ago
docker rm
Delete the specified container (the running container cannot be deleted)
[root@docker ~]# docker ps -a / / view the running status of the container (nginx is running at this time) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 22 minutes ago Up 5 minutes 80/tcp unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 30 minutes ago Exited (137) 10 seconds ago kind_yonath [root@docker ~]# docker rm de6d7ac190f9 (found when deleting nginx container, unable to delete) Error response from daemon: You cannot remove a running container de6d7ac190f9645505474d826c3730e2feb6b6e44fe6b99c0d88208a4e6a69ba. Stop the container before attempting removal or force remove [root@docker ~]# docker ps -a (niginx container is still running) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 25 minutes ago Up 9 minutes 80/tcp unruffled_visvesvaraya eab77da51c8f httpd "httpd-foreground" 34 minutes ago Exited (137) 3 minutes ago kind_yonath [root@docker ~]# docker rm eab77da51c8f / / delete the httpd container to stop running eab77da51c8f [root@docker ~]# docker ps -a / / when viewing again, httpd has been deleted CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 26 minutes ago Up 9 minutes 80/tcp unruffled_visvesvaraya
docker attach
Enter the running container (unable to interact, it will enter the foreground of the http container process)
[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 26 minutes ago Up 9 minutes 80/tcp unruffled_visvesvaraya [root@docker ~]# docker attach de6d7ac190f9
docker exec
Enter the specified container and be able to interact
[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de6d7ac190f9 nginx "/docker-entrypoint...." 26 minutes ago Up 9 minutes 80/tcp unruffled_visvesvaraya [root@docker ~]# docker exec -it de6d7ac190f9 /bin/bash root@de6d7ac190f9:/# ls bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var boot docker-entrypoint.d etc lib media opt root sbin sys usr root@de6d7ac190f9:/# cd etc/ root@de6d7ac190f9:/etc#
docker info
View docker details
[root@docker ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.6.3-docker) scan: Docker Scan (Docker Inc., v0.9.0) Server: Containers: 1 Running: 1 Paused: 0 Stopped: 0 Images: 2 Server Version: 20.10.11 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux Default Runtime: runc Init Binary: docker-init containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d runc version: v1.0.2-0-g52b36a2 init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 4.18.0-193.el8.x86_64 Operating System: Red Hat Enterprise Linux 8.2 (Ootpa) OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 1.758GiB Name: docker ID: UJQ7:W4K6:UVQ3:TG7C:243M:E5BN:SVKN:24W2:FUFC:MH3G:R56O:JUIB Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://a74l47xi.mirror.aliyuncs.com/ Live Restore Enabled: false