Details of docker common instructions

Time:2019-12-9

Instruction detailed

Pull image from remote warehouse

//Docker image pull library / Hello World pull from library by default can be omitted
docker image pull hello-world

View image list

docker image ls

Run a docker image to generate a container instance

// operation
docker run hello-world
//Echo will print a piece of text in the shell to prompt
Hello world
  • Check whether the specified image exists locally. If not, download it from the public warehouse
  • Create and start a container with a mirror
  • Allocate a file system and mount a read-write layer outside the read-only image layer
  • Bridge a virtual interface from the bridge interface configured by the host to the container
  • Configure an IP address to the container from the address pool
  • Execute user specified application
  • Container terminated after execution

View container

//List containers that are running on this machine
docker container ls
//List all containers on this machine, including those that are terminated
docker container ls --all

Start an interactive container

docker run -t -i ubuntu:14.04 /bin/bash
  • The docker run command and start the container named Ubuntu: 14.04.
  • -T indicates to specify a pseudo terminal or terminal in the new container
  • -I means to allow us to interact with (stdin) inside the container.
  • Can reduce – t – I to – it easy to remember
  • We also specified a new command in the container, / bin / bash. This will launch the bash shell inside the container, which is an interactive program
  • Note that “tag” is followed by “: latest if no default is specified

So when the container is started, we will get a command prompt:

[email protected]:/#

This means that we have entered the container

Start the daemons and keep the container running in the background

docker run -d ubuntu:14.04
//Note that - D and - it are used at the same time, - it will be invalid, - D needs to return a container ID

Port mapping, mounting data volumes, naming containers

Docker run - D -- name = ant ﹣ web ﹣ pro - V / host absolute path: / container Directory - P docker external port: docker internal port nginx
  • -P: random port mapping, the internal port of the container is randomly mapped to the high port of the host
  • -P: Specifies the port mapping in the format: host port: container port
  • –Name = “nginx LB”: specify a name for the container;
  • –Volume, – V: bind a volume
  • If the directory in the container does not exist, both will be created automatically
  • -V $PWD / Directory: / directory in container $PWD represents current folder
  • When you change the data volume inside the container, it is actually soft connected to the outside, and the data changes directly on the host

View data volume

docker volume ls

Start terminated container

The container can be run by creating a new one through run or restarting the container that has been stopped. However, start can no longer specify the instructions to run when the container starts, because docker can only have one foreground process.
When the container stops (or Ctrl + D), it exits after saving the state of the current container. The next start keeps the changes made when it was last closed. And every time you enter the attach interface, it’s the same as the first run or commit.

docker start $CONTAINER_ID
docker stop $CONTAINER_ID
docker restart $CONTAINER_ID

Exit container

exit

Container entry

docker exec -it ant-design-pro_web /bin/bash
//Docker exec - it 64b9ded82141 bash (can be ID)

Delete one or more containers, images (RM, RMI)

//Delete container
docker rm <container_id/contaner_name>

//Delete all stopped containers
docker rm $(docker ps -a -q)

//Delete image
docker rmi <image_id/image_name ...>

Docker viewing data scroll

docker volume ls

Commit container, creating a new image

We hope that we can customize our own image, install some basic environments (such as the nodes above), and then make our own basic image.

 docker commit -a "runoob.com" -m "my apache" a404c6c174a2  mymysql:v1 
  • -A: image author submitted;
  • -C: use the dockerfile command to create an image;
  • -M: explanatory text at the time of submission;
  • -P: pause the container during commit.

Export and import of docker image

Docker save - O < save path > < image name: label >
docker save -o ./ubuntu18.tar ubuntu:18.04 
docker load --input ./ubuntu18.tar
Docker export < container name > > save path >
docker export ubuntu18 > ./ubuntu18.tar
Docker import < file path > < container name >
docker import ./ubuntu18.tar ubuntu18
  • The export command exports the tar file from the container, while the Save command exports the images
  • When the exported file is imported back, all the history of the image cannot be preserved (that is, the layer information of each layer can be viewed from the dockerfile if you are not familiar with it), and the rollback operation cannot be performed; while the save is based on the image, so the layer information of the next layer can be completely preserved during import.

Docker installs some software complete instructions

docker run -d --name mysql3 -p 3308:3306 -v /usr/local/docker/mysql/data2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
  • -E set environment variable, MySQL “root” password login password
  • -V / usr / local / docker / MySQL / config / MySQL D. CNF: / etc / MySQL / mysql.conf.d/mysqld.cnf: mount the / etc / MySQL / mysql.conf.d/mysqld.cnf configuration file in the container to the / usr / local / docker / MySQL / config / mysqld.cnf file of the host computer;
  • -V / usr / local / docker / MySQL / data: / var / lib / MySQL: mount the / var / lib / MySQL data directory in the container to the / usr / local / docker / MySQL / data directory of the host computer;
  • MySQL: 5.7 is your image name