Record the daily use commands of Docker. This article is mainly for Linux + MAC operating system. It is not sure whether Windows is applicable, so use with caution

1. The docker process

Docker process starts, stops, and restarts

# start docker
service docker start
# shutdown docker
service docker stop
# restart docker
service docker restart
Copy the code

2. Mirror operation

Image as a prerequisite for container execution, the general need to master several commands are nothing more than search, download, delete, create

# mirror list
docker images
Retrieve the image from the mirror repository
docker search xxx
# Download image
docker pull xxx
# delete mirror
docker rmi xxx
Copy the code

It is necessary to be a little more detailed about creating a mirror

Create an image from a container
docker commit -m="First submission" -a="A Gray Blog"Dd85eb055fe8 yh/centos: v0.1# Mirror history query
docker history yh/centos
Copy the code

The above parameters are described

  • -mAs with Git commits, follow with a description
  • -aCopyright notice, I created this thing, if you have any questions, please contact me
  • dd85eb055fe8The container id
  • Yhh/quick - OS: 0.1Name of the image to be created

3. Container operations

Then comes the main course, the various operations of the container, startup, shutdown, restart, log query, and various internal operations of the container

a. run

The first step is to load the image and create the container

Docker Run Image name: versionCopy the code

Run can be followed by a number of parameters, such as container exposure port designation, storage mapping, permissions, etc

Case1: created and executed in the background

docker run -i -t -d centos:latest
Copy the code
  • The key parameters are-d, specifies whether the container runs with the foreground or background, without adding the foreground
  • -i: Opens STDIN for console interaction
  • -t: Supports terminal login

Case2: Runs a container with commands that are executed continuously in the background

docker run -d centos:latest ping www.baidu.com
Copy the code

Case3: Runs a container that executes continuously in the background, with commands that restart the program after it has been terminated

docker run -d --restart=always centos:latest ping www.baidu.com
Copy the code

Case4: Specifies the container name

docker run -d --name=yhh_centos centos:latest
Copy the code

Case5: Exposes container port 80 and binds to host port 8080

docker run -d --name=yhh_centos -p 8080:80 centos:latest
Copy the code

Case6: Specifies that the container is shared with the host directory (/home/yihui/ HTML/WWW)

docker run -d --name=yhh_centos -v /home/yihui/html/www:/var/www centos:latest
Copy the code

B. the base

Once the container is created, the basic operations are started, stopped, restarted, and deleted

View the container list and list all containers
docker ps -a 
The container name or id can be used to start the container
docker start xxx  Yhh_centos = f57398AB22C5
# close the container
docker stop xxx
# to restart
docker restart xxx
# remove
docker rm xxx
Copy the code

When viewing the container list, if the start parameter of a container is very long, docker ps -a will find that the complete start command cannot be seen. In this case, you can display the complete command with the parameter –no-trunc

docker ps -a --no-trunc
Copy the code

C. in order

Let’s move on to some advanced container manipulation techniques (which are actually not that cool)

To demonstrate some more advanced content, create a container as a test

docker run -it -d --name=yhhos centos
Copy the code

Container Log Query

Log, the magic tool for locating problems

# query the logs of the XXX container
docker logs yhhos
Copy the code

It’s almost impossible to use the command directly, because it prints out all the logs, which can directly blind our titanium Eye

General log can add two parameters -f, -t

docker logs -f -t --since="2019-05-11" --tail=10 yhhos
Copy the code
  • --since: This parameter specifies the start date of the log output, that is, only the log output after the specified date.
  • -f: Displays real-time logs
  • -t: Displays the date when a log is generated
  • --tail=10: View the last 10 logs.

File copy

Fish a file out of the container; Or force plug, one CP

Copy the test.md file from the current directory to/TMP
docker cp test.md yhhos:/tmp

# copy/TMP /test.md to the current directory
docker cp yhhos:/tmp/test.md ./out.md
Copy the code

Into the container

Get inside the container and do whatever you want…

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

Get all information about the container

docker inspect yhhos
Copy the code

II. The other

1. A gray Blog:liuyueyi.github.io/hexblog

A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll

2. Statement

As far as the letter is not as good as, has been the content, purely one’s own words, because of the limited personal ability, it is hard to avoid omissions and mistakes, such as finding bugs or better suggestions, welcome criticism and correction, not grudging gratitude

  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840

3. Scan attention

A gray blog