Docker [system Environment: CentOS7]

A,Install the docker

Install the docker

Yum makecache Fast yum Makecache Fast yum Makecache Fast

① yum -y install GCC ② yum -y install GCC c++

Then enter the following operations

Yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ yum remove docker-client \ docker-latest \ Docker-latest -logrotate \ docker-logrotate \ docker-engine 2, install yum install -y yum-utils 3, install latest-logrotate \ docker-engine Yum - config - manager \ - add - repo \ [mirror address] / / https://download.docker.com/linux/centos/docker-ce.repo / / foreign address / / http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo / / ali cloud image address (recommended) 4, install docker docker - ce: community edition docker - ee: Enterprise version yum install docker-ce docker-ce-cli containerd. IO 5. Start docker systemctl start docker 6 7, Hello world (official image test) Docker run hello world 8, check docker images downloaded to the local imageCopy the code

Uninstall Docker

  1. Yum remove docker-ce docker-ce-cli containerd. IO // Uninstall docker
  2. Rm -rf /var/lib/docker // delete all images from the docker file.
/var/lib/docker: is the default working path of dockerCopy the code

Configure the use of Ali Cloud image service (remember root permission)

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://x24bnhrm.mirror.aliyuncs.com"]
}
EOF

systemctl daemon-reload

systemctl restart docker
Copy the code

** two, command Xiaoquan

1. Commands in the system

  • Start docker: systemctl start docker
  • Check the docker running status: systemctl status Docker
  • Stop the docker service: systemctl stop the docker service
  • Restart docker: systemctl restart docker
  • Docker: systemctl enable docker

2, docker commands

2.1. Mirror correlation
  • To view the summary of the docker itself (you need to start the docker first) : docker info
  • View the local image: Docker Images
  • Search remote images: docker search Image name
  • Pull image (the latest version can be pulled by default, or a colon can be added to specify the version) : Docker pull image name
  • Delete image (if the image has no container or stop container) : docker RMI image ID or name
  • Delete all images (provided all images can not have containers started) : Docker RMIdocker images -q
2.2. Container related
  • Docker ps (common)

  • View all containers: Docker Ps-A

  • View the container that was last run: Docker PS-1

  • Docker ps -f status=exited

  • Create container: Docker Run

    ·) -i: runs the container

    ·) -t: indicates that the container will enter its command line after it is started

    ·) –name: name the created container

    ·) -v: indicates directory mapping

    ·) -p: indicates port mapping. The former is a host port, while the latter is an in-container port. Multiple P-s can be used for multi-port mapping

    ·) -d: indicates that a daemon container will be created and run in the background, that is, run in the background without entering the corresponding command line. (As opposed to -t)

  • Interactive container creation (create and run foreground) : docker run-it –name= custom container name Image name: label /bin/bash

    • (Once out of the foreground and there are no running processes in the container, the container stops running)
  • Docker run-di –name= custom container name Image name: label

    • (This will stop the container by default if there are no running processes in the container.)
  • Docker exec-it container name /bin/bash

  • Stop container: docker stop container name or ID

  • Start container: docker start Container name or ID

  • Docker cp File or directory to be copied Container name: container directory

    Docker cp Container name: container directory is the file or directory that needs to be copied

  • Directory mounting: docker run-di –name= user-defined name -v Host directory: container directory Image name: label

  • Docker inspect container name or ID

    ), to check the container only the IP (i.e., select what you want data) : the docker inspect – format = ‘{. NetworkSettings. IPAddress}’ container name or id

  • Delete container (must stop container first) : docker RM container name or ID

3. Docker development environment construction

  • Mysql > create mysql > create mysql > create mysql > create mysql > create mysql Docker run -di –name=docker_mysql -p host port: container port -e MYSQL_ROOT_PASSWORD= custom remote connection password
  • Create and start the Tomcat container with port and folder mappings: docker run -di –name=docker_tomcat -p 8008:8080 -v /usr/local/webapps:/usr/local/tomcat/webapps tomcat:latest
    • The address of the host folder is preceded by a colon:This is followed by the folder address in the Docker container (same for ports: before host, after container).
  • Docker run-di –name=docker_nginx -p 80:80 Specifies the image name

4. Docker backup and migration

  • Save the container as image ① : Docker commit Name of the container to be backed up Name of the image after backup
  • Save the image as a tar file ② : Docker save-o package filename backup image name
  • Docker load -i tar file

5, Dockerfile script construction

Dockerfile: Create a new image (development environment, project, etc.) based on the base image (operating system, or a new image built from Dockerfile).

The benefits of creating an image using the Dockerfile script:

·) for developers: a completely consistent development environment can be provided for developers to avoid environmental conflicts.

·) For testers: you can work directly with the image built during development or build a new image from the original Dockerfile file.

·) for operation and maintenance personnel: Seamless migration of applications can be realized during deployment.

Note: You can use Vim to create a Dockerfile directly with a fixed file name

  • FROM image_name:tag: Which base image to use to start the build process // It exists locally before use, if it does not exist, it will be downloaded automatically
  • MAINTAINER Custom username: MAINTAINER the image creator
  • ENV key value: configures environment variables.
  • RUN command: Executes a Linux command. Dockerfile is used to create directories. It is also the core part of Dockerfile.
  • ADD source_dir/file dest_dir/file: copies the files on the host to the container. If only the files are compressed, the files are automatically decompressed during execution.
  • COPY source_dir/file dest_dir/file: Similar to ADD, except that compressed files are not automatically decompressed.
  • WORKDIR path_dir: Sets the working directory. (Based on which directory to work).

Use vim to edit the following content in Dockerfile. All files and folders must already exist, except those under the RUN command.

FROM centos:latest MAINTAINER myzyt WORKDIR /usr RUN mkdir /usr/local/java ADD jdk-8u221-linux-x64.tar.gz /usr/local/java/env JAVA_HOME /usr/local/java/jdk1.8.0_221 ENV JRE_HOME $JAVA_HOME/jre ENV CLASSPATH $JAVA_HOME/bin/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH ENV PATH $JAVA_HOME/bin:$PATHCopy the code

Docker build -t=’jdk1.8′. Docker build -t=’jdk1.8′. Represents the current directory, which specifies the folder where the Dockerfile file resides.

6. Configuration of Docker Registry (uploading to remote repository requires dockerHub account registration firstHub.docker.com/signup)

  • Login: docker login -u username

    Enter the password directly below, similar to the command line login of MySQL service

  • Pull image: Docker pull Registry

  • Create private repository container: docker run-di –name=registry -p 55:5000 registry

  • Edit daemon.json file to make Docker trust private repository address: vi /etc/docker/daemon.json

    ·) Add something to json: “insecure-registries”:[” IP :5000″]

  • Run the systemctl restart command to restart the docker service

  • Upload an image to a private repository:

    · Docker tag jdk1.8 IP :5000/jdk1.8

    ·) Step 2: Start docker private server: Docker start Registry

    · step 3: Upload image: docker push IP :5000/jdk1.8