“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
Common Docker commands
Mirror related commands
Look at mirror
View all local mirrors
docker images
Copy the code
These images are stored in the /var/lib/docker host directory
Search the mirror
To search for an image on the network, run the following command: Note that you must ensure that your current system is networked
Docker Search Image nameCopy the code
Pull the mirror
Pull image: Download the image from the Docker repository to the local PC. The image name format is name: version number. If the version number is not specified, it is the latest version. If you do not know the version of the image, you can go to the Docker Hub to search for the image.
Docker pull image nameCopy the code
For example, I want to download the Centos7 image
docker pull centos:7
Copy the code
Remove the mirror
Delete a mirror by its ID
Docker RMI image IDCopy the code
Deleting all Mirrors
Make sure this image is not used by any container before deleting it. If there is a running docker container, it will report “Error: container_delete: Impossible to remove a running container, please stop it fifirst”
Docker images -q # docker images -q #Copy the code
Container-specific commands
Check the container
View the running container
docker ps
Copy the code
View all containers (view running and stopped)
Docker PS -- a Docker Ps-allCopy the code
View the container that was last run
Docker ps - lCopy the code
View the stopped container
docker ps -f status=exited
Copy the code
Create and start containers
Create container command:
Docker run parameter Image name: image label /bin/bashCopy the code
Common parameters for creating containers are as follows:
-i runs the container. If this parameter is not added, the container is created through the image and is not started.
-t indicates that the command line is displayed after the container is started. After adding these two parameters, the container creation can be logged in. Assign a dummy terminal (if you only add it, it will automatically enter the container).
-d adds the -d argument to run to create a daemon container to run in the background (so that the container is not automatically logged in after being created).
–name Specifies the name of the created container.
-V indicates directory mapping (the former is a host directory, and the latter is a directory mapped to the host). You can use multiple -v to map multiple directories or files. Note: It is best to do directory mapping, make changes on the host, and then share them on the container.
-p indicates port mapping. The former is a host port, and the latter is a mapped port in the container. You can use multiple -p to do multiple port mappings. For example, you can map port 8080 of the Tomcat container in Docker to a certain port 8080 of the host. Then you only need to visit Tomcat in the future: http://ip address of the host: 8080/
After entering the container, initialize the command to execute: /bin/bash; You can write without writing
Interactive container
Run the container interactively. Run the container directly into the container and exit the container directly into the container
This happens only the first time, and starting the container later is a daemon.
Docker runit --name= container name Image name: tag /bin/bashCopy the code
At this time, we use the ps command to check, and we find that we can see the started container, which is in the started state
Exit the current container
exit
Copy the code
Guardian container
Docker run-di --name= container name Image name (or image ID): label /bin/bashCopy the code
Log in to the container/go to the container directory
Docker exec-it container name (or container ID) /bin/bashCopy the code
Note: here the script /bin/bash must be written after logging into the container
Stop and start containers
Stop container:
Docker stop container name (container ID)Copy the code
Start container:
Docker start docker startCopy the code
File copy
The cp command can be used if we need to copy the host files to the container
# create an empty file touch xxx. HTML # copy the file to the tomcat webapps directory docker cp xxx. HTML 59 b35c0bbe6d: / usr/local/tomcat/webapps # switch to the tomcat container to check the docker exec - it tomcat container ID/bin/bashCopy the code
You can also copy files out of the container
Docker cp container name: # container need to copy the file or directory to copy to the tomcat container to copy the files back out docker cp 59 b35c0bbe6d: / usr/local/tomcat/webapps/XXX. HTML. /Copy the code
Directory mount
We can create a container by mapping the host directory to the directory in the container, so that we can influence the container by modifying the files in a directory on the host.
Create container add -v host directory: container directory example:
docker run -di -v /usr/local/myhtml:/usr/local/myhtml --name=mycentos3 centos:7
Copy the code
View the CONTAINER IP address
We can view the various data that the container is running with the following command
Docker inspect Container nameCopy the code
You can also run the following command to output the IP address
Docker inspect - format = '{{. NetworkSettings. IPAddress}}' container name (ID) containerCopy the code
Remove the container
Deletes the specified container. Running containers cannot be deleted
Docker rmI docker rmI docker rmI docker rmI dockerCopy the code
Docker Data Volume
Data Volume Overview
A data volume is a directory or file on the host. After a container directory is bound to a data volume directory, the changes of the container directory are immediately synchronized.
A data volume can be mounted by multiple containers at the same time, and a container can be mounted by multiple data volumes.
Simply put, a data volume is essentially a shared folder, which serves as a bridge for data sharing between the host and the container.
Data Volume function
- Container data persistence
- The external machine communicates indirectly with the container
- Data exchange between containers
Data volume configuration mode
One container is attached to one data volume
When creating a startup container, use the -v parameter to set the data volume
docker run ... -v Host directory (file): directory (file) in the container...Copy the code
Matters needing attention:
- The directory must be an absolute path
- If the host directory does not exist, it will be created automatically
- Multiple data volumes can be mounted
Case study:
Docker run -di --name=c1 -v host_data1:/root/c1_data centos:7 /bin/bashCopy the code
View the data volumes attached to the container
You can run the following command to view the data volumes mounted in the container
Docker inspect Container nameCopy the code
Multiple data volumes are attached to one container
You can run the following command to mount multiple data volumes
docker run -di --name=c1 -v /root/host_data1:/root/c1_data1 -v /root/host_data2:/root/c1_data2 centos:7 /bin/bash
Copy the code
One data volume is mounted to multiple containers
Multiple containers attach one data volume to implement data sharing
docker run -di --name=c2 -v /root/host_data_common:/root/c2_data centos:7
docker run -di --name=c3 -v /root/host_data_common:/root/c3_data centos:7
Copy the code
Multiple containers mount 1 container (this container mounts 1 data volume)
## create and start c3 data volume container, Docker run -di --name=c3 -v /root/host_data_common:/root/c3_data centos:7 /bin/bash Run the --volumes-from command to configure the data volume docker run-di --name=c1 --volumes-from C3 centos:7 /bin/bash docker run-di --name=c2 --volumes-from c3 centos:7 /bin/bashCopy the code
Deploy the software in Docker
MySQL deployment
- Search for mysql mirror
docker search mysql
Copy the code
- Pull mysql image
Docker pull mysql: 5.7Copy the code
- Create containers and set port mapping and directory mapping
docker run -di --name=mysql -p 3307:3306 -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD = 123456 mysql: 5.7Copy the code
Parameter Description:
- -p 3307:3306: maps port 3306 of a container to port 3307 of a host.
- -v /root/mysql/logs:/logs: Mounts the logs directory under the host directory (/root/mysql) to /logs in the container. Log directory
- – v/root/mysql/data: / var/lib/mysql: will host machine directory (/ root/mysql) under the data directory mounted to the container of/var/lib/mysql. The data directory
- -e MYSQL_ROOT_PASSWORD=123456: Initializes the password of user root.
- Enter the container and operate mysql
Docker exec -- it c_mysql /bin/bashCopy the code
- Connect to mysql in the container using Navicat
Tomcat deployment
- Searching for tomcat Images
docker search tomcat
Copy the code
- Pull the Tomcat image
docker pull tomcat:8-jdk8
Copy the code
- Create containers and set port mapping and directory mapping
docker run -di --name=c_tomcat -p 8080:8080 -v /root/tomcat/webapps:/usr/local/tomcat/webapps tomcat:8-jdk8
Copy the code
Parameter Description:
- -p 8080:8080: maps port 8080 of a container to port 8080 of a host
- – v/root/tomcat/webapps: / usr/local/tomcat/webapps: will host directory (/ root/tomcat/webapps) mounted to the container webapps
-
Deploy the service to Tomcat using the FinalShell file upload
-
Use an external machine to access Tomcat to test the deployment service
Nginx deployment
- Search for Nginx images
docker search Nginx
Copy the code
- Pull the Nginx image
docker run -di --name=mynginx -p 80:80 nginx
Copy the code
Redis deployment
- Search for redis images
docker search redis
Copy the code
- Pull the Redis image
docker pull redis
Copy the code
- Create a container and set port mapping
docker run -id --name=c_redis -p 6379:6379 redis
Copy the code
- Using an external machine to connect to Redis, test
Migration and Backup
Docker development environment, install a lot of containers in Docker, carry out the corresponding configuration, persist the running containers in Docker into images, and install the corresponding images into the production environment. 1. Persist the corresponding container in the Docker in the development environment into image 2. Save the image as a compressed package and send it to the production server. 3. In the production environment, the compressed package > image > container is required
The container is saved as an image
We can save the container as an image by using the following command
Docker commit {running container name/container ID} {ImageName} :{image Tag} {ImageName} :{Tag} # example docker commit XXX redis:version_xxx_1.0.0Copy the code
If the mirror label is not written, the default value is latest
Image backup
We can save the image as a tar file by using the following command
Docker save -o docker save -o redis. Tar redis:version_xxx_1.0.0 Restored name and tag # -o: output fileCopy the code
Mirror restoration and migration
First we delete the c_tomcat_bak image and then execute this command to restore it
Docker load -i {backup image file} # example docker load -i redis. Tar # -i: specifies the file to importCopy the code
After executing the command, you can view the mirror again and see that the mirror has been restored. You can run the test again
Docker run - di - name = mytomcat -p 8081:8080 - v/root/tomcat/webapps /, / usr/local/tomcat/webapps redis: version_xxx_1. 0.0Copy the code