An article on learning to use docker (super detailed)

1. Introduction to docker
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, and then publish it to any popular Linux or Windows operating system machine. In virtualization, containers use a sandbox mechanism completely, and there will be no interfaces between them.
docker image

https://hub.docker.com/
2. One-click installation of docker linux system

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

3. Common commands of docker service

stop docker service

systemctl stop docker

Start the docker service

systemctl start docker

Restart the docker service

systemctl restart docker

Start the docker service on boot

systemctl enable docker

View docker service status

systemctl status docker

Four, docker common commands

Version

docker version
docker info

pull command

docker pull 镜像名 
docker pull redis
docker pull redis:版本号

View mirror list

docker images

View running images

docker ps
docker ps -a

run mirror

Parameter analysis

-d Hang background operation

-p mapping port host port:container port

--rm automatically delete the container after closing the container

docker run -p 80:80 88736fe82739

multi-terminal mapping

docker run -p 80:80 nginx

Run the specified version number

docker run -p 80:80 nginx:1.22.1

Automatically delete the container after closing it

docker run --rm -p 80:80 nginx:1.22.1 

The docker service starts automatically

docker run --restart -d -p 80:80 nginx:1.22.1 

enter the mirror

docker exec -it 7a91e6f458bb /bin/bash

stop mirroring

docker stop name/id

delete mirror

docker rmi -f name/id

mirror rename

docker tag id 名称:版本号
如:docker tag 8d28fc6920f9 demo:0.01

Transfer files from the host to the container

docker cp AdventureWorksDW2019.bak id	:/tmp/AdventureWorksDW2019.bak

5. Package image command

View the REPOSITORY (warehouse name) to be packaged

docker images 

Package the image, which can be moved to other places for use

docker save redis > redis.tar
docker save --output redis.tar redis
docker save -o redis.tar redis

redis:latest REPOSITORY (warehouse name):TAG

docker save -o redis.tar redis:7.0.5

 # 将打包好的镜像拷贝到新机器上面,执行load命令解压,执行命令
docker load -i redis.tar     
docker load < redis.tar 
六、使用docker-compose.yml创建启动docker容器
version: '2.1'
services:
  mysql888:
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=Root123456
    ports:
      - "13306:3306"
    volumes:
      - /tmp/mysql:/var/lib/mysql
  nginx1:
    image: nginx:1.22.1
    restart: always
    ports:
      - "10080:80"
    volumes:
      - /tmp/nginx:/usr/share/nginx/html:ro

  nginx2:
    image: nginx:1.22.1
    restart: always
    ports:
      - "10082:80"
    volumes:
      - /tmp/nginx:/usr/share/nginx/html:ro

  nginx3:
    image: nginx:1.22.1
    restart: always
    ports:
      - "10083:80"
    volumes:
      - /tmp/nginx:/usr/share/nginx/html:ro

explain

version: '2.1'
services:	
  # 容器名称
  mysql8:
  	# 要拉取的镜像名称与版本
    image: mysql:latest
    # docker服务开机自启容器
    restart: always
    # 环境变量
    environment:
      - MYSQL_ROOT_PASSWORD=Root123456
    # 端口映射 宿主机:容器
    ports:
      - "13306:3306"
    # 挂载目录
    volumes:
      - /tmp/mysql:/var/lib/mysql
  # 第二个容器
  nginx 1.22.1:
  	# 要拉取的镜像名称与版本
    image: nginx:1.22.1
    # docker服务开机自启容器
    restart: always
    # 端口映射 宿主机:容器
    ports:
      - "10080:80"
    # 挂载目录 :ro 表示只读 read-only system
    volumes:
      - /tmp/nginx:/usr/share/nginx/html:ro

Seven, docker-compose command

create and start

docker-compose up

stop and remove

docker-compose down

stop

docker-compose stop

Guess you like

Origin blog.csdn.net/qq_41588098/article/details/131976156