preface

This paper mainly records some common Docker instructions to facilitate query and improve work efficiency.

Mirror article

To create a container, the home page must have an image, so this article starts with an image.

Mirror the drop-down

There are many ways to get an image, from dockerHub drop-down (download), from your own image source, or build your own.

Image pull down using the docker pull command as follows:

$Docker pull ubuntu: 16.04
Copy the code

Viewing the Mirror List

After the mirror is successfully pulled down, you can run the following command to view the image:

$ docker image ls 

$ docker image ls | grep ubuntuUbuntu 16.04 96DA9143Fb18 1 months ago 124MBCopy the code

Remove the mirror

$Docker Image RM Image name/ID of an image
Copy the code

Image upload

Take the above uploading to Dockerhub as an example. The detailed steps are as follows:

1. Create an account in Dockerhub. Assume the username is yangan

In 2.DockerhubCreate a repository (such as Ubuntu) for uploading images

3. The terminal runs docker login –username= your username to login

$ docker login --username=yangan
Password:
Login Succeeded
Copy the code

4. Label the image (match it with the warehouse name)

#usage
Usage:	docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

$Docker tag ubuntu: 16.04 yangan/ubuntu_test: v1
Copy the code

5. Run push to upload the file

#Username indicates the username and tagname indicates the tagname 
$ docker push username/ubuntu_test:tagname

$ docker push yangan/ubuntu_test:v1
Copy the code

Image Import and Export

1. Export the image$ docker save <image_name>:<tag> | gzip > <image_name>_<tag>.tar.gz2. Import an image$ gunzip -c <image_name>_<tag>.tar.gz | docker load2. You can also decompress the tar file and load it$ docker load < <image_name>_<tag>.tar
Copy the code

Container article

Now that we have our image from the above steps, we can create the container.

Container to create

Container creation uses the Docker run subcommand, which can pass various parameters to construct containers with different functions.

Usage:

docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

Common parameters:

-d: specifies whether the container is running in the background. If this parameter is not specified, the container will stop automatically after exiting. -p: specifies the port to be mapped. -e: specify the environment variable parameter --rm: automatically delete the container after the container is performed. -- Privileged: grant the container privileged. If this parameter is specified, the root user in the container has the real root permission. Otherwise, the root user inside the container is just a normal user permission outside.Copy the code

Common demo:

#1. The default terminal is bash
$Docker run - it ubuntu: 16.04
root@efe0291ff0c3:/# echo $SHELL
/bin/bash

#2. Specify the terminal
$Docker run -it Ubuntu :16.04 /bin/sh
# echo $0
/bin/sh

#3. Specify the user name
$Docker run-it --name ubuntu123 Ubuntu :16.04
root@b650d1846d40:/#

#4. Specify other parameters
$Docker run -d --name ubuntu234 -p 12007:12007 -p 9001:9001 -v /data:/data -e PYTHONIOENCODING= UTF-8 Ubuntu :16.04
Copy the code

View a list of containers

#View the running container
$ docker ps
$ docker container ls

#View all containers
$ docker ps -a  
$ docker container ls --all 
Copy the code

Remove the container

$Docker Container Rm Indicates the container name/ID  

#Delete inactive containers and inactive images
$ docker system prune 
Copy the code

Into the container

#1. Enter the container and useexitExiting the container does not kill the container 
$ docker exec -it container_name /bin/bash   

#2. Enter the container and useexitExiting the container kills the container  
$ docker attach container_name  
Copy the code

The container packing

In addition to the Dockerfile mentioned above, you can also package the image directly on the basis of the container, using the command docker commit.

$ docker commit ubuntu_test ubuntu_test:v1
Copy the code

As shown above, this command means to package the Ubuntu_test container into an image ubuntu_test:v1, where v1 is the label and is generally used to specify the version. The packaging process does not affect the original container.

Container file copy

#1. Copy the container files to the host (wp2md is the container name)
$docker cp wp2md:/exitwp/text /home/xxx/Documents/

#2. Copy the host file to the container
$ docker cp /home/xxx/Documents/test.md wp2md:/exitwp/text 
Copy the code

Viewing Container Logs

$Docker logs -f -t --tail Number of rows container name
Copy the code

Overall information

docker info

This command is used to view basic information about the local Docker, such as the version, number of containers, number of mirrors, and source of mirrors.

$ docker info
Client:
 Debug Mode: false

Server:
 Containers: 18
  Running: 8
  Paused: 0
  Stopped: 10
 Images: 30
 ...
Copy the code

docker stats

This command is used to view the resource usage of a container, such as CPU usage and memory usage. By default, this command will refresh every second, that is, output until you interrupt.

Dea2e5f26346 linux-test 0.00% 23.41MiB / 1.952GiB 1.17% 14.3MB / 328kB 116MB / 256MB 2 ad1ad315d73f mysql-test 0.15% 175.6MiB / 1.952GiB 8.79% 10.7kB / 0B 30.6MB / 4.1kB 27...Copy the code

If you want to output only the current state, add the –no-stream argument

docker stats –no-stream

To view information about a specified container, add Registry

Docker stats –no-stream Registry container ID

docker system

This command is mainly used to manage Docker. There are four subcommands. I usually use Docker system df to check disk usage:

Examples are as follows:

$ docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              18                  9                   6.993GB             3.749GB (53%)
Containers          12                  5                   4.051GB             584MB (14%)
Local Volumes       14                  4                   2.965GB             938MB (31%)
Build Cache         0                   0                   0B                  0B
Copy the code

Write in the last

Dear bosses, creation is not easy, but it needs to be honed and summarized constantly, welcome to pay attention to me, I am Yan Gan, I will share with you all kinds of practical programming knowledge and skills, your praise and attention is the biggest motivation for my progress and creation!!