Technical work, should be praised and then see, form a habitCopy the code

Docker use tutorial related series of directories


What is docker image

Docker image introduction

The operating system is divided into kernel and user space. For Linux, the root file system is mounted to provide user space support after the kernel is started. A Docker Image, on the other hand, is a root file system. Docker image is a special file system, in addition to providing programs, libraries, resources, configuration files required by the container runtime, but also contains some configuration parameters prepared for the runtime (such as anonymous volumes, environment variables, users, etc.). The image does not contain any dynamic data and its contents are not changed after the build.

Docker images are stored in /var/lib/docker-/ by default

The relationship between images and containers

The relationship between an image and a container is similar to that between a class and an instance in object-oriented programming. An image is a static definition and a container is an entity of the image runtime. Containers can be created, started, stopped, deleted, paused, and so on. The essence of a container is a process, but unlike processes that execute directly on the host, container processes run in their own separate namespace. So a container can have its own root file system, its own network configuration, its own process space, and even its own user ID space. The processes in the container run in an isolated environment and are used as if they were operating on a separate host system. This feature makes container-wrapped applications more secure than running directly on the host. As mentioned earlier, images are layered, and so are containers. Each container runtime is based on the image layer, on which a storage layer of the current container is created. The storage layer prepared by the container runtime can be called the container storage layer. The container storage layer lives the same as the container. When the container dies, the container storage layer dies with it. Therefore, any information stored in the container storage layer is lost when the container is deleted. As per Docker best practices, containers should not write any data to their storage layer, and the container storage layer should remain stateless. All file writing operations should use data volumes or bind host directories. Read and write operations in these locations skip the storage layer and directly write to the host (or network storage), achieving higher performance and stability. The lifetime of a data volume is independent of the container. The container dies and the data volume does not die. Therefore, after using data volumes, the container is deleted or re-run without data loss.


List docker images

Start the docker

systemctl restart docker.service
Copy the code

List docker images

docker images
Copy the code

 

Three, pull mirror image

Pull from the Docker hub

The following problems often occur when pulling an image from a Docker Hub:

Solution:

Docker pull image from docker hub, because it is obtained from abroad, the speed is slow. You can configure a domestic mirror source to obtain domestic mirrors, improving the pull speed.

Ustc Image Source (recommended)

configuration

cd /etc/docker
Copy the code

Vi daemon. Json copy save {" registry - mirrors: "/" https://docker.mirrors.ustc.edu.cn "}Copy the code

Restart the Docker service

systemctl restart docker.service
Copy the code

Pull the centos image

docker pull centos
Copy the code

After the pull is successful, view it

docker images
Copy the code

How to use a third-party image

Hub.docker.com/search?type…

There are instructions in the documentation on how to use it

Find the mirror

docker search tomcat
Copy the code

NAME: the mirror

DESCRIPTION: Indicates the DESCRIPTION of the mirror

STARS: User ratings. The higher the value, the more popular it is

OFFICIAL: Whether it’s OFFICIAL

AUTOMATED: Automatic construction, indicating that the image is created by the Docker Hub automatic construction process;

Build the mirror

Install tomcat

1. Install centos first

List docker images

docker images
Copy the code

If you already have it, you do not need to install it. If you do not have it, install centos

docker pull centos
Copy the code

2. Create interactive containers

docker run -i -t --name=mycentos centos
Copy the code

Note:

What do these two parameters (-i, -t) mean

Options Mean
-i Run the container in interactive mode, usually with -t;
-t Reassign a pseudo-input terminal to the container, usually used in conjunction with -i;

Into the container

Install the JDK

Yum install Java - 1.8.0 comes with - its * - yCopy the code

Successful installation

Setting environment Variables

Centos8 after installing Java using yum, view the Java installation path

ls -lrt /usr/bin/java 
ls -lrt /etc/alternatives/java
Copy the code

vi /etc/profile
Copy the code

Paste the following content at the bottom of the file and save

Note: after JAVA_HOME, is your actual installation path!

export JAVA_HOME=/usr/local/jdk-xxxx
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
Copy the code

Run the source /etc/profile command to test the Java -version commandCopy the code

Copy Tomcat to the server

Can be uploaded to the server via SCP

Go back to the Docker server and copy Tomcat into the container

Docker cp apache tomcat - 8.5.47.zip mycentos: / root /Copy the code

Into the container

docker attach mycentos
Copy the code

Exit container:

exit
Copy the code

Looking for a container

docker ps -a
Copy the code

Error:

You cannot attach to a stopped container, start it first
Copy the code

Solution: Start the container

Look at Tomcat in the container

Unpack the zip

First installed unzip

Move to the

Mv apache tomcat - 8.5.47 / usr/local/tomcat8Copy the code

The container is submitted as a new image

Let’s exit the container

exit
Copy the code

The container is submitted as a new image

docker commit mycentos mytomcat8
Copy the code

Start the Tomcat container with the new image

-p is a port mapping

docker run -d -i -t --name=tomcat -p 8888:8080 mytomcat8 /bin/bash
Copy the code

 

Start the Tomcat container

 

Start tomcat

Note: The port accessed is the docker mapped port, not the port of Tomcat itself

That’s it, over!