Docker installation and use tutorial

Docker

Docker installation

1. Uninstall old

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. Install the required environment

sudo yum install -y yum-utils

3. Set the address of the mirror warehouse

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo # is from abroad. It's very slow. Don't use it
    
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # recommend Alibaba cloud

4. Update the index of the yum package

yum makecache fast

5. Install docker

sudo yum install docker-ce docker-ce-cli containerd.io

6. Start docker

systemctl start docker

7. Download the Hello world image and start the container

docker run hello-world

8. Uninstall docker

sudo yum remove docker-ce docker-ce-cli containerd.io

sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

9. Configuring alicloud image acceleration

sudo mkdir -p /etc/dockersudo tee /etc/docker/daemon.json <<-'EOF'{  "registry-mirrors": ["https://xxx.aliyuncs.com"]}EOFsudo systemctl daemon-reloadsudo systemctl restart docker

.

Docker service

  • systemctl start docker: start docker.

  • systemctl stop docker: close docker.

  • systemctl enable docker: set the docker service to startup.

  • Docker version: view the docker version number.

  • docker info: also used to view information.

  • Docker – help: you can view the docker commands.

.

Docker image

  • docker search mysql: query the mysql image.

  • docker rmi -f + image id / image name: version number: forcibly delete the image.

  • docker rmi -f id of image 1 id of image 2: delete multiple images.

  • docker rmi -f $(docker images -aq): delete all images.

  • Docker pull mysql: download version 5.7.32 of mysql.

  • docker images: view all downloaded images.

    • -a: List all local mirrors
    • -q: Only mirror ID s are listed
    • – digests: displays the summary information of the image
    • – no TRUNC: displays the complete image information
  • Where is logstash: check where logstash is.

  • docker tag: labeling an image is equivalent to copying the image and changing its name.

    #For example, now there is one centos image[root@localhost ~]$ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEcentos              latest              1e1148e4cc2c        2 weeks ago         202MB    #I am for centos After developing and developing a version, I can label this version. After labeling, a new image will be generated#Format: docker tag from_image new_image: version number[ root@localhost ~]$ docker tag centos centos:v1    [root@localhost ~]$ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEcentos              latest              1e1148e4cc2c        2 weeks ago         202MBcentos              v1                  1e1148e4cc2c        2 weeks ago         202MB
    

.

Docker container

  • docker ps: view running containers.

    • -a: View all containers.
    • -n: View the containers created n times recently.
    • -q: Only container ID s are displayed.
  • docker stop + container id / container name: close the container.

  • docker start + container id / container name: open the container.

  • docker restart + container id / container name: restart the container.

  • docker kill + container id / container name: forcibly close the container.

  • docker rm + container id: deletes the container.

  • docker rm -f + container id: forcibly delete the container.

  • docker rm -f $(docker ps -aq): delete all containers.

  • docker update + container name / ID -- restart = always: set the container to restart automatically.

  • docker logs -f + container id --tail=1000: only the last 1000 lines of log information are displayed.

  • docker attach + container id / container name / bin/bash.

  • docker exec -it + container id / container name / bin/bash.

  • docker top + container id / container name (view the process of the container).

  • docker inspect + container id (view the details inside the container).

  • docker cp + container id: path of container file to the host = = (copy files inside the container to the host) =.

  • docker cp host file path container id: container internal path = = (copy the host file to the container) = =.

  • docker load -i tar file name: unzip a tar file to the docker repository.

  • docker save image name: tag number - o custom tar file name: print an image into a tar package and save it.

  • Package the container into an image: docker commit -m "description" - a "author information" container id new image name: custom label.

.

Dockerfile

Dockerfile overview

It mainly uses the docker build instruction to create a custom docker image

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-toyaudgx-1638689785981) (D: \ typora images \ image-20210920151252699. PNG)]

  • After the docker client side executes the docker build instruction, it will package the directory where the Dokerfile is located as the above directory and send it to the docker server side at one time. If some files do not want to be packaged and sent in the past, just create a. dockerIgnore file and ignore it. After receiving it, the docker server side will start packaging and building an image.
  • Each line of instruction in Dockerfile will generate an image on the docker server side and store it in the docker cache to facilitate the next reading efficiency; The final image is presented to us.
  • If you don't want to use the last image cache, you can add -- no cache to the docker build instruction, so that you can build it from scratch every time.

Dockerfile common instructions

  • **FROM: * * basic image, indicating which parent image the current image is based on.

  • **MAINTAINER: * * name and email address of the image MAINTAINER (obsolete).

  • **RUN: * * additional shell commands to be executed when the container is built.

  • **Export: * * the port number of the current container exposed to the outside world.

  • **WORKDIR: * * indicates the directory in which the terminal logs in by default after the container is started, that is, the foothold. If it is not specified, it represents the root directory; WORKDIR can have multiple paths. If there is a relative path, the path is relative to the path of the previous WORKDIR instruction.

  • **ENV: * * used to set environment variables during image building.

    ENV MY_PATH /user/mytestWORKDIR $MY_PATH # It means that the destination directory after I run the container is / user/mytest
    
  • **ADD: * * copies the files in the host to the image; If it is a compressed package, it will decompress itself; It can also be a URL address.

  • **COPY: * * the function is the same as that of ADD, but the compressed package will not be decompressed by itself, just a simple COPY.

    COPY/ADD ./market-service.jar /vcloud-market-springboot2.jar
    
  • **VOLUME: * * allows docker internal files to be mounted. If they are not written, they cannot be mounted.

  • **CMD: * * specify a command to run when the container is started. There can be multiple CMD commands in Dockerfile, but only the last command takes effect. The last CMD command will be replaced by the parameters after docker run.

    # For example, running docker run -p 6666:8080 tomcat ls -l will not create the Tomcat container instance. It will implement the ls -l command because the parameters have been replaced
    
  • **ENTRYPOINT: * * the function is the same as that of CMD, except that the parameters after docker run will not be replaced, but will be appended to the command for execution.

    When CMD is used with ENTRYPOINT, the form of json array should be written.

    ENTRYPOINT writes fixed commands and CMD writes constantly changing commands.

  • **onbuild: * * run the command when building an inherited Dockerfile. After the parent image is inherited by the child image, the onbuild of the parent image will be triggered.

  • **USER: * * set which USER can run the dockerfile.

  • **. dockeignore: * * ignore something.

Dockerfile to build centos7 with vim function

FROM centos:7RUN yum install -y vim# The second method: [yum","install","-y","vim "]

Dockerfile to build centos7 with Tomcat compressed package

From centos:7RUN yum install -y vimEXPOSE 5672EXPOSE 15672WORKDIR /zyhWORKDIR bb #WORKDIR It has the function of adding, which is the relative path under the previous path; So the end result is docker Inside the container/zyh/bb Directory COPY ./a.txt /zyh/bb/b.txt # Use relative path ADD https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.71/bin/apache-tomcat-8.5.71.tar.gz /zyh/bb # download the Tomcat compressed package to the / zyh/bb directory of the docker container run tar - zxvf. / apache-tomcat-8.5.71.tar.gzrun MV apache-tomcat-8.5.71 tomcatworkdir tomcatvolume / zyh/bb / Tomcat / webapps # allow the webapps directory inside the docker container to be mounted

Constructing springboot application with Dockerfile

1. First create a springboot project and set the 8080 port number

2. Create Dockerfile file

FROM openjdk:8-jreWORKDIR /docker01ADD docker_demo01-0.0.1-SNAPSHOT.jar demo01.jarEXPOSE 8080ENTRYPOINT ["java","-jar"]CMD ["demo01.jar"]

3. Generate an image and run the docker container

docker build -t demo01:R4.3C01_01_c ./docker run -d -p 8080:8080 --name demo demo01:R4.3C01_01_c #The first 8080 indicates external, and the latter indicates that the browser exposed inside docker accesses xxx.xxx.xx.xx:8080/hello

.

Docker Compose

Docker Compose running example

For example, in addition to the web server itself, a web project often needs to add mysql, redis, es, mq containers, etc. in order to efficiently manage multiple docker containers, many small containers are deployed to form a project and start together.

1. Download compose

# Put it in / usr / local / bin, so we don't need to configure the environment variable sudo curl - L https://github.com/docker/compose/releases/download/1.25.5/docker-compose- `uname -s`-`uname -m` > /usr/local/bin/docker-compose

2. Grant permission to execute

chmod +x /usr/local/bin/docker-compose

3. View version number

[root@xxx bin]# docker-compose -vdocker-compose version 1.25.5, build 8a1c60f6

4. docker-compose.yml file

version: "3.0"services:  tomcat01: #The specified service name is unique    container_name: tomcat01  #Specify a container name that you want to use as run --name    image: tomcat:8.0-jre8    #Who creates the image used by the current service and wants to be run image    ports:      - "8081:8080"           #Host computer:Inside the container, in order to prevent errors, it's best to wrap it in quotation marks run -p    volumes:                  #Specifies the container volume mapping that you want to be on run -v      - tomcat01Volume:/usr/local/tomcat/webapps    networks:                 #Specifies the network bridge to be used as run --network      - vcloudnet01  tomcat02:     container_name: tomcat01    image: tomcat:8.0-jre8    ports:      - "8082:8080"    volumes:                        - tomcat02Volume:/usr/local/tomcat/webapps    networks:                       - vcloudnet01        mysql:    container_name: mysql01    image: mysql:5.7.32    ports:      - "3307:3306"    volumes:      - mysqlLog:/var/log/mysql      - mysqlData:/var/lib/mysql      - mysqlConf:/etc/mysql    networks:                       - vcloudnet01    environment:              #Setting environment variables      - MYSQL_ROOT_PASSWORD=123456      networks:                     #Define the bridges used by the above services. You can write multiple bridges  vcloudnet01:  vcloudnet02:  volumes:                      #Define the name of the mounted data volume required by the above service. You can write multiple tomcat01volume: tomcat02volume: mysqllog: mysqldata: mysqlconf:

5. Just execute docker compose up - D in the same level directory of docker compose. You can visit xxx.xx.xx: 8081 or 8082 in the browser.

docker-compose.yaml configuration file

version

Indicates the version of the corresponding docker, generally 3.0.

build

This is equivalent to docker build, which specifies the Dockerfile file to generate an image.

ports

Equivalent to docker run -p, specify the exposed port number, host: inside the container.

volumes

Equivalent to docker run -v, there are two ways to mount container volumes.

  • Custom assignment: write the absolute path directly.
version: "3.0"services:  tomcat01: #Specify the unique volume of the service name: - / zyh / docker01: / usr / local / Tomcat / webapps
  • Automatic assignment: it will automatically assign you an address on the host for mounting with the files inside the container.
version: "3.0"services:  tomcat01: #The specified service name is unique  volumes:    - tomcat01Volume:/usr/local/tomcat/webapps    volumes: #Define the name of the data volume to be mounted by the above service. You can write multiple tomcatvolumes:

How can I see where this address is assigned?

#Note: the name of the item will be added to the volume name by default( vcloudtest),Then use_Spliced[root@xxx vcloudtest]# docker volume lsDRIVER    VOLUME NAMElocal     docker-compose_tomcat01Volumelocal     docker-compose_tomcat02Volumelocal     vcloudtest_tomcat01Volumelocal     vcloudtest_tomcat02Volume#use docker inspect The instruction can see where the assigned address is[root@xxx ~]# docker inspect vcloudtest_tomcat01Volume[    {        "CreatedAt": "2021-10-10T14:05:45+08:00",        "Driver": "local",        "Labels": {            "com.docker.compose.project": "vcloudtest",            "com.docker.compose.version": "1.25.5",            "com.docker.compose.volume": "tomcat01Volume"        },        "Mountpoint": "/var/lib/docker/volumes/vcloudtest_tomcat01Volume/_data",        "Name": "vcloudtest_tomcat01Volume",        "Options": null,        "Scope": "local"    }]#The file address is right here/var/lib/docker/volumes/vcloudtest_tomcat01Volume/_data lower[root@xxx vcloudtest]# cd /var/lib/docker/volumes/vcloudtest_tomcat01Volume/_data[root@xxx _data]# lsdocs  examples  host-manager  manager  ROOT

networks

Equivalent to docker run --network, used to specify which bridge to use.

version: "3.0"services:  tomcat01: #Unique service name    container_name: tomcat01    image: tomcat:8.0-jre8 #Who is the image used to create the current service    ports:      - "8081:8080"    networks:      - vcloudnet01      networks:                  #Define the bridge used by the above service. You can write multiple vcloudnet01: vcloudnet02:

How do I view the specified bridge name?

#The project name is added by default( vcloudtest),Then use_Spliced[root@xxx vcloudtest]# docker network lsNETWORK ID     NAME                         DRIVER    SCOPE938d7d8b689c   bridge                       bridge    local74d586b7aa3d   docker-compose_default       bridge    local25a8cfc576d4   docker-compose_vcloudnet01   bridge    local63b49b5bfd90   host                         host      local09e53334b1d0   none                         null      local568f617b2843   vcloudtest_default           bridge    local159181bd7772   vcloudtest_vcloudnet01       bridge    local

environment

Setting environment variables, such as MySQL startup, requires specifying the root user password.

version: "3.0"services:   mysql:    container_name: mysql01    image: mysql:5.7.32    ports:      - "3307:3306"    volumes:      - mysqlLog:/var/log/mysql      - mysqlData:/var/lib/mysql      - mysqlConf:/etc/mysql    networks:                       - vcloudnet01    environment:              #Set environment variable - MYSQL_ROOT_PASSWORD=123456

env_file

It is used to replace the environment parameter to prevent sensitive information from being exposed.

version: "3.0"services:   mysql:    container_name: mysql01    image: mysql:5.7.32    ports:      - "3307:3306"    volumes:      - mysqlLog:/var/log/mysql      - mysqlData:/var/lib/mysql      - mysqlConf:/etc/mysql    networks:                       - vcloudnet01    env_file:      - ./mysql.env

mysql.env file:

MYSQL_ROOT_PASSWORD=123456

Create a new mysql.env file (which must end with. Env) in the current project directory, and put the environment variables to be set in the environment into the file in the form of key value.

depends_on

Configuring the service startup depends on which services are started first.

version: "3.0"services:  tomcat01: #Unique service name    container_name: tomcat01    image: tomcat:8.0-jre8 #Who is the image used to create the current service    ports:      - "8081:8080"    networks:      - vcloudnet01    depends_on: #Configuring the service startup depends on which services are started first. Note that the following is the service name, not the container name - tomcat02 - mysql

build

First use the Dockerfile file to generate an image, and then use the image.

version: "3.0"services:  demo01: #Unique service name    build: #First use Dockerfile Generate an image before using it      context: ./docker01 #appoint Dockerfile Context directory    container_name: demo01 #Specify container name ports: "8080:8080" networks: - vcloudnet01 dependencies_on: - tomcat01

Docker compose instruction

  • Docker compose up - D: start all containers defined in yaml in the background.
  • Docker compose down: stop all containers and remove the network.
  • Docker compose exec + Service Name: enter the specified container.
  • Docker compose PS: displays all container information defined in the yaml file.
  • Docker compose restart + Service Name: restart the specified container.
  • Docker compose RM - F + Service Name: deletes the specified container.
  • Docker compose Top + Service Name: view the process information running in the specified container.
  • Docker compose pause + Service Name: pause the specified container.
  • Docker compose unpause + Service Name: recover the specified container.
  • Docker compose logs - F + Service Name: view the logs of the specified container.

.

Doker installing Tomcat

The Tomcat container created with docker only needs to use curl localhost:8081 to get the HTML page. It doesn't matter if the host can't access it with the browser. It's a problem of port mapping.

# The specified path is mounted (not recommended) because it is under the host computer/zyh/tomcat There is nothing in it. By default, it will webapps The data in the directory is empty. You can't access it Tomcat Home page. docker run -d -p 8080:8080 --name:tomcat01 -v /zyh/tomcat:/usr/local/tomcat/webapps tomcat:8.0-jre8# If you do not specify a path to hang on (recommended), it will not only not webapps The data in the directory will be cleared, and the data will be copied to/zyh/tomcat Under the directory, you can access the home page. docker run -d -p 8080:8080 --name:tomcat02 -v tomcatVolume:/usr/local/tomcat/webapps tomcat:8.0-jre8# tomcatVolume Is the name of the data volume. You can docker volume inspect To see where the mapped path is[root@xxx vcloudtest]# docker volume inspect tomcatVolume[    {        "CreatedAt": "2021-12-05T15:03:12+08:00",        "Driver": "local",        "Labels": null,        "Mountpoint": "/var/lib/docker/volumes/tomcatVolume/_data",        "Name": "tomcatVolume",        "Options": null,        "Scope": "local"    }]

Tags: Linux Operation & Maintenance Docker Container

Posted on Sun, 05 Dec 2021 06:38:39 -0500 by zohab