SpringBoot e-commerce project mall (40K + STAR) address: github.com/macrozheng/…
Abstract
Docker is often used to build various environments, simple and easy to use! But sometimes often forget the command, summed up a very practical Docker command, for Java development is basically enough, I hope to help you!
Docker profile
Docker is an open source application container engine that allows developers to package applications and dependencies into a portable image that can then be distributed to any popular Linux or Windows machine. Docker makes it easier to package, test, and deploy applications.
Docker environment installation
- The installation
yum-utils
;
yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code
- Add docker repository location for yum source
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Copy the code
- Install docker service;
yum install docker-ce
Copy the code
- Start the Docker service.
systemctl start docker
Copy the code
Common Docker image commands
Search the mirror
docker search java
Copy the code
Download mirror
docker pull java:8
Copy the code
Viewing the Image Version
Since the Docker search command can only find out whether the image exists, but not the version supported by the image, we need to search the supported version through the Docker Hub.
-
Access the official website of Docker Hub, address: hub.docker.com
-
Then search for the desired image:
- View the versions supported by the image:
- Download the image:
Docker pull nginx: 1.17.0Copy the code
List the mirror
docker images
Copy the code
Remove the mirror
- Delete mirror by name:
docker rmi java:8
Copy the code
- Delete mirror by name (mandatory) :
docker rmi -f java:8
Copy the code
- Delete all unreferenced mirrors:
docker rmi `docker images | grep none | awk '{print $3}'`
Copy the code
- Forcibly delete all mirrors:
docker rmi -f $(docker images)
Copy the code
Packaging image
Image tag. Use the Dockerfile file in the current directoryDocker build-t mall/mall-admin: 1.0-snapshotCopy the code
Common Docker container commands
Create and start the container
docker run -p 80:80 --name nginx \
-e TZ="Asia/Shanghai"\ - v/mydata/nginx/HTML: / usr/share/nginx/HTML \ - d nginx: 1.17.0Copy the code
- -p: maps host ports to container ports in the following format: Host port: container port.
- –name: Specifies the container name. You can then use the container name to manipulate the container.
- -e: Sets the environment variable of the container, where the time zone is set.
- -v: mounts files on the host to the host in the following format: Host file directory: container file directory.
- -d: indicates that the container runs in background mode.
List the container
- List running containers:
docker ps
Copy the code
- List all containers:
docker ps -a
Copy the code
Stop the container
Note: $ContainerName indicates the name of the container, and $ContainerId indicates the ID of the container.
docker stop $ContainerName(or $ContainerId)
Copy the code
Such as:
docker stop nginx
# or
docker stop c5f5d5125587
Copy the code
Force stop container
docker kill $ContainerName
Copy the code
Start the container
docker start $ContainerName
Copy the code
Into the container
- Query the container first
pid
:
docker inspect --format "{{.State.Pid}}" $ContainerName
Copy the code
- Enter container according to the PID of container:
nsenter --target "$pid" --mount --uts --ipc --net --pid
Copy the code
Remove the container
- Delete the specified container:
docker rm $ContainerName
Copy the code
- Delete containers by name wildcard, such as delete by name
mall-
Container at the beginning:
docker rm `docker ps -a | grep mall-* | awk '{print $1}'`
Copy the code
- Forcibly delete all containers;
docker rm -f $(docker ps -a -q)
Copy the code
View the container logs
- View all logs generated by the container:
docker logs $ContainerName
Copy the code
- Dynamically viewing logs generated by containers:
docker logs -f $ContainerName
Copy the code
View the IP address of the container
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $ContainerName
Copy the code
Change the startup mode of the container
Change the container startup mode to always
docker container update --restart=always $ContainerName
Copy the code
Synchronize host time to container
docker cp /etc/localtime $ContainerName:/etc/
Copy the code
Specify the container time zone
docker run -p 80:80 --name nginx \
-e TZ="Asia/Shanghai"Nginx \ - d: 1.17.0Copy the code
View container resource usage
- View the resource usage of the specified container, such as CPU, memory, network, and I/O status:
docker stats $ContainerName
Copy the code
- View the resource usage of all containers:
docker stats -a
Copy the code
View the usage of container disks
docker system df
Copy the code
Execute container internal commands
docker exec -it $ContainerName /bin/bash
Copy the code
Specify the account to access the container
Log in to the container as user root
docker exec -it --user root $ContainerName /bin/bash
Copy the code
Viewing All Networks
docker network ls
Copy the code
[root@local-linux ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
59b309a5c12f bridge bridge local
ef34fe69992b host host local
a65be030c632 none
Copy the code
Creating an External Network
docker network create -d bridge my-bridge-network
Copy the code
Designated container network
Docker run -p 80:80 --name nginx \ --network my-bridge-network \ -d nginx:1.17.0Copy the code
Example Modify the storage location of a mirror
- Docker image:
docker info | grep "Docker Root Dir"
Copy the code
- Disable the Docker service:
systemctl stop docker
Copy the code
- Move the original image directory to the destination directory:
mv /var/lib/docker /mydata/docker
Copy the code
- Establish soft connection:
ln -s /mydata/docker /var/lib/docker
Copy the code
- If you look again, you can see that the image location has been changed.
In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!