Today is my fourth article in the nuggets! Thank you for coming to read, and hope to continue to pay attention in the future, I will export more technical dry goods, we make progress together! Interested can click on my profile picture to view the history of dry goods!
In the future, it will be divided into several major projects, such as concurrent projects, source code projects, interview projects, etc. (only the dry stuff will be shared).
Please welcome our star of the day: Docker
Docker origin
Before we share the dry goods, we need to know who this big brother is
Docker was born in 2013.
Docker is an open source container engine that helps deliver applications faster. Docker isolates the application from the infrastructure layer and manages the infrastructure as a program. With Docker, applications can be packaged, tested, and deployed faster, and the cycle time from writing to deploying and running code can be shortened.
This is the Maven central repository, where you put your packages as images and anyone can download them and run them.
Learn more about Docker
Let’s start with a picture
This figure vividly describes the overall architecture of Docker, and we will explain it one by one. Let’s start with the part marked 2 in the figure, which contains three modules:
- Docker daemon
It should be noted in advance that Docker can be understood internally as a normal Linux operating system. So the Docker daemon is a background process that runs on a docker-host. You can communicate with it through the Docker client.
- Images (Docker Images)
A Docker image is a read-only template that contains instructions for creating a Docker container. It is similar to the system installation CD-ROM, which can be used to install the system. In the same way, the Docker image can be used to run the programs in the Docker image. Any set of programs can become an image (template), and use Docker to run its own internal program (with normal operating system, naturally can run the program)
- Container (Container)
A container is a runnable instance of an image. The relationship between a mirror and a container is somewhat similar to the relationship between a class and an object in object orientation. Containers can be started, stopped, moved, or deleted using the Docker API or CLI commands. It is important to note here that the container is a running instance of the image, not a dependency, not a dependency, but a JVM and JDK. The JVM is the runtime environment, and without this environment, the JDK will not run. But the vessel is a set of vessels that nurture the mirror image of you, and I am the example of your mirror production.
Next, look at the part marked 1 in the figure above:
- Registry
Docker Registry is a service that centrally stores and distributes images. Once the Docker image is built, it is ready to run on the current host. But if you want to run the image on another machine, you need to manually copy it. At this point, you can use Docker Registry to avoid manual replication of the image.
A Docker Registry can contain multiple Docker repositories, each repository can contain multiple image tags, and each tag corresponds to a Docker image. This is similar to Maven’s repository. If The Docker Registry is compared to Maven repository, the Docker repository can be understood as the path of a CERTAIN JAR package, while the image label can be understood as the version number of the JAR package.
Finally, say part 3 in the figure below:
- Client (Docker)
The Docker client is Docker’s user interface, which accepts user commands and configuration identifiers, and communicates with the Docker Daemon. In the figure, docker build and other commands are related to Docker.
Docker installation (actual combat)
Docker is an open source commercial product that comes in two versions: Community Edition (CE) and Enterprise Edition (EE). The Enterprise edition includes a number of paid services that individual developers don’t usually use. The following introductions are for the community edition. Please refer to the official documentation for the installation of Docker CE. Here, CentOS is used as an example:
- Docker requires CentOS to have a kernel version later than 3.10
Run the uname -r command to check your current kernel version
uname -r
- Log in to the Centos as the root user. Make sure your yum package is up to date.
yum -y update
- Uninstalling an older version (if an older version has been installed)
yum remove docker docker-common docker-selinux docker-engine
- Install the required packages, yum-util provides the yum-config-manager functionality and the other two devicemapper drivers depend on
yum install -y yum-utils device-mapper-persistent-data lvm2
- Set up the YUM source and update the yum package index
Yum – config – manager – add – repo mirrors.aliyun.com/docker-ce/l…
yum makecache fast
- You can view all docker versions in all repositories and select a specific version to install
yum list docker-ce –showduplicates | sort -r
- Install the docker
Yum -y install docker-ce-18.03.1.ce # yum -y install docker-CE-18.03.1. ce # yum -y install docker-CE-18.03.1. ce #
Yum -y install docker-ce # yum -y install docker-ce #
- Boot and add boot
systemctl start docker
systemctl enable docker
- Verify that the docker installation is successful (both client and service parts indicate that the docker installation and startup are successful)
docker version
- Uninstall the docker
yum -y remove docker-engine
Docker common command – image installation
- Search the mirror
You can use the docker search command to search for images stored in Docker Hub. After executing this command, Docker will search for image repositories containing the keyword Java in Docker Hub.
docker search java
The above list contains five columns with the following meanings:
- NAME: indicates the NAME of the image repository.
- DESCRIPTION: Indicates the DESCRIPTION of the mirror warehouse.
- STARS: The number of mirror repository favorites, indicating the popularity of the mirror repository, similar to GitHub’s Stars0
- OFFICAL: Indicates whether it is an official repository. Images marked [0K] in this column are created and maintained by the official project team of each software.
- AUTOMATED: indicates whether the image warehouse is automatically built.
Restart the Docker service:
sudo systemctl daemon-reload
sudo systemctl restart docker
- Download mirror
Use the docker pull command to download the image from Docker Registry. After executing this command, Docker will download the latest version of the Java image from the Java repository in Docker Hub. If you want to download a specific version, add a colon after Java to specify the version, for example: Docker pull Java :8
docker pull java:8
- List the mirror
Use the docker images command to list the images that have been downloaded
docker images
The meanings of the preceding list are as follows
- REPOSITORY: indicates the name of the REPOSITORY to which the image belongs.
- TAG: mirror TAG. The default value is “latest”.
- IMAGE ID: IMAGE ID, which indicates the unique identifier of the IMAGE.
- CREATED: indicates the time when the image is CREATED.
- SIZE: indicates the SIZE of the mirror.
- Deleting a Local Mirror
Use the docker rmi command to delete the specified image
Docker RMI Java (force delete + -f)
Delete all images (Caution)
docker rmi $(docker images -q)
Docker common commands – container start
- Create and start a container
To create and start a container, use the following docker run command. This is the most commonly used command. It has many options.
-d option: indicates running in the background
-p option: indicates random port mapping
-P option: specifies the port mapping in the following four formats.
— ip:hostPort:containerPort
— ip::containerPort
— hostPort:containerPort
— containerPort
The –net option: specifies the network mode. This option has the following optional parameters:
–net=bridge: The default option to connect to the default bridge.
–net=host: The network on which the container uses the host.
–net=container: name-or-id: tell Docker to make the new container use the network configuration of the existing container.
–net= None: The network of the container is not configured. Users can customize the network configuration
Let’s try it on nginx. As with the Java download above, the image installation steps are omitted. The startup command is as follows:
docker run -d -p 91:80 nginx
This will start an Nginx container. In this case, two parameters are added to Docker run, which have the following meanings:
-d Background run -p Host port: Container port # Open container port to host port
Visit http://Docker host IP:91/ and you will see the nginx main screen as follows:
Note that when using the docker run command to create a container, it first checks whether the specified image exists locally. If no image of that name exists locally, Docker will automatically download the image from Docker Hub and start a Docker container.
- List the container
Use the docker ps command to list running containers
docker ps
To list all containers, including those that have been stopped, use the -a parameter. The list contains seven columns with the following meanings
– CONTAINER_ID: indicates the ID of a container.
– IMAGE: indicates the IMAGE name.
– COMMAND: indicates the COMMAND that is run when the container is started.
– CREATED: indicates the creation time of the container.
– STATUS: indicates the running STATUS of the container. UP indicates that the system is running. Exited indicates that it has stopped.
– PORTS: indicates the port number of the container.
– NAMES: indicates the container name. The name is automatically generated by Docker by default, or can be specified using the –name option of the Docker run command.
- Stop the container
Use the docker stop command to stop the container (the container service is suspended)
docker stop f0b1c8ab3633
F0b1c8ab3633 is the container ID. You can also use the docker stop container name to stop the specified container
- Force stop container
docker kill f0b1c8ab3633
- Start the stopped container
Use the docker run command to create and start a container. Stopped containers can be started using the docker start command
docker start f0b1c8ab3633
- View all information about the container
docker inspect f0b1c8ab3633
- Viewing container Logs
docker container logs f0b1c8ab3633
- Look at the process in the container
docker top f0b1c8ab3633
- The container and host copy files from each other
- Copy files from container to host:
Docker cp container id: want to copy files in hosting the corresponding path to the container Such as: docker cp 7 aa5dc458f9d: / etc/nginx/nginx. Conf/mydata/nginx
- Copy files from host to container:
Docker cp Host file path to copy Container ID: the corresponding path to copy to the container
- Into the container
Use the docker exec command to enter a running Docker container. If the docker run command runs the container without using the -it argument, use this command to enter the container. Once inside the container, you can execute commands in the Shell of the container
Docker exec -it f0b1C8ab3633 /bin/bash docker exec -it f0b1C8ab3633 /bin/bash
- The vim, ping, and ifconfig commands are installed in the container
apt-get update
Apt-get install vim #
Apt-get install iputills -ping
Apt-get install net-tools # install ifconfig
Run the docker rm command to delete the specified container
docker rm f0b1c8ab3633
This command can only delete the stopped containers. If you want to delete the running containers, use the -f parameter to forcibly delete all containers
docker rm -f $(docker ps -a -q)
OK, Docker is basically done here, and that’s just scratching the surface. I’m going to keep typing docker deeper stuff later. If you found this post helpful, please like it, follow it, and check it out later if you need these commands. OK, thanks. See you next time!
Summarizing Docker (advantages)
- Simplify procedures
Docker allows developers to virtualize their applications and dependencies by packaging them into a portable container and publishing them on any popular Linux machine. Docker has changed the way of virtualization, enabling developers to directly put their achievements into Docker for management. Convenience and speed have been the biggest advantage of Docker. Tasks that used to take days or even weeks can be completed in seconds under the treatment of Docker containers.
- Avoid choice phobia
If you have a phobia of choice, or a senior patient. Docker packs your tangle! Like Docker images; Docker image contains the running environment and configuration, so Docker can simplify the deployment of a variety of application instances. For example, Web applications, background applications, database applications, big data applications such as Hadoop clusters, message queues, and so on can be packaged into a mirror deployment.
- On the one hand, the advent of cloud computing era makes it unnecessary for developers to configure high hardware for the pursuit of results. Docker has changed the mindset that high performance is bound to high price. The combination of Docker and the cloud makes the cloud space more fully utilized. It not only solves the problem of hardware management, but also changes the way of virtualization.
overtones
Learn a little, work a little, and share a little every day. Thanks for reading, liking, and following. See you next time!