Docker common command quick reference manual

Posted by nykoelle on Mon, 09 Dec 2019 06:07:27 +0100

Record the daily use commands of docker. This article is mainly for linux + mac operating system. It is not sure whether window is applicable. Use it carefully

<!-- more -->

1. docker process

Three common case s are docker process start, stop and restart

# Start docker
service docker start
# Shutdown docker
service docker stop
# Restart docker
service docker restart

2. Image operation

As a prerequisite for the execution of containers, the general commands you need to master are nothing more than search, download, delete and create

# Mirror list
docker images
# Retrieve image, retrieving from image warehouse
docker search xxx
# Download mirroring
docker pull xxx
# delete mirror
docker rmi xxx

It's necessary to go into a little more detail about creating images

# Create a mirror from a container
docker commit -m="First time submission" -a="Ash Blog" dd85eb055fe8 yh/centos:v0.1
# Image history query
docker history yh/centos

The above parameters are described

  • -m is the same as git's submission, followed by the description
  • -a copyright notice. I created this thing. If you have any questions, please contact me
  • dd85eb055fe8 container id
  • Image name created by yhh / quick OS: 0.1

3. Container operation

The next step is the main dish, various operations of the container, startup, shutdown, restart, log query and various things to do inside the container

a. run

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

docker run image name: version

run can be followed by many parameters, such as container exposure port specification, storage mapping, permissions, etc. because there are too many parameters, here are just a few different examples to show how to add parameters

Case 1: create and execute in the background

docker run -i -t -d centos:latest
  • The key parameter is - d, which specifies whether the container runs with the foreground or the background, without adding the foreground
  • -i: open STDIN for console interaction
  • -t: support terminal login

Case 2: run a container with commands continuously executing in the background

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

Case 3: run a container that executes continuously in the background with commands. After the program is terminated, it can restart and continue running

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

Case 4: specify container name

docker run -d --name=yhh_centos centos:latest

Case 5: expose container port 80 and bind with host port 8080

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

Case 6: specify the container to share with the host directory (/ home/yihui/html/www)

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

b. basic exercises

After the container is created, there are some basic operations: start, stop, restart and delete

# View the list of containers, listing all containers
docker ps -a 
# start the container. start can be followed by the container name or the container id
docker start xxx  # (the xxx here can be the container name: yhh_centos or the container id: f57398ab22c5)
# Close container
docker stop xxx
# restart
docker restart xxx
# delete
docker rm xxx

When viewing the container list, if the startup parameters of a container are very long, you can directly use docker PS-A to find that you cannot see the complete startup command. At this time, you can bring the parameter -- no TRUNC to display the complete command

docker ps -a --no-trunc

c. advanced

Next, I'll go to some advanced operation skills of containers (actually not particularly cool)

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

docker run -it -d --name=yhhos centos

Container log query

Journal, the artifact of locating problems

# Query the log of xxx container
docker logs yhhos

Basically, I don't know how to use the above command directly, because all the logs are printed on it, which can directly blind our titanium alloy x-eyes

General log can be added with two parameters - f, -t

docker logs -f -t --since="2019-05-11" --tail=10 yhhos
  • --since: this parameter specifies the start date of the output log, that is, only the logs after the specified date are output.
  • -f: view real-time logs
  • -t: view the date of log generation
  • --tail=10: view the last 10 logs.

File copy

Pull out a file of the container; or force the plug, one cp is enough

# Copy the test.md file of the current directory to the / tmp directory of the container
docker cp test.md yhhos:/tmp

# Copy the / tmp/test.md directory of the container to the current directory
docker cp yhhos:/tmp/test.md ./out.md

Container entry

Go inside the container and do whatever you want

docker exec -it yhhos /bin/bash

Get all container information

docker inspect yhhos

II. other

1. A grey Blog: https://liuyueyi.github.io/hexblog

A grey personal blog, recording all the learning and working blog, welcome to visit

2. statement

A letter to the best of one's ability is not as good as a letter to the best of one's ability. There are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct. Thank you very much

3. Scanning attention

A grey blog

Topics: Programming Docker CentOS Linux Mac