1, Summary of common Docker commands
1. Help command
# View all command details of docker docker --help # View the usage details of the corresponding command of docker docker command --help # For example, check the usage of docker images docker images --help [root@fussy ~]# docker images --help Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show image IDs
2. Mirror command
# Query nginx image from DockerHub docker search nginx [root@fussy ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 16041 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 2104 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of... 820 [OK] # Pull nginx image docker pull nginx # Pull nginx specified version image docker pull nginx:1.9 # View all mirrors docker images -a [root@fussy nginx]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 3 months ago 231MB # Modify the version information of the image (the original image remains unchanged, and a copy image with the modified version information is generated) docker tag Source image:TAG Target image:TAG docker tag centos:latest my/centos:1.0 [root@fussy nginx]# docker tag centos:latest my/centos:1.0 [root@fussy nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 3 months ago 231MB my/centos 1.0 5d0da3dc9764 3 months ago 231MB # View the ID s of all mirrors docker images -aq [root@fussy nginx]# docker images -aq 92b28ed32bef 5d0da3dc9764 # Deletes the specified mirror docker rmi -f image # Delete all mirrors docker rmi -f $(docker images -aq) # Query specified image details docker inspect image docker inspect centos # Build mirror docker build -f File path -t Generated image name:TAG . docker build -f Dockerfile -t myimages:1.0 .
3. Container command
# Start container docker run image docker run centos # Start the container and enter the command line docker run -it image # Start the container and run in the background docker run -d image # Start the container and specify the network docker run --net mynet # Start the container and specify the container name docker run -it --name centos01 --net mynet centos # Start the container and specify the port mapping (multiple can be specified) docker run -p Host port:Container port mirroring docker run -p 3500:80 -p 3600:8080 nginx # Start the container and map ports randomly docker run -P image # Start the container and specify a named data volume (multiple can be specified) docker run -v Host path:Container path mirroring docker run -v /usr/local/nginx/conf:/etc/nginx -v /usr/local/mysql/data:/etc/mysql/data image # View container details docker inspect Container name or container ID # Enter the specified container # The first way (re entering the command line) docker exec -it Container name or container ID /bin/bash docker exec -it 44a231940d74 /bin/bash [root@fussy nginx]# docker exec -it 44a231940d74 /bin/bash [root@44a231940d74 /]# # The second method (enter the last command line) docker attach Container name or container ID docker attach 44a231940d74 [root@fussy nginx]# docker attach 44a231940d74 [root@44a231940d74 /]# # Exit and stop the container exit # Exit without stopping the container ctrl + q + p # View all running containers docker ps # View all containers (including stopped) docker ps -a # View all container ID S docker ps -aq # Delete all containers docker rm -f $(docker ps -aq) # Stop, start container docker stop/start Container name or container ID # Submit the container to the warehouse and become an image docker commit -m Submit information -a Author container name or container ID Image name:TAG docker commit -m 'First submission' -a 'liulusheng' ccff597ebb23 myimages:1.0
4. Network command
# View all networks of docker docker network ls # View the details of the corresponding network docker network inspect Network name or network ID docker network inspect mynet [root@fussy ~]# docker network inspect mynet [ { "Name": "mynet", "Id": "447f6cf844a45e15bc746a7c97736ed61937b19b29f40b3b9d8bb364de26d949", "Created": "2021-12-30T13:48:14.4242933+08:00", "Scope": "local", "Driver": "bridge", ] # Create custom network docker network create -d Network mode (default is bridge Bridge mode) --subnet Subnet mask --gateway Gateway network name docker network create -d bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet # Delete the specified network docker network rm Network name docker network rm mynet
5. Other orders
# View container ip details ip addr [root@44a231940d74 /]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.0.2/16 brd 192.168.255.255 scope global eth0 valid_lft forever preferred_lft forever # Log in to DockerHub warehouse docker login [root@fussy nginx]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: 18879676724 Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded # Exit DockerHub warehouse docker logout # Commit mirror to remote warehouse # The target image name must be in the form of 'DockerHub user name / image name' before it can be published to DockerHub to ensure the uniqueness of the image name. # If the target image name does not meet the requirements, you can use the following command to modify it` docker tag source image: TAG target image: TAG` docker push Target image:TAG docker push 18879676724/centos:1.0 [root@fussy nginx]# docker push 18879676724/centos:1.0 The push refers to repository [docker.io/18879676724/centos] 74ddd0ec08fa: Pushing [===> ] 17.45MB/231.3MB 74ddd0ec08fa: Pushed 1.0: digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc size: 529
2, Dockerfile common commands summary
The script commands in Dockerfile are all capitalized. After writing the Dockerfile file, use docker build -t xxx Command builds an executable image.
Command: docker build -f file path - t image name: TAG
If the Dockerfile script file name is Dockerfile, the - f parameter can be omitted
1. FROM
Customized images are all FROM based images. nginx here is the basic image required for customization
FROM nginx
2. RUN
RUN: the shell command executed when docker build builds the image
FROM nginx # The following steps will create a three-layer image. Note that each RUN will create a new layer image on the docker # Too many meaningless layers will cause the mirror image to expand too much. RUN yum -y install wget RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" RUN tar -xvf redis.tar.gz
FROM nginx # You can use the & & symbol connection command to generate only one layer of image RUN yum -y install wget \ && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \ && tar -xvf redis.tar.gz
3. MAINTAINER
Declare the author and contact information of the image
FROM nginx # Author and contact information MAINTAINER Author 312885991@qq.com
4. COPY
Copy instruction, which copies files or directories from the context directory to the specified path in the container
COPY <Source path 1>... <Target path> # < source path >: source file or source directory. Here can be wildcard expression. The wildcard rule should meet the filepath of Go Match rule # < target path >: the specified path in the container. The path does not need to be bui lt in advance. If the path does not exist, it will be created automatically. COPY hom*.jar /mydir/app.jar
5. ADD
The ADD instruction is similar to the use lattice and function of COPY. The difference is that if the source file is a compressed file, the source file will be decompressed and decompressed to the directory path.
ADD jdk1.8.tar.gz /usr/local/jdk
6. ENV
Set the environment variable and define the environment variable, then this environment variable can be used in subsequent instructions.
ENV <key> <value> ENV <key1>=<value1> <key2>=<value2>... # Example ENV NODE_VERSION 7.2.0 RUN echo $NODE_VERSION
7. EXPOSE
Just declare the port. Help image users understand the daemon port of the image service to facilitate configuration mapping. When random port mapping is used at runtime, that is, when docker run -P, the port of export will be automatically mapped randomly.
EXPOSE <Port 1> [<Port 2>...]
8. WORKDIR
Working path, that is, the default path to enter after the container is started
WORKDIR /usr/local
9. LABEL
The LABEL instruction is used to add some metadata to the image in the form of key value pairs. The syntax format is as follows:
LABEL <key>=<value> <key>=<value> <key>=<value> ... # Example, adding authors LABEL authors="runoob"
10. VOLUME
Define anonymous data volumes. If you forget to mount the data volume when starting the container, it will be automatically mounted to the anonymous volume to avoid the loss of important data due to container restart.
VOLUME ["<Path 1>", "<Path 2>"...] # Mount the contents of / etc/nginx and / etc/mysql/data directories in the container to the host # When starting the container docker run, we can also modify the mount point through the - v parameter VOLUME ["/etc/nginx", "/etc/mysql/data"]
11. CMD
Similar to the RUN instruction, it is used to execute shell command programs, but the running time points of the two are different: CMD runs when the docker run container starts, while RUN runs when the docker build builds the image.
The program specified by CMD instruction can be overwritten by the program specified to run in docker run command line parameter
If there are multiple CMD instructions in Dockerfile, only the last one takes effect.
CMD <shell command> CMD ["<param1>","<param2>",...] # This is written to provide default parameters for the program specified by the ENTRYPOINT instruction
12. ENTRYPOINT
The ENTRYPOINT instruction is basically equivalent to the CMD instruction. It is also executed when the docker run container is started, but it will not be overwritten by the instruction specified by the command line parameters of docker run. Instead, these command line parameters are passed to the program specified by the ENTRYPOINT instruction for execution.
If there are multiple entry point instructions in Dockerfile, only the last one takes effect.
ENTRYPOINT ["<executeable>","<param1>","<param2>",...]
- It can be used with CMD command: CMD is generally used only when parameters are changed. CMD here is equivalent to passing parameters to ENTRYPOINT
FROM nginx ENTRYPOINT ["nginx", "-c"] # Fixed parameter CMD ["/etc/nginx/nginx.conf"] # Variable parameter # 1. Operation without parameter transmission docker run nginx:test # The following commands will be run in the container by default to start the main process nginx -c /etc/nginx/nginx.conf # 2. Parameter transmission operation docker run nginx:test -c /etc/nginx/new.conf # The following commands will be run in the container by default to start the main process nginx -c /etc/nginx/new.conf
3, SpringBoot microservice packaging Docker image
1. Write Dockerfile file
Create a new Dockerfile file and write the script command.
# Based on jdk1 eight FROM java:1.8 # Statement author MAINTAINER author xxx@qq.com # Copy the jar package to the root directory of the running container COPY *.jar /app.jar # Commands executed when the container is running ENTRYPOINT java -jar /app.jar > app.log # Parameters passed to ENTRYPOINT when the container is running CMD ["--server.port=8080"] # It will eventually execute Java - jar / APP jar > app. log --server. port=8080
2. Build image
Place the Jar package and Dockerfile in the same folder
# Build images from Dockerfile files docker build -t app:1.0 .
3. Start the container
# Start the container through the app image and map ports 8181 - > 8080. Then you can access the item through ` host IP:8181 ' docker run -d -p 8181:8080 app:1.0