Docker (Miscellaneous notes)

Time:2022-1-1

Reference link:http://www.dockerinfo.net/doc…
Docker is a container technology
Container is a lightweight, portable and self-contained software packaging technology, which enables applications to run in the same way almost anywhere.
Develop containers that are created and tested on your own machine and can run on the virtual machine, physical server or public virtual machine of the production system without any modification.
The goal of docker project is to achieve a lightweight operating system virtualization solution. Docker is based on technologies such as Linux container (LxC).
The differences between docker and traditional virtualization methods show that the container is virtualized at the operating system level, directly reusing the operating system of the local host, while the traditional method is implemented at the hardware level.
Containers do not require virtual hardware or a full operating system. Multiple containers share the kernel of the host.
Docker features: lightweight, portable and less resource occupation
For developers: build once, run anywhere
For O & M personnel: configure once, run anything

1、 Update Yum source
1. First, back up the system’s own Yum source configuration file / etc / yum repos. d/CentOS-Base. repo
[[email protected] ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2. Enter the folder where the yum source configuration file is located
[[email protected] ~]# cd /etc/yum.repos.d/
3. Download the yum source configuration file of 163 to the folder above
[[email protected] yum.repos.d]# wget http://mirrors.163.com/.help/…
4. Run Yum makecache to generate a cache
[[email protected] yum.repos.d]# yum makecache
5. When you update the system, you will see the following mirrors 163.com information
[[email protected] yum.repos.d]# yum -y update

2、 Install docker
1. Update source Yum or up2date
yum update
2. Uninstall the old version (if the old version has been installed)
yum remove docker docker-common docker-selinux docker-engine
3. Install the required packages. Yum util provides the yum config manager function. The other two are dependent on the devicemapper driver
yum install -y yum-utils device-mapper-persistent-data lvm2
4. Set Yum source
yum-config-manager –add-repo http://mirrors.aliyun.com/doc…
5. You can view all docker versions in all warehouses and select a specific version to install
yum list docker-ce –showduplicates | sort -r
6. Install docker
Yum install docker CE # since only stable warehouse is enabled by default in repo, the latest stable version 17.12.0 is installed here
Yum install < fqpn > # for example: sudo Yum install docker-ce-17.12.0 ce
7. Boot and join boot
systemctl start docker
systemctl enable docker
8. Verify whether the installation is successful (there are two parts: client and service, which means that the installation and startup of docker are successful)
docker version

3、 Set alicloud source
Log in to alicloud, select the image accelerator, and configure it according to different operating systems

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://*****.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

4、 Docker usage
1. Pull image
docker pull mysql:5.6
2. Run MySQL

docker run -p 3306:3306 --name mymysql -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

3. Check whether the installation is successful through the docker PS command
4. Start container: docker start container ID
5. Enter the MySQL container: docker exec – it 064c6bea326d / bin / Bash
6. Execute the command MySQL – H localhost – U root – P, prompt for the password, and enter 123456 to perform relevant database operations

Installing nginx

docker pull nginx

docker run --name nginx-test -p 80:80 -d nginx
Run create container instance

--Name container naming

-V mapping directory

-D set container background operation

-P native port mapping maps the container's port 80 to the native port 80
The last nginx statement is to use the name of the image

Create some key directories:
mkdir -p /root/nginx/www /root/nginx/logs /root/nginx/conf

Copy the nginx test container configuration file locally
docker cp 481e121fb29f:/etc/nginx/nginx.conf /root/nginx/conf

Create a new nginx container nginx web, and map the * * WWW, logs, conf * * directory to the local directory
docker run -d -p 80:80 --name nginx-web -v /root/nginx/www:/usr/share/nginx/html -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/logs:/var/log/nginx nginx

Start nginx Web
docker start nginx-web

4. Basic use
Version view: docker version
Image pull: docker pull
Image query: docker image
Image deletion: docker RMI nginx
Container run: docker run
Port mapping: docker run – P 3306:3306
Suspend storage volume: docker run – V
Enter container: docker exec it container_ id /bin/bash
View container logs: (1) enter the container to view (2) exit the container to view docker logs – f container_ id
Query container: docker PS (running container), docker PS – A (all containers)
Setting environment variables: setting startup variables, such as setting the password of MySQL root
You can query environment variables in docker_ Query description on hub
Container stop: docker stop container_ id
Container deletion: docker RM container_ id
Warehouse login: docker login
Image build: docker build – t Lala: 1.0 – F / root / dockerfile
Image tag: docker tag MySQL: 5.6 myregistry (name of image warehouse) / MySQL: 1.0
Image push: docker push myregistry (name of image warehouse) / MySQL: 1.0

5. Dockerfile syntax
FROM、RUN、 CMD/ENRTYPOINT、 EXPOSE
From: basic image
Run: commands
Add: copy file
Workdir: set working directory
CMD: run the program in the working directory
Expose: Port 5000