Docker common command collation and introduction

  • 2020-05-17 07:00:07
  • OfStack

What is Docker?

Docker is an open source engine that makes it easy to create a lightweight, portable, self-contained container for any application. Containers that developers compile and test on their laptops can be deployed in bulk in production environments, including VMs (virtual machine), bare metal, OpenStack clusters, and other basic application platforms.
Docker is commonly used in the following scenarios:

Automated packaging and distribution of web applications;

Automated testing and continuous integration, release;

Deploy and tune databases or other backend applications in a service-oriented environment.

Build your own PaaS environment by compiling or extending the existing OpenShift or Cloud Foundry platforms from scratch.

Here are some common commands

Docker common commands

Container operation

Start the container

Start the container and start bash (interactive mode) :

$docker run -i -t < image_name/continar_id > /bin/bash

Start the container to run in back-desk mode (more general) :

$docker run -d -it image_name

ps: image_name contains tag here: hello. demo. kdemo: v1. 0

Attach to container

Attach to the running container

docker attach < id, container_name >

Go inside the running container and run bash at the same time (better than attach)

docker exec -t -i < id/container_name > /bin/bash

ps: docker exec is so useful that we usually package it as a script and place it in a globally callable location, for example, as indocker.sh:


$cat indocker.sh 
docker exec -t -i $1 /bin/bash
#  Look at the containers that need to be attached id
$docker ps | less -S
CONTAINER ID    IMAGE                         
9cf7b563f689    hello.demo.kdemo:v160525.202747

$./indocker.sh 9cf7b563f689 

View container logs

View container logs

docker logs < id/container_name >

View log output in real time

docker logs -f < id/container_name > (similar to tail-f) (with timestamp -t)

Check the container

List all currently running container

$docker ps

List all running container in 1 row (very clear when there are many containers)

$docker ps | less -S

List all container

$docker ps -a

List the most recent container startup

$docker ps -l

Displays process information in a running container

$docker top Name/ID

See details inside the container:

$docker inspect < id/container_name >

Install the new program in the container

$docker run image_name apt-get install -y app_name

Note: when executing the apt-get command, take the -y parameter with you. If you do not specify the -y parameter, the apt-get command will enter the interaction mode, requiring the user to enter the command to confirm, but this interaction will not be responsive in the docker environment. After the apt-get command is executed, the container is stopped, but changes to the container are not lost.

Copy file/directory from container to local 1 path

$docker cp Name:/container_path to_path
$docker cp ID:/container_path to_path

Save changes to the container (commit) when you make a change to a container (by running a command in the container), you can save the changes to the container so that you can run the container from the latest saved state the next time.

$docker commit ID new_image_name

Note: image is equivalent to a class, container is equivalent to an instance, but you can dynamically install new software on the instance, and then solidify this container into an image using the commit command.

Delete a single container

$docker rm Name/ID

- f � force = false; -- l, link=false Remove the link and the underlying container; -- v, volumes=false Remove the volumes associated to the container

Delete all containers

$docker rm `docker ps -a -q`

Stop, start, kill, and restart 1 container


$docker stop Name/ID 
$docker start Name/ID 
$docker kill Name/ID 
$docker restart name/ID

Operating Image

List the mirror

$sudo docker images

-a, all=false Show all images; � no - trunc = false Don 't truncate output; -q, quiet=false Only show numeric IDs

Retrieve image from dockerhub

$docker search image_name

Download image

$docker pull image_name

Delete one or more images;

$docker rmi image_name
- f � force = false Force; -- no-prune =false Do not delete untagged parents

Displays the history of a mirror;

$docker history image_name

Publish the docker image

$docker push new_image_name

ps: to publish an image in private Registry, you need to include the domain name of Registry in the name of the image (if not port 80, you also need to include the port number), for example:

$docker push dockerhub.yourdomain.com:443/hello.demo.kdemo:v1.0

Pull the docker image

$docker pull image_name

Network operating

View docker0's network (host operation)

$ip a show docker0

View the IP address of the container

$docker inspect -f '{{ .NetworkSettings.IPAddress }}' < id, container_name >

Attach to the inside of the container to see its inside ip:

$ip a show eth0

See docker basics

View the docker version

$docker version

View information about the docker system

$docker info

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: