Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common commands in docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what are the common commands in docker", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what are the common commands in docker?"

According to my own understanding, generally speaking, it can be divided into the following categories:

Container Life cycle Management-docker [run | start | stop | restart | kill | rm | pause | unpause]

Container operation and maintenance-docker [ps | inspect | top | attach | events | logs | wait | export | port]

Container rootfs command-docker [commit | cp | diff]

Image repository-docker [login | pull | push | search]

Local image management-docker [images | rmi | tag | build | history | save | import]

Other commands-docker [info | version]

Look at a transition map.

1. List the images on the machine (images) # docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 14.10 2185fd50e2ca 13 days ago 236.9 MB...

We can determine which server the image is from according to REPOSITORY. If there is no /, it means the official image. Similar to username/repos_name, it represents the personal public library of Github. Similar to regsistory.example.com:5000/repos_name, it means private server.

The IMAGE ID column is actually an acronym. If you want to display it in its entirety, take the-- no-trunc option.

two。 Search for image (search) in docker index

Usage: docker search TERM

# docker search seanloNAME DESCRIPTION STARS OFFICIAL AUTOMATEDseanloook/centos6 sean's docker repos 0

The scope of the search is official images and all personal public images. The / of the NAME column is followed by the name of the warehouse.

3. Drop image or repository (pull) from docker registry server

Usage: docker pull [OPTIONS] NAME [: TAG]

# docker pull centos

The above command should be noted that before docker v1.2, all images in the centos repository of official images will be downloaded, but the instructions in the official documents have changed since v.13: will pull the centos:latest image, its intermediate layers and any aliases of the same id, that is, only images whose tag is latest (and other tag of the same images id).

You can also specify a specific image:

# docker pull centos:centos6

Of course, you can also pull it from someone's public warehouse (including your own private warehouse), such as docker pull username/repository:

# docker pull seanlook/centos:centos6

If you do not have a network, or obtain an image from another private server, such as docker pull registry.domain.com:5000/repos:

# docker pull dl.dockerpool.com:5000/mongo:latest4. Push an image or repository to registry (push)

Corresponding to the pull above, it can be pushed to Docker Hub's Public, Private and private server, but not to Top Level Repository.

# docker push seanlook/mongo# docker push registry.tp-link.net:5000/mongo:2014-10-27

Registry.tp-link.NET can also be written as IP,172.29.88.222.

In the absence of repository, the push under the command line will be created as a private library for us, while the one created through the browser will be a public library by default.

5. Launch a container (run) from image

The docker run command first creates a layer of writable Container from a specific image, and then launches it with the start command. The stopped container can be restarted and the original changes retained. There are many startup parameters for run command. Here are some general instructions. For more information, please refer to http://www.cnphp6.com/archives/24899.

When using docker run to create containers, standard operations for Docker to run in the background include:

Check whether the specified image exists locally. Download it from the public repository if it does not exist.

Create and start a container using an image

Assign a file system and mount a read-write layer outside the read-only mirror layer

Bridge a virtual interface to the container from the bridge interface configured by the host host

Configure an ip address from the address pool to the container

Execute a user-specified application

The container is terminated after execution

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

5.1 use image to create container and execute the appropriate command, and then stop # docker run ubuntu echo "hello world" hello word

This is the easiest way, and it's almost indistinguishable from executing echo 'hello world' directly locally, when in fact it starts from a local ubuntu:latest image to a container and exits after executing the print command (docker ps-l can see it). It is important to note that there is a default-- rm=true parameter, which stops the container and removes it from the file system when the operation is complete. Because Docker containers are so lightweight, users often delete and create new containers at any time.

After the container starts, a CONTAINER ID is automatically generated randomly, and this ID can be changed to IMAGE ID after the following commit command.

Use image to create a container and enter interactive mode, login shell is / bin/bash# docker run-I-t-- name mytest centos:centos6 / bin/bashbash-4.1#

The-- name parameter above can specify the name of the container after startup. If not, docker will give us a name. Mirror centos:centos6 can also be replaced by IMAGE ID (68edf809afe7), and a pseudo terminal will be started, but we can only see one or two processes through the ps or top command, because the core of the container is the executed application, and all the resources needed are necessary for the application to run. In addition, there are no other resources, which shows that Docker has a very high utilization of resources. After exiting with exit or Ctrl+D, the container disappears (the missing container is not completely deleted? )

(so which TAG will multiple images with different TAG but the same IMAGE ID run?

Run a container and put it in the background to run # docker run-d ubuntu / bin/sh-c "while true; do echo hello world; sleep 2; done" ae60c4b642058fefcc61ada85a610914bed9f5df0e2aa147100eab85cea785dc

It will directly suspend the launched container to run in the background (this is called saas), and will output a CONTAINER ID. You can see the information of this container through docker ps, you can check its output docker logs ae60c4b64205 outside the container, or you can connect to the running terminal through docker attach ae60c4b64205. At this time, the container disappears when you exit the Ctrl+C, press ctrl-p ctrl-q to exit to the host, and keep the container still running.

In addition, if-d starts but the subsequent command execution ends, such as / bin/bash, echo test, the container still terminates when it is done. And-d cannot be used with-- rm

You can run memcached, apache, and so on in this way.

Mapping host to container ports and directories

It is useful to map the port from the host to the container. For example, if you run memcached in container with port 11211, the host running the container can be accessed by connecting to the internel_ip:11211 of container. If you need to access memcached from other hosts, you can use the-p option, such as-p, to write it in the following ways:

By default, port 11211 that binds all network cards of the host (0.0.0.0) to port 11211 of the container-p 127.0.0.1 11211 only binds port 11211 of this interface-p 127.0.0.1 localhost 5000LV p 127.0.0.1

Directory mapping is actually the path of "binding and mounting" host to the directory of container, which is more convenient for internal and external transfer of files. In the section of building private servers, in order to prevent the saved images from being deleted after the private server container stops, the submitted images should be saved to the mounted host directory. It is relatively easy to use-v, and add-v when binding multiple directories.

-v / tmp/docker:/tmp/docker

Another link between the two container is available-link, as detailed in the advanced section or official documentation.

Here is an example:

# docker run-- name nginx_test\ >-v / tmp/docker:/usr/share/nginx/html:ro\ >-p 80:80-d\ > nginx:1.7.6

Set up index.html under / tmp/docker of the host, and you can access it through http://localhost:80/ or http://host-ip:80.

6. Solidify a container into a new image (commit)

When we are making our own images, we will install some tools and modify the configuration in container. If we do not do commit to save, then container stops and then starts, and these changes disappear.

Docker commit [repo:tag]

The later repo:tag is optional

You can only submit a running container, that is, a container that can be seen through docker ps.

Check the container that just ran # docker ps-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc9fdf26326c9 nginx:1 nginx-g.. 3 hours ago Exited (0).. Nginx_test starts an existing container (run starts after creating a new container from image). You can also use docker start nginx_test instead of [root@hostname docker] # docker start c9fdf26326c9c9fdf26326c9docker run-I-t-- sig-proxy=false 21ffe545748baf / bin/bashnginx service does not start # docker commit-m "some tools installed" >

-a "seanlook7@gmail.com"

Please note that when you repeatedly commit a container, you will get a new IMAGE ID each time. If the following repository:tag does not change, you can see through docker images that the repository:tag of the previously submitted image will become:, so try to avoid repeated submissions.

In addition, observe the following points:

Commit container only pause containers, this is to ensure the consistency of the container file system, but not stop. If you want to make other changes to this container:

You can resubmit to get a new image2 and delete the next new image1

You can also close the container and start with a new image1, continue to modify, and delete the image1 after submitting the image2.

Of course, this will be very painful, so it is common to use Dockerfile to build to get the final image, see []

Although a new image is generated, and you can see that it is 100MB in size, you can quickly know from the commit process that it doesn't actually take up 100MB's hard disk space independently, but just modifies it based on the old image, and they share most of the common "slices".

Lower

1. Turn on / stop / restart container (start/stop/restart)

The container can be run by creating a new one through run, or you can re-start the stopped container, but start can no longer specify the instructions to run when the container starts, because docker can only have one foreground process.

When a container stop (or Ctrl+D), it exits after saving the state of the current container, and the next time it is start, it will be changed the last time it was closed. And every time you enter attach, the interface is the same, the same as the first time run starts or commit submits.

CONTAINER_ID=$ (docker start) docker stop $CONTAINER_IDdocker restart $CONTAINER_ID

These commands can be used through a complete example of how docker creates a container that runs background processes and provides shell terminals at the same time.

two。 Connect to a running container (attach)

The container you want to attach on must be running, and you can connect the same container at the same time to share the screen (similar to the attach of the screen command).

The official document says that you can detach through CTRL-C after attach, but in fact, after my test, if container is currently running bash,CTRL-C is naturally the input of the current line, without exiting; if container is currently running a process in the foreground, such as outputting nginx's access.log log, CTRL-C will not only cause you to exit the container, but also stop it. This is not what we want, detach should mean to leave the container terminal, but the container is still running. Fortunately, attach can carry-sig-proxy=false to ensure that CTRL-D or CTRL-C does not close the container.

# docker attach-- sig-proxy=false $CONTAINER_ID3. View the underlying information of image or container (inspect)

The objects of inspect can be image, running container, and stopped container.

Look at the container's internal IP# docker inspect-- format=' {{.NetworkSettings.IPAddress}}'$CONTAINER_ID172.17.42.354. Delete one or more container, image (rm, rmi)

You may build or commit a lot of images during use, and useless images need to be deleted. However, there are some conditions for deleting these images:

There may be multiple TAG in the same IMAGE ID (possibly in different repositories). First of all, you need to delete the tags according to these image names. When you delete the last tag, the image will be deleted automatically.

If the IMAGE NAME you want to delete is in the same REPOSITORY, you can delete the remaining TAG; at the same time through docker rmi. If you are in a different Repo, you still need to delete the TAG one by one.

The mirror cannot be deleted when there is also a container started by this image (even if it has been stopped)

TO-DO

How to view the dependency between the image and the container

Delete Container

Docker rm

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

Delete Mirror

Docker rmi

Here is a complete example:

# docker images d9bbd13f5066Removing intermediate container e08963fd5afbSuccessfully built d9bbd13f5066

The above PATH is., so all files in the current directory (excluding those in .dockerboards) will be packaged by tar and transferred to docker daemon (usually native). From the output to Sending build context..., there is a Removing intermediate container process, which can retain the container through-- rm=false.

TO-DO

Docker build github.com/creack/docker-firefox failed.

6. Tag the image (tag)

There are two main functions of tag: one is to give an easy-to-understand name for the image, and the other is to re-specify the repository of the image through docker tag, so that it can be automatically submitted to the repository during push.

Merge all tag of the same IMAGE_ID into a new # docker tag 195eb90b5349 seanlook/ubuntu:rm_test, create a new tag, and keep the old record # docker tag Registry/Repos:Tag New_Registry/New_Repos:New_Tag7. View container information container (ps)

The docker ps command can view the container's CONTAINER ID, NAME, IMAGE NAME, port opening and binding, and the COMMNAD executed after the container starts. CONTAINER_ID is often found through ps.

Docker ps displays the currently running container by default

Docker ps-a view includes all containers that have been stopped

Docker ps-l shows a newly started container (including stopped ones)

8. View the running processes in the container (top)

The container runtime does not necessarily have / bin/bash terminals to interactively execute top commands to view the running processes in container, and there may not be top commands, which is useful for docker top. In fact, using ps-ef on host | grep docker can also see a similar set of process information. Think of the process in container as a child process that starts docker on host.

9. Other commands

Docker also has some commands that are not very commonly used, such as login, cp, logs, export, import, load, kill, etc., which are relatively simple.

These are all the contents of this article entitled "what are the commonly used commands in docker?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report