Docker three elements container, image, warehouse

Install the configuration

  • The installation

CentOS7, for example

Update the mirror source first

sudo yum update
Copy the code

Install the docker

sudo yum install docker
Copy the code
  • Mirror configuration

Cr.console.aliyun.com/cn-shenzhen… Enter the website, register ali Cloud account, and obtain the address of the mirror service, such as:

https://xxx.mirror.aliyuncs.com
Copy the code

After obtaining the address, configure the docker image

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}
EOF
Copy the code

After the configuration is complete, reload the configuration file and restart the system

sudo systemctl daemon-reload
sudo systemctl restart docker
Copy the code
  • hello-world

Enter the following command to check the docker installation and configuration success

docker run hello-world
Copy the code

The console displays the following message, if successful, hello-world is a mirror

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
Copy the code

The mirror command

  • View all local mirrors
docker images
Copy the code

Some parameter definitions:

The name of the instructions
REPOSITIRT Name of the mirror
IMAGE ID Mirror ID
TAG Mirroring version, latest by default
CREATED Creation time
VIRTUAL SIZE Occupied Disk Size

View all local images, including the middle image layer, such as Tomcat image, also contains JDK image

docker images -a
Copy the code

View the ids of all local mirrors

docker images -q
Copy the code

View the summary description of the local mirror

docker images --digests
Copy the code

View the complete information of the local IMAGE, such as the original IMAGE ID is part of the capture

docker images --no-trunc
Copy the code
  • Look in the mirror repository

In tomcat, for example

docker search tomcat
Copy the code

The complete image information is displayed in the search result

docker search --no-trunc tomcat
Copy the code

Finds images that are at or above the specified favorites value

docker search -s 30 tomcat
Copy the code

When you find it, pull it down

  • Pull the mirror

Download the image from the mirror repository

docker pull tomcat
Copy the code

Download the image of the specified version number

docker pull tomcat:latest
Copy the code
  • Remove the mirror

Ordinary delete

docker rmi tomcat
Copy the code

Forcibly Deleting a Mirror

docker rmi -f tomcat
Copy the code

Example Delete a mirror of a specified version

docker rmi tomcat:latest
Copy the code

Deleting Multiple Mirrors

docker rmi tomcat hello-world
Copy the code

Group delete all

docker rmi ${docker images -qa}
Copy the code

The container

You need a local image to run the container, which is an instance of the image.

  • Run the container

Start the container (if no container is created). The centos is used as an example

docker run centos
Copy the code

Related parameters

  • –name: Specifies the name of the container

  • -d: Background startup

  • -I: Runs the container in interactive mode

  • -t: reallocates a pseudo-terminal to a container and works with -i

  • -p: indicates the uppercase letter P, indicating random port mapping

  • -p: indicates the lowercase letter p, which specifies the post-break mapping. Local port: indicates the container port

For example, run a centos container named centos-container-service in interactive terminal mode

docker run -it --name centos-container-service centos
Copy the code

Note: Some containers that start in the background may shut down immediately because there is no foreground process. You can run the container as a foreground process

  • Container list

Lists all containers currently running

docker ps
Copy the code

Related parameters:

-n: -n + 3, the container of the last three runs

-a: displays the current and historical containers

-l: displays the newly created container

-q: Displays only container numbers

–no-trunc: Displays detailed information

You can also combine operations, for example, delete all containers

docker rm -f ${docker ps -q}
Copy the code
  • Remove the container

Delete a container based on its ID

docker rm containerID
Copy the code

This command can only be used to delete stopped containers. To delete running containers, you need to delete them forcibly.

Related parameters:

-f: Forcible deletion

  • Viewing container Logs
docker logs containerId
Copy the code

Related parameters:

-f: Follows the latest logs

-t: displays the timestamp

–tail: Displays the number of last printed lines

For example, check the latest 100 tomcat logs, which are commonly used in development

docker logs -f --tail=100 tomcat
Copy the code

Note: In some logging frameworks, timestamps have been added, so the -t parameter is not often used

  • File copy

Copy files in and out of containers

Docker cp containerId: container path Host pathCopy the code

Copy/TMP /yum.log from the container to /docker/ TMP on the host

docker cp containerId:/tmp/yum.log  /dokcer/tmp/
Copy the code

You can also copy the files on the host to containers. For example, create a demo. TXT file and copy it to the root directory of containers

touch demo.tet
docker cp demo.text containerId:/
Copy the code
  • Viewing Container Information

View information about processes in a container

docker top containerId
Copy the code

View the details inside the container, describing the container as JSON data

docker inspect containerId
Copy the code
  • Console command

Exec opens a new terminal, executes the command, and returns the result

docker exec -t containerID Commond
Copy the code

For example, open a terminal

docker exec -it containerID /bin/bash
Copy the code

For example, run ls

docker exec -t containerID ls
Copy the code

Result returned:

bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
Copy the code

Attach to the previous terminal

docker attach containerID
Copy the code

The difference between attach and exec: Attach does not start a new process; exec starts a new process and returns results by typing a command

  • Stop, kill & start, and restart

Start the

docker start containerId
Copy the code

restart

docker restart containerId
Copy the code

Forced to stop

docker stop contaninerId
Copy the code

kill

docker kill containerId
Copy the code
  • Out of the container

The following two commands can only be used when inside the container.

Exit The container stops and exits

exit
Copy the code

CTRL + P + Q Does not stop the container, just exits the console

  • Submit a new container

Submit the modified container to generate a new image file

docker commit -m "summary of commit" containerId imageName:version
Copy the code

Related parameters

  • -m: indicates the submitted information
  • – a: author