Docker common commands (transfer)

Author: http://www.cnblogs.com/me115/p/5539047.html

 

Operation container

start the container

  • Start the container and start bash (interactively):
$docker run -i -t <image_name/continar_id>/bin/bash
  • Start the container to run in the background (the more general way):
$docker run -d -it  image_name

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

attached to the container

  • Attach to a running container
docker attach <idcontainer_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 encapsulate it as a script and put it in a globally callable place, for example, it can be written as an indocker.sh:

$cat indocker.sh 
docker exec - t - i $1 / bin / bash
 # View the container id that needs to be attached 
$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 )

View containers

  • List all currently running containers
$docker ps
  • List all running containers in one line (very clear when there are many containers)
$docker ps | less -S
  • list all containers
$docker ps -a  
  • List the most recently started container
$docker ps -l
  • Display process information in a running container
$docker top Name/ID  
  • Check out the details of the inside of the container:
$docker inspect <id/container_name>
  • Install new programs in containers
$docker run image_name apt-get install -y app_name  

Note: When executing the apt-get command, bring the -y parameter. If the -y parameter is not specified, the apt-get command will enter the interactive mode, requiring the user to enter a command for confirmation, but this interaction cannot be responded to in the docker environment. After the apt-get command is executed, the container is stopped, but changes made to the container are not lost.

  • Copy files/directories from the container to a local path
$docker cp Name:/container_path to_path  
$docker cp ID:/container_path to_path
  • Save changes to the container (commit) When you make changes to a container (by running a command in the container), you can save the changes to the container, so that you can run it from the latest saved state next time. container.
$docker commit ID new_image_name  

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

  • delete a single container
$docker rm Name/ID 

-f, –force=false; -l, –link=false Remove the specified link and not 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, restart a container
$docker stop Name/ID  
$docker start Name/ID  
$docker kill Name/ID  
$docker restart name/ID

Manipulate Image

  • list mirrors
$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
  • downloadimage
$docker pull image_name
  • delete one or more mirrors;
$docker rmi image_name  

-f, –force=false Force; –no-prune=false Do not delete untagged parents

  • show the history of a mirror;
$docker history image_name
  • Publish docker image
$docker push new_image_name

ps: To publish the image in the private Registry, the domain name of the Registry needs to be included in the image name (if it is not port 80, the port number needs to be included). For example:

$docker push dockerhub.yourdomain.com:443/hello.demo.kdemo:v1.0
  • Pull the docker image
$docker pull image_name

network operation

  • View the network of docker0 (operation on the host)
$ip a show docker0
  • View the IP address of the container
$docker inspect -f '{{ .NetworkSettings.IPAddress }}'<idcontainer_name>

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

$ip a show eth0

View docker basic information

View docker version

$docker version

View the information of the docker system

$docker info

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327014513&siteId=291194637