Docker

1. A Docker Image is a read-only template. Images can be used to create Docker containers, and one image can create many containers
A container is an object, and a mirror is a class

Student s1 = new Student() Student s2=new Student() Student s3=new Student()

Student is a template, a mirror image.

The warehouse is where the image is stored
Official summary:

Dokcer uses a Container to run one or a group of applications independently. Containers are running instances created with images.

It can be started, started, stopped, and deleted. Each container is an isolated, secure platform.

Think of the container as a simplified version of the Linux environment.

Why Docker

1. Lighter weight: Container-based virtualization, which only contains the runtime environment required for service running. The CentOS/Ubuntu basic image is only 170M. Hosts can deploy 100-1000 containers
2. More efficient: No OPERATING system virtualization overhead
3. Be more agile and flexible
The container is a simplified version of the Linux virtual machine environment, stripped of some hardware.

You can’t create a container without an image, that’s the basic premise

1. Create and start a container:
docker run [options] image[command][arg...]
Copy the code
Some have one minus sign, some have two minus signs

– name= “New container name” Specifies a name for the container

-d: Runs the container in the background and returns the container Id, that is, starts the daemon container

-I: Runs the container in interactive mode, usually in conjunction with -t

-t: reassigns a pseudo-input terminal to the container. It is usually used together with -i.

-p: indicates random port mapping

-p: specifies the port mapping. The value can be in the following formats

​ ip:hostPort:containerPort

​ ip::containerPort

​ hostPort:containerPort

​ containerPort

Docker runit Imageid interactively starts the Dockers image to generate container instances. Docker PS lists all running containers to see which containers are loadedCopy the code

docker ps [options]

Options Description Common:

-a: Lists all currently running containers + historically run containers

-l: displays the newly created container

-n: Displays the n containers created recently

Docker Ps-N 3 Last 3

-q: displays only container ids in silent mode

– no-trunc: does not truncate the output

Exit the container in two ways

1. Exit The container stops exiting

2. CTRL +P+Q The container exits without stopping

Enter the running container and interact with it on the command line

Docker execit container id ls -l/TMP can be viewed from the outside, while attach can be attached to the inside

Docker exec -it Container ID bashShell
Re-enter the Docker Attach container ID
Attach directly enters the terminal of the container start command and does not start a new process
Exec opens a new terminal in the container and can start a new process

Start the container

Docker start Container instance ID or name

docker restart

Stop container Docker stop force stop docker kill

Delete stopped containers

Docker RM Container Id deleted closed

Docker rm f Container ID Forcibly deleted

Docker RMI delete image

Delete multiple containers at once

docker rm -f $(docker ps -a -q)

docker ps -a -q|xargs docker rm

Start a container in background mode

Start the daemon container

docker run -d centos
Problem: Then docker ps-a to check, will find that the container has quit, it is important to clarify a point: Docker container background operation, there must be a foreground process. Commands run by the container that are not always suspended (such as running top,tail) will exit automatically.
The dockers mechanism problem, such as your Web container, let’s take nginx as an example. Normally, we configure to start the service just by starting the responding service. For example, service nginx start. However, if nginx runs in daemon mode, there will be no applications running in the Docker foreground. Such a container will commit suicide immediately after it starts in daemon mode because it feels it has nothing more to do. Run the program as a console process

Viewing container Logs

Docker logs -f -t –tail Container ID

-t is to add the timestamp -f follows the latest log print –tail number shows the last number

Docker logs -f-t –tail 3

Docker Top Container ID of the process running in the container

Docker inspect Container ID

Copy files from container to host docker cp Container ID: path Path

Image: An image is a lightweight, executable, standalone package used to package a software runtime environment and software developed based on the runtime environment. It contains everything needed to run a piece of software, including code, runtime, libraries, environment variables, and configuration files

Docker run it -p 8888:8080 tomcat Starts Tomcat

-p indicates the exposed port

P Host port number: port number of the Docker container

-p is random assignment. I: interaction. T: terminal