This is the 21st day of my participation in the August Text Challenge.More challenges in August

๐ŸŒˆ Past review

Thank you for reading, I hope to help you, if there is a flaw in the blog, please leave a message in the comment area or add my private chat in the home page, thank you for every little partner generous advice. I’m XiaoLin, a boy who can both write bugs and sing rap

  • ๐Ÿ’–5 minutes introduction to Docker core components ๐Ÿ’–Docker series introductory tutorial
  • ๐Ÿ’–5 minutes with you to run Docker๐Ÿ’–Docker series of introductory tutorials
  • ๐Ÿ’– take 20 minutes per day to get you started on ElasticSearch 4๏ธ discount

Docker installation software

5.1 Docker install MySQL

Pull the mirror

#Mysql 8.0 is installed
docker pull mysql:latest
Copy the code

Start the container

#-e specifies the environment variable
docker run -p 3306:3306 -e MYSQL_DATABASE=workdb -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
Copy the code

Into the container

#The container ID should be replaced according to the actual situation
docker exec -it 3e8bf7392b4e bash
Copy the code

Log on to the MySQL

mysql -u root -p
Copy the code

Change the password

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Copy the code

Authorize remote login access

CREATE USER 'wkcto'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 
GRANT ALL PRIVILEGES ON *.* TO ' wkcto'@'%';
Copy the code

5.2 Docker install Nginx

Pull the mirror

docker pull nginx
Copy the code

Start the container

docker run -d -p 80:80 nginx
Copy the code

Into the container

#The container ID should be replaced according to the actual situation
docker exec -it 3e8bf7392b4e bash
Copy the code

5.3 installing Zookeeper in Dokcer

Pull the mirror

docker pull zookeeper
Copy the code

Start the container

docker run -p 2181:2181 -d zookeeper
Copy the code

Into the container

#The container ID should be replaced according to the actual situation
docker exec -it 3e8bf7392b4e bash
Copy the code

Docker custom image

6.1. Understand the Dockerfile file

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. Dockerfile is used to build a Docker image. A Dockerfile file is composed of a line of command statements. Based on these commands, an image can be built.

6.2 Basic structure of Dockerfile

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. We usually divide a Dockerfile into four parts:

  1. Basic image information.
  2. Maintainer information
  3. Mirror operation instructions.
  4. The container executes instructions when it starts.

6.3 Dockerfile instruction

6.3.1, FROM

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. The first directive in the Dockerfile file must be the FROM directive. In addition, if you create multiple images in the same Dockerfile, you can use multiple FROM directives (each image once). The specified images, such as existing images, are called base images. Syntax format:

FROM Image name#Or is itFROM Image name: version numberCopy the code

6.3.2, MAINTAINER

This command is used to specify the author information in the following syntax:

MAINTAINER Author nameCopy the code

6.3.3, RUN

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. The RUN command will execute the specified command based on the current image and submit it as a new image. If the command is long, use \ to wrap the line. Each RUN command creates a new image on top of the current image to RUN the command in the syntax:

RUN Indicates the command to be executed#demonstration
RUN echo xiaolin
Copy the code

6.3.4, EXPOSE

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. Specify the port used by the container that runs the image. Although we exposed the port number when building the image, we still need to specify the port mapping when running the container. When we use EXPOSE, we only tell Docker that the container running the image will use port 80. For security reasons, Docker does not open this port, so we need to specify the port mapping when running the container using the image. The syntax format is:

EXPOSE the port numberCopy the code

6.3.5, CMD

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. The CMD command provides the commands that the container runs by default, similar to the RUN command. It’s all about executing a command. But the command specified by the RUN command is RUN during the image build process, and the CMD command is RUN while the container is running. If we specify the command to run in the docker run command, the CMD command will be overwritten and the default command will not be executed.

6.3.6, ENTRYPOINT

This command is very similar to the 1CMD command. Unlike the RUN command, it is not overwritten by the start command of docker RUN.

6.3.7, ADD

Copy files and directories to an image built using Dockerfile. The source of the destination can be a local address or a remote address.

If it is a local address, the local address must be a relative address in the build directory. For remote urls, docker does not recommend using curl or wget to obtain them. The destination path must be specified as the absolute path in the image.

6.3.8, VOLUME

Used to add volumes to containers created based on images. A volume can have one or more container-specific directories. This directory can bypass the federated file system. Provides the ability to share and persist data.

6.3.9, WORKDIR

This directive creates a container from the directive and sets the working directory inside the container. Both the ENTRYPOINT and CMD commands are executed in this directory, and we can also use this command to specify working directories for subsequent builds. An absolute path is usually used, and if a relative path is used, the path is passed consistently.

6.3.10, USER

Specify what USER the image will run as. If USER is not specified, the container will run as root. Syntax format:

USER xiaolin
Copy the code

6.3.11, ONBUILD

Mirror triggers, which are executed when an image is used as the base image by another image, insert instructions during the build process.

6.3.12, ENV

The main purpose of this directive is to set the environment variable, which is valid both during build and during run. For example, to configure the JDK environment:

ENV JAVA_HOME /usr/local/jdk1.8.0_121 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar $PATH:$JAVA_HOME/binCopy the code

6.4. Use DockerFile to customize the image

We create a directory and a Dockerfile file

Write a Dockerfile file

from centos:latest
maintainer xiaolin
RUN yum install -y epel-release
RUN yum install -y nginx
EXPOSE 80
Copy the code

Use the build command to build the image

docker build -t 'nginx_mirror' .
Copy the code

Build containers with images

#-p 80 is followed by the name you specified when building
docker run -itd --name nginx_env1 -p 80 nginx_mirror
Copy the code