In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to install and configure Docker", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to install and configure Docker.
1. Introduction
1.1 reasons for emergence
In the process from front and rear development to testing to production, we often encounter a problem. When it is clear that I have no problem running locally, why did I report an error in the test environment or production environment? often this is due to different development, testing and production environments and configurations.
Anyone who has messed with the configuration of the environment knows that it is troublesome to change a system, a virtual machine, a machine, and a new one, which is laborious and time-consuming. Because of the environment and configuration, all kinds of strange Bug always pop up like the gopher in the gopher game.
Docker
Docker provides a good solution to this problem, through mirroring to package the system environment from the bottom to the top, so as to achieve the seamless operation of services across platforms. In other words, when installing, move the specific environment exactly the same, so as to solve the situation that "if you can run on my computer, you can't run in xx".
Another important reason is lightweight, container-based virtualization. The Docker image only contains the runtime environment needed for business operation. A CentOS/Ubuntu basic image is only 170m, because a lightweight host can easily install hundreds of containers.
1.2 what is it?
Docker is a cloud open source project based on the Go language, which has attracted a lot of attention since its release in 2013. Docker allows you to assemble applications as quickly as you can with containers and shields code-level differences as much as possible like standard containers. It packages the application and its dependencies in a file. Running this file will generate a virtual container.
The program runs in this virtual container as if it were running on a real physical machine. With Docker, you don't have to worry about the environment.
This article does not compare the differences and advantages and disadvantages between virtual machines and Docker, every article has, said bad, if you want to know, you can Baidu??, I will not say more here, let's directly see how to install and use it.
two。 Installation & configuration
2.1 install under Mac
Use Homebrew Cask to install directly under Mac:
# Homebrew install $braw cask install docke
Then, after installing the input command, report the error directly!
➜~ docke zsh: command not found: docker # error report
Don't worry when you encounter this error, double-click the Docker application in the application list after installation, enter the password and use this command.
2.2 install under CentOS
Docker requires that the CentOS version must be 6.5 or above before installation.
# install $sudo yum install yum-utils device-mapper-persistent-data lvm2 $sudo yum-config-manager-- add-repo https://download.docker.com/linux/centos/docker-ce.repo $sudo yum install docker-ce # Open Docke $sudo systemctl start docke
You can download the installation package directly on Windows, or you can download the installation package directly to the official website without using Homebrew on Mac. Baidu is full of installation methods, and there is no need to say much about the rest.
3. Simple configuration and running.
3.1 configure Image acceleration
Add a registry-mirrors item to JSON in MacOS's Docker configuration Perferences-> Docker Engine or Windows's Settings-> Deamon as follows
Docker image acceleration configuration
After the configuration, docker info can look up the image acceleration address we configured on the command line.
➜~ sudo docker info... Registry Mirrors: https://reg-mirror.qiniu.com/ http://hub-mirror.c.163.com/ https://registry.docker-cn.com/...
If your system's Docker does not have a client, such as CentOS, you can directly modify the deamon configuration file:
# modify / create docker deamon configuration file $sudo vi / etc/docker/daemon.json # modify the following configuration {"experimental": false, "debug": true, "registry-mirrors": ["https://reg-mirror.qiniu.com"," http://hub-mirror.c.163.com", "https://registry.docker-cn.com"]} # modified: wq restart $sudo systemctl restart docke
3.2 Hello World!
And then we can run happily. Our first Docker instruction, Hello World.
Docker ran to Helloworld.
Good start!
4. Image & Container & Warehouse
The relationship between an image and a container is like a class and an instance of a class. An image can run multiple containers at the same time, and a single container instance can create a new image. As shown below:
Image container repository
Let's explain the elements that appear in this diagram.
Concept description Docker image Images is used to create read-only templates for Docker containers, such as Ubuntu 16.04 system, Nginx 1.16.0, etc., is a special file system, including programs, libraries, resources, parameters, etc., but does not contain any dynamic data, and the content will not be changed after construction. A single image can create multiple containers, Docker containers, Container containers are one or a group of applications that run independently and are isolated from each other. Is a running instance created by an image, which is essentially a process Can be seen as a simple version of the Linux environment + applications running in which the Docker client Client client uses the command line or other tools to use Docker SDK (https://docs.docker.com/develop/sdk/) communicates with the Docker daemon process Docker host Host a physical or virtual machine used to execute the Docker daemon and container Docker repository Repository centralized storage of image files, divided into public warehouse and private warehouse. Docker registration server Registry is a service that centrally stores and distributes images, officially called Docker Hub. A Docker Registry can contain multiple repositories, and each repository can contain images of multiple tags Tag. Different tags correspond to different versions. Docker MachineDocker Machine is a command line tool that simplifies Docker installation. Docker can be installed on the corresponding platform through a simple command line, such as VirtualBox, Digital Ocean, Microsoft Azure.
Diagram of the life cycle of the container
The life cycle of the container
The five core states of the container, which are represented by the color blocks in the figure: Created, Running, Paused, Stopped, Deleted:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Created: the container has been created, and the relevant resources required by the container are ready, but the program in the container is not yet running.
Running: the container is running, that is, the application in the container is running.
Paused: the container is paused, indicating that all programs in the container are in a paused (not stopped) state.
Stopped: the container is stopped, the occupied resources and sandboxie environment still exist, but the applications in the container have been stopped.
Deleted: the container has been deleted, and the related resources and management information stored in Docker have been released and removed.
This article mainly focuses on the use, and does not elaborate on the switching of these states. Let's start directly below.
5. Basic use
5.1 Operation Command
# enable Docker Boot self-start $sudo systemctl enable docke # turn off Docker Boot self-start $sudo systemctl disable docke
5.2 Mirror command
# to download the image, first look for it locally, do not go to the image, and finally do not go to hub. The tag does not write default to lastest $docker pull [image name]: [tag Tag] # lists all local image files,-a shows all local images (including intermediate images),-Q only shows image ID -- digests displays summary information of the image $docker image ls $docker images # Deletes the image file,-f forcefully deletes the image $docker rmi [image name] [: tag Tag] $docker rmi [image name 1] [: tag Tag] [image name 2] [: tag Tag] # Delete multiple $docker rmi $(docker ps-a-Q) # delete all, followed by a subcommand # query the image name -- no-trunc displays a complete description of the image,-- filter=stars=30 lists images with star no less than the specified value,-- filter=is-automated=true lists automatically built type images $docker search [keyword] # download image, tag tag defaults to lastest, or you can add it yourself, such as: 3.2.0$ docker pull [image name] [: tag Tag]
5.3 Container commands
# list the containers that are running on this machine,-a list all containers on this machine, including those that are terminated, and-Q silent mode displays only the container number -l displaying the recently created container $docker container ls # is equivalent to the following command $docker ps # create and launch container $docker run [option] [container name] # launch container $docker start [container ID] / [container Names] # restart container $docker restart [container ID] / [container Names] # terminate container operation $docker kill [container ID] # forcibly terminate It is equivalent to sending a SIGKILL signal to the main process in the container, and those ongoing operations will lose all $docker kill $(docker ps-a-Q) # forcibly terminate all containers $docker stop [container ID] # and send a SIGTERM signal to the main process in the container. Then send the SIGKILL signal $docker stop $(docker ps-a-Q) # stop all containers # stop running container files after a period of time, it will still occupy hard disk space, you can delete it using the docker container rm command,-f force delete containers that are running $docker rm [container ID] $docker rm `container-aq` # delete all containers that have stopped Because the unstopped rm cannot be deleted, you need to add-f # to view the output of the container,-t to add a timestamp,-f to print with the latest log,-- tail number to show the last number, and if docker run, do not use-it Use this command to view the output $docker logs [Container ID] # View Container process Information $docker top [Container ID] / [Container Names] $docker port [Container ID] / [Container Names] # exit Container $exit # Container exit ctrl + p + Q # Container exit, shortcut key # enter Container $docker attach [Container ID] # exit the container will stop Input from this machine directly into the container $docker exec-it [container ID] # does not stop the container when exiting the container, executes the command in the running container, and does not create and start a new container # set the container to start $docker container update-- restart=always [container name] automatically when docker starts
In particular, I would like to talk about docker run's option, because it is most commonly used:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
-- name specifies a name for the container
-d the container goes to the background after startup, and returns the container ID, that is, starts the guarded container.
-P random port mapping
-p 80pur8080 maps local port 80 to port 8080 of the container
The first command executed internally after the bash container is started. Start bash here to ensure that users can use Shell
-I runs the container in interactive mode, usually in conjunction with-t
-t reassign a pseudo-input terminal to the container, the Shell of the container is mapped to the current Shell, and then the commands entered in the local window are passed into the container, usually used with-I
-- rm automatically deletes container files after the container terminates
-- restart=always sets container self-startup
-v / xxx:/yyy mapping command to map the local xxx directory to the yyy directory in the container, that is, to change the contents of the local xxx directory, the contents of the container yyy directory will also be changed.
For example, I run a CentOS Docker container under CentOS:
# download $docker pull centos # based on the centos image downloaded above, create a new centos instance named mycentos0901, and enter the container's bash $docker run-it-- name mycentos0901 0d120b6ccaa8 [root@169c9fffeecd /] # to enter the container, and enter the command below. Note that a string of ID $ls # behind root can see the list of centos root files $docker # bash: docker: command not found this container does not have docke installed
Isn't it amazing that we can view the container list by executing docker ps under CentOS at the beginning:
Image-20200901225909737
You will find that the ID above is the ID of the container running in the list below. The mirrored ID is also the CentOS image ID under the pull in front of us, and the name is also our mycentos0901.
If you report to Conflict after docker run. The container name "xxxx" is already in use by container just run docker rm $(docker ps-a-Q) to delete stopped containers, or delete docker rm [containerID] precisely, and that's fine.
5.4 Command usage for several common scenarios
Guarded startup container
Use centos to start a container docker run-d-name mycentos0903 0d120b6ccaa8 in background mode. After startup, check docker ps-an and find that the container is not running. This is because of the running mechanism of Docker: the Docker container runs in the background, and there must be a foreground process.
If the commands run by the container are not those that have been suspended all the time, such as top, tail, the end of the run will exit automatically. So in order for the container to run continuously in the background, the running program needs to be run as a foreground process.
For example, here you run a command in the background, which keeps printing docker run-d centos / bin/sh-c "while true; do echo hello zzyy; sleep 2; done", and then let's logs check:
Docker_logs
Operate on the container after exiting the container
After exiting the container, you can use the exec method to manipulate the running container:
Image-20200911142617186
Copy files to the outside in the container
Copy files using the cp command
$docker cp [container ID] / [container Names]: [file directory to be copied] [local directory] # Container files are copied to the local $docker cp [local directory] [container ID] / [container Names]: [file directory to be copied] # Local files are copied to the container
Cp can copy not only the files / folders in the container to the local machine, but also the files / folders in the local machine to the container.
To demonstrate, first create a boring file xixi.txt in the container, and then copy it to the local machine:
Image-20200921210352644
When practical, we can copy configuration, logs and other files to the local.
6. Install MySQL
# query image $docker search mysql # download image. It will be slow when image acceleration is not configured. It is better to configure $docker pull mysql # View image $docker images # create and run container $docker run-d-p 3307 MYSQL_ROOT_PASSWORD=888888-v / Users/sherlocked93/Personal/configs/mysql.d:/etc/mysql/conf.d-- name localhost-mysql mysql
Explain the above parameters a little bit:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
-p 3307 3306 maps port 3307 of the local machine to port 3306 of the mysql container, and change it as needed
-e MYSQL_ROOT_PASSWORD=
Set the root user password for remote login
-- name
Optional, set container alias
-v xxx/mysql.d:/etc/mysql/conf.d maps the settings folder under the local directory to the container's / etc/mysql/conf.d
-v xxx/logs:/logs mounts the logs directory under the local specified directory to the / logs of the container
-v xxx/data:/var/lib/mysql mounts the data directory under the host directory to the / var/lib/mysql of the container
Run the screenshot:
Install Mysql
Then go to Navicat and connect to MySQL.
This is awesome! It's really a few lines of orders, and it's much happier than before.
7. Install Nginx
Nginx installation and other similar, if you do not know how to use Nginx, you can refer to this article, read the basic understanding of how to use and configure.
# query / download image $docker search nginx $docker pull nginx
Image-20200922203203685
Then create a temporary container to copy the default configuration to the local machine. Here, I put the configuration file in the / mnt directory, mainly three configuration folders:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
/ etc/nginx place Nginx configuration file
/ var/log/nginx/ places Nginx log files
/ usr/share/nginx/html/ places Nginx front-end static files in this folder
Copy all these directories to the nginx, nginx_logs, and html folders under the / mnt folder on your local machine.
The temporary container you just created is useless docker rm-f [temporary container ID] kill the temporary container, and then docker run re-create the Nginx container:
$docker run-d-- name localhost-nginx-p 8082 mnt/nginx:/etc/nginx 80\-v / mnt/nginx:/etc/nginx\-v / mnt/nginx_logs:/var/log/nginx\-v / mnt/html:/usr/share/nginx/html\-- privileged=true nginx
-- privileged=true means that the container has privileges such as read and write to the mounted directory.
Other configurations have been mentioned above, so there should be no need to talk about them.
Image-20200922204931582
Then you can access it in your own browser. If it is a CVM, remember to open the corresponding port.
8. Install Easy Mock
Because Easy Mock relies on Redis and MongoDB, it should be a best practice for the local environment to use docker-compose to build Easy Mock.
Install docker-compose
Official document: https://docs.docker.com/compose/install/
First of all, you have to make sure you have a docker environment. If you are a Windows / Mac user, if you install the client, you will bring your own docker-compose.
Since we are building on the CVM CentOS7.6, we need to install docker-compose by ourselves and run the following command to download the current stable version of docker-compose
$sudo curl-L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname-s)-$(unam
Modify file permissions to executable files
$sudo chmod + x / usr/local/bin/docker-compose
Verify that the installation is successful
$docker-compose version
Write docker-compose.yml configuration file
You can refer to the deployment documentation given in the official documentation, or you can refer to my configuration process below.
First create a new file docker-compose.yml and copy the contents of the following docker-compose file into docker-compose.yml, and then replace the comment location in the content with the local address you need
Version:'3' services: mongodb: image: mongo:3.4.1 volumes: # / apps/easy-mock/data/db is the address where the database files are stored Modify it to the local address as needed -'/ apps/easy-mock/data/db:/data/db' networks:-easy-mock restart: always redis: image: redis:4.0.6 command: redis-server-- appendonly yes volumes: # / apps/easy-mock/data/redis is the address where redis data files are stored Modify it to the local address -'/ apps/easy-mock/data/redis:/data' networks:-easy-mock restart: always web: image: easymock/easymock:1.6.0 # easy-mock as needed. Here is npm start. Change it to npm run dev command: / bin/bash-c "npm run dev:server" ports:-7300 npm run dev:server 7300 # to your desired mapping volumes: # log address, and change it to the local address -'/ apps/easy-mock/logs:/home/easy-mock/easy-mock/logs' networks:-easy-mock restart: always networks: easy-mock:
Start Easy Mock
In the docker-compose file directory, run the following command:
$docker-compose up-d
If you encounter a file permission error reported by an easymock docker instance
Error: EACCES: permission denied....
To execute the following command in the project root directory
$chmod 777 / yourfile/logs
Then you can access easy-mock through your domain name .com: 7300 on the browser!
If you think the domain name followed by the port number is ugly, you can access your deployed easy-mock by configuring the secondary domain name of Nginx. See this article for the method of configuring the secondary domain name.
9. Visual management
With regard to the visual query tool, here is a simple introduction of a LazyDocker, because it runs on the terminal, and supports keyboard operations and mouse clicks, it is very irritating, with this some query statements can be typed a few times less.
Lzd
The installation is relatively simple, run the following command:
$docker run-- rm-it-v\ / var/run/docker.sock:/var/run/docker.sock\-v ~ / .config/lazydocker:/.config/jesseduffield/lazydocker\ lazyteam/lazydocke
You can set the alias of a terminal
$alias lzd='docker run-- rm-it-v / var/run/docker.sock:/var/run/docker.sock-v / .config/lazydocker:/.config/jesseduffield/lazydocker lazyteam/lazydocker'
Then you can browse your image, container, log, configuration, status, and so on by typing lzd on the terminal.
At this point, I believe you have a deeper understanding of "how to install and configure Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.