This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Life is short. Let’s learn Python

preface

In the previous article, I introduced the mirroring commands, which are natural to run with the mirror. This article introduces the container commands

Run the commands associated with the container

Ps: Commands about container operations This section uses Tomcat as an example.

docker pull tomcat  Download the Tomcat image
Copy the code

Docker run Image name :TAG(image ID) : the simplest way to run containers through images. Containers are isolated from each other, and tomcat service cannot be accessed through the outside world at this time. Therefore, a series of parameters need to be added after this command. Use docker run –help to view the options for the Docker run command

-p Host port: service listening port in the container: the host port can be mapped to a port in the container. Multiple ports can be mapped. To meet the requirements of accessing the Tomcat service on the host. Start the Tomcat service again, and you can access the Tomcat service from the host. However, N tomcat services can be started through this method. However, it should be noted that port 8080 of the host has been occupied, and it can no longer be used to map docker container with port 8080 of the host. Other ports can be selected.

Host port: Container service port mirroring name :TAG

docker run -p 8080:8080 tomcat:latest
docker run -p 8081:8080 tomcat:latest
...
Copy the code

-d: starts the container for port mapping and runs services in the background. There is a problem with port mapping only, as using the shortcut CTRL + C will stop the container, and the -d option will start the service in the background.

Docker run -d -p host port: container service port mirroring name :TAG

docker run -d -p 8082:8080 tomcat:latest
Copy the code

–name Unique name: start the container, specify the name to the current container, port mapping, background start. Specify a name for the container when it is run. The container name is unique.

Docker run -d -p Host port: container service port --name: container name unique image name :TAG

docker run -d -p 8083:8080 --name tomcat01 tomcat:latest
Copy the code

View commands related to containers

Docker PS: View the currently running containers. The following table shows the meaning of each “field” in the result. This command also has options.

Docker ps -a: View all containers, both running and not running.

Docker ps -q: View all running container ids.

Docker ps -aq: view all container ids.

CONTAINER ID Container unique ID
IMAGE Which image to run the resulting container on
COMMAND Execute commands inside the container
CREATED Container creation time
STATUS Current status of the container. Up indicates that the container is running
PORTS Port that the current service is listening on in the container
PORTS Container name. If you don’t specify Docker, you’ll specify a random name

Stop | restart container related commands

Docker start Container name or container ID: opens the container

Docker restart Container name or container ID: restarts the container

Docker stop Container name or container ID: Stops the container normally

Docker kill Container name or container ID: Stops the container immediately

Container to delete

Docker rm containers name | id (able to distinguish between container) : stop deleting a container, if you remove a running container complains

Docker rm -f container name | container id: mandatory delete container, the container can be run

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

View the service run logs in the container

Docker logs container name | container id: the background of service can’t view the log, so you can view through the command log, no real-time display effect

Docker logs -f container name | container id: real-time monitoring on the log

Docker logs – tf container name | container id: real-time display the log in time at the same time

Docker logs – tail N container name | container id: N is arbitrary Numbers, check the container log at the end of the line N

View the processes in the container

Docker top container name | container id: check the container of all processes

Interact with the inside of the container

Docker exec – it container name | container id bash: said in interactive mode (it) into the container and interact with the orders of the container terminal. Bash represents the terminal inside the container, similar to the command line on Linux systems.

Exit: exits the container.

File interaction between the operating system and the container (transferring files to each other)

Docker cp Container ID | container name: resource path in the container Operating system path: copy files in the container to the operating system.

docker cp tomcat01:/usr/local/RUNING.txt ./RUNING.txt  # Copy files
docker cp tomcat01:/usr/local/webapps ./webapps   # Copy directory
Copy the code

Docker CP Operating system path Container ID | container name: Inside the container Destination path: Resources on the host are copied to the container.

docker cp a.txt tomcat01:/usr/local/
Copy the code

View the details inside the container

Docker INSPECT Container ID | container name: Allows you to view information inside the container, such as data volume mounting, network information, and port information.

Data volume (volume)

The core role of the data volume is to achieve the book host system and the container between the file sharing, specifically can achieve a directory in the host machine can directly affect the corresponding directory in the container. The container is required to bind data volumes at startup. There are two types of data volumes.

The first way – customize the data volume

To start the container, run the docker run -d -p 8080:8080 –name tomcat01 -v host directory: container directory tomcat:latest. Note that the host directory must be an absolute path. In this way, if the path inside the container contains data, the data will be overwritten by the corresponding data on the host.

docker run -d -p 8080:8080 --name tomcat01 -v /root/apps:/usr/local/webapps tomcat:latest

If there are no files or data in the host directory, then the data in the corresponding directory in the container will be emptied
Copy the code

The second way – automatic data volume

Hosting the directory may not be specified, by any name, the name is the alias data volume, the alias of the directory does not exist in the host machine, docker will automatically to aa to construct a path on the host, and the directory is empty, have an advantage is that use this way, because the host machine directory is empty, All files in the corresponding directory in the container will be copied out and put on the host directory. Such as:

docker run -d -p 8080:8080 --name tomcat01 -v aa:/usr/local/webapps tomcat:latest

# aa represents the name of the data volume. The name is arbitrary
When starting the container, all contents in the container directory corresponding to AA will be copied to the host directory corresponding to AA
Find / -name aa
Copy the code

The biggest benefit of using automatic data volumes is data protection. When you start a container using an automatic data volume directory, all the data in the directory will be copied to the mapping directory of the host. In this way, even if the container is dead, the data in the container will not be lost.

The container is packaged as an image

Docker commit -m “description” -a “author information” ID of the container to be packaged | name New image name: tag: The image is packaged according to the existing container

docker commit -m "my tomcat" -a "author"Tomcat01 mytomcat: 1.0Copy the code

The backup image

Docker save Image name: tag-o File name: Back up the image to a file and save it in the current directory.

Docker save mytomcat:1.0 -o mytomcat:1.0.tarCopy the code

Import the packaged tar image file to the local Docker repository

Docker load -i xxx.tar: loads the packaged tar file to the local Docker repository

docker load -i mysql.tar
Copy the code

conclusion

The article was first published in the wechat public account program Yuan Xiaozhuang, synchronized with nuggets.

Please explain where it came from. If you pass by, please put out your cute little finger and click like before you go (╹▽╹)