Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Docker is now a leading software container platform in the IT industry. The emergence and popularity of Docker enables developers to package applications and dependent packages into a portable container. Rapid deployment of applications can be realized through the container. Arguably, Docker makes creating, deploying, and managing containers extremely easy.
The above is mandarin, do not understand it does not matter, the important thing is to operate!
If you already know About Docker but haven’t actually used it, read on and try Docker yourself.
Install using the installation script
The automatic installation script is provided. You can use the following command line interface (CLI) to automatically install the software:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
Copy the code
Daocloud = daoCloud = daoCloud = daoCloud = daoCloud = daoCloud = daoCloud = daoCloud
curl -sSL https://get.daocloud.io/docker | sh
Copy the code
Manual installation
Uninstall the old docker version that has been installed
If docker has been installed on the machine before, you need to run the following command to uninstall the old docker:
- List the related packages that Docker has installed
sudo yum list installed | grep docker
Copy the code
Containerd.io. X86_64 1.4.11-3.1. el7@docker-ce -stable docker-ce. X86_64 3:20.10.9-3. el7@docker-ce -stable X86_64 1:20.10.9-3.el7@docker-ce-stable docker-ce-rootless extras. X86_64 20.10.9-3.el7@docker-ce-stable Docker - scan - plugin. X86_64 0.8.0-3. El7 @ docker - ce - stableCopy the code
- Delete respectively
yum -y remove containerd.io.x86_64 \
docker-ce.x86_64 \
docker-ce-cli.x86_64 \
docker-ce-rootless-extras.x86_64 \
docker-scan-plugin.x86_64
Copy the code
- Deleting Related Software Packages
sudo yum remove docker docker-common docker-selinux docker-engine
Copy the code
- Delete related images and containers
sudo rm -rf /var/lib/docker
Copy the code
Install dependencies and set up the Docker image
- Docker relies on some of the necessary tools for the system, which can be installed in advance.
yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code
- Set docker image (Ali Cloud Image)
sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code
Install the docker
sudo yum install docker-ce docker-ce-cli containerd.io
Copy the code
Start the docker
- Activation:
sudo systemctl start docker
#or
service dockers start
Copy the code
After running the startup command, you can run the docker version command to check whether the Docker is successfully installed.
- Configure automatic startup after startup:
sudo systemctl enable docker
Copy the code
- stop
sudo systemctl stop docker
#or
sudo service docker stop
Copy the code
- restart
sudo systemctl restart docker
#or
sudo service docker restart
Copy the code
- Check the docker status
sudo systemctl status docker
#or
sudo service docker status
Copy the code
Common Docker commands
Docker image related commands
#Search the mirrorDocker Search Specifies the image name#Pull the mirrorDocker pull image name :tag#View the downloaded image
docker images
#Remove the mirrorDocker RMI image ID#Forcibly Deleting a MirrorDocker Rmi-f Image IDCopy the code
Docker container-related commands
Create and start the container
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Common OPTIONS:
- -I: Runs the container in interactive mode, usually in conjunction with -t
- -t: reassigns a pseudo-input terminal to the container. It is usually used together with -i
- –name: Specifies the container name
- -d: Creates a daemon to run the container in the background and returns the container ID
- -p: randomly allocates port mappings
- -p: specifies the port mapping. The former is the host port and the latter is the container port mapping
- -m: Sets the memory size used by the container
For example, use the image boystar/ubantu to start a container named my_ubantu in interactive mode, and execute the /bin/bash command within the container.
[root@centos7 ~]# docker run -it --name my_ubantu boystar/ubantu /bin/bash root@6065ec1d0437:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@6065ec1d0437:/#Copy the code
Docker ps [OPTIONS]
Common OPTIONS:
- -a: View all containers
- -l: displays the newly created container
- -n: Displays the n containers created recently
- -q: displays only container ids in silent mode
Out of the container
-
Exit: The container stops exiting
-
CTRL +P+Q The container does not stop exiting
Into the container
Docker Attach Container nameCopy the code
Or:
Docker exec -it Container name/container ID /bin/bashCopy the code
Other commands
#Start the containerDocker start Container name/container ID#Stop the containerDocker stop Container name/container ID#Force stop containerDocker kill Container name/container ID#Remove the containerDocker RM Container name/container ID#Forcibly deleting containersDocker rm -f Container name/container ID#Delete all containers
docker rm $(docker ps -a -q)
#Viewing container LogsDocker logs-t-f Container name/container IDCopy the code
Actual Docker installation Redis
Pull the Redis image
docker pull redis:latest
Copy the code
Start the container
docker run -itd --name my_redis -p 6379:6379 redis
Copy the code
-p 6379:6379: maps port 6379 of the container service to port 6379 of the host. External users can directly access Redis services through host IP :6379.
Run the Redis client tests
[root@centos7 ~]# docker exec -it my_redis /bin/bash root@862c8129f379:/data# redis-cli 127.0.0.1:6379> set test 1 OK 127.0.0.1:6379 >Copy the code
conclusion
This paper mainly introduces the installation and use of Docker under Linux, but the concept and architecture of Docker are not introduced. In addition, there may be problems when pulling images from Docker Hub in China. Image accelerator can be configured. Docker official and many domestic cloud service providers provide domestic accelerator service, which is not described here.
Original is not easy, if small partners feel helpful, please click a “like” and then go ~
Finally, thank my girlfriend for her tolerance, understanding and support in work and life!