Why Docker

  • Packaging a whole set of environments into a mirror, no need to reconfigure the environment, to solve the problems of the environment.
  • Docker containers are process isolated, so no one affects the other.
  • Compared with traditional VM virtualization methods, Docker has the advantages of fast startup speed, high resource utilization and low performance overhead.

Concepts in Docker

  • Image: A Docker Image is an ISO file system. For example, Centos official image centos-7-x86_64-DVD-2009. iso contains a complete Centos operating system environment.
  • Container: The relationship between an Image and a Container is similar to that between classes and instances in Java object-oriented programming. The Image is the definition of the class, and the Container is the instance of the Image at runtime. Containers can be created, started, stopped, deleted, or paused.
  • Repository: A Repository can be viewed as a code control center where images are stored.

CentOS 7 Install the Dokcer

  1. Example Set the Docker image source
yum install -y yum-utils 

yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code
  1. Install the Docker Community version
yum install docker-ce docker-ce-cli containerd.io
Copy the code
  1. Start the Docker
systemctl start docker
Copy the code
  1. Convention HelloWorld: Docker run hello-world

Dokcer Common command

  1. The mirror command
Docker images // Lists all images on the local hostCopy the code
Docker search [image name] // Search imageCopy the code
Docker pull [image name] // Pull the imageCopy the code
Docker rmi-f [image name] docker Rmi-f [image name 1 Image name 2] docker rmi-f [image name 1 image name 2] docker rmi-f [image name 1 image name 2] docker rmi-f $(docker image-q) // Delete all imagesCopy the code
  1. Container order
Docker run - it 】 【 mirror name / / in interactive mode to start the container docker run - d / / mirror name 】 【 set mode to start the container after docker run - d - P random port mapping, Internal port of the container Port randomly mapped to the host Docker run -d -p Specifies the port mapping in the format of host port: container portCopy the code
Docker ps // lists the currently running containersCopy the code
Exit // The container stops and exits CTRL +p+q // The container does not stop and exitsCopy the code
Docker restart docker restart docker restart docker restart docker restart docker restartCopy the code
Docker stop / / stop containers id/name 】 【 container docker kill / / containers id/name 】 【 forced to stop the containerCopy the code
Docker rm / / delete container vessel id 】 【 docker rm -f $(docker ps - a - q) / / delete all containers docker ps - a - q | xargs docker rm / / delete all containersCopy the code
Docker exec-it [container id] [bashshell] docker exec-it [container id] [bashshell] docker exec-it [container id] [bashshellCopy the code
Docker commit [container ID] [Image name: version number] // Create a new image from the containerCopy the code

More commands: # Docker

Dokcer Container data volume

Container data volumes can be used to store the data of Docker applications, and can also be used to share data between Docker containers.

  1. Add a container data volume by running a command
Docker run -d -v host file absolute path: container file absolute path [image name]Copy the code
  1. Add container data volumes using DockerFile

An image with a VOLUME created through DockerFile and then creating a container will automatically mount the VOLUME directory to the default host directory

VOLUME["dataVol1","dataVol2"]
Copy the code
  1. Mount a created data volume from another container
Docker run -d --volumes from [图 片]Copy the code

DokcerFile

DockerFile is a build file used to build a Docker image. It is a script composed of a series of commands and parameters

Docker build -f [dockerFile path] -t [image name] : version number.Copy the code

Related command: # Docker Dockerfile

Install Redis using Docker

  1. Pull the mirror
Docker pull redis: 3.2Copy the code

2. Create a local redis.conf, the purpose is to customize the content of this configuration file to be mapped to the redis configuration file in Docker at startup

mkdir -p /opt/redis/conf
cd /opt/redis/conf
touch redis.conf
Copy the code

Set the password

  1. Start the container
Docker run - p - 6379-6379 - v/opt/redis/data: data - v/opt/redis/conf/redis. Conf: / etc/redis/redis conf - d redis: 3.2 redis-server /etc/redis/redis.conf --appendonly yesCopy the code
  1. Connect the redis