1. List the images on the machine

# docker images 
REPOSITORY               TAG             IMAGE ID        CREATED         VIRTUAL SIZE
ubuntu                   14.10           2185fd50e2ca    13 days ago     236.9 MB
Copy the code

Repos_name = ‘REPOSITORY’; / / Repos_name = ‘REPOSITORY’; / /repos_name = ‘REPOSITORY’; / /repos_name = ‘REPOSITORY’; Similar to regsistory.example.com: 5000 / repos_name said private servers. The IMAGE ID column is actually an abbreviation, and the –no-trunc option is used to display it completely

2. Search for image in docker index (search)

Usage: docker search TERM

# docker search seanlo
NAME                DESCRIPTION           STARS     OFFICIAL   AUTOMATED
seanloook/centos6   sean's docker repos 0Copy the code

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

3. Pull image or repository from Docker Registry Server (pull)

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

# docker pull centos
Copy the code

Before docker v1.2, you could download all the official images in the centos repository. Will pull the centos:latest image, its intermediate layers and any aliases of the same ID, That is, only images with the latest tag (and other tags with the same image ID) will be downloaded. You can also specify a specific mirror:

# docker pull centos:centos6
Copy the code

Docker pull username/repository<:tag_name> :

# docker pull seanlook/centos:centos6
Copy the code

If you don’t have a network, or from other private servers to get the mirror, like docker pull registry.domain.com: 5000 / repos: < tag_name >

# docker pull dl.dockerpool.com:5000/mongo:latest
Copy the code

4. Push an image or repository to Registry (push)

Corresponding to pull above, it can be pushed to Docker Hub’s Public, Private and Private servers, but cannot be pushed to Top Level Repository.

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

Registry.tp-link.net can also be written as IP, 172.29.88.222. In the absence of a repository, a push from the command line creates a private repository for us, whereas a push from the browser defaults to a public repository.

5. Start a container (run) from the image

The Docker run command first creates a writable container layer from a specific image and then starts it with the start command. A stopped Container can be restarted and retain the original changes. There are a lot of the run command to start the parameters, below is some general instructions, more parts please refer to www.cnphp6.com/archives/24… When docker run is used to create containers, the standard operations that Docker runs in the background include:

  • Check whether the specified image exists locally and download it from the public repository if it does not
  • Create and start a container with an image
  • Assign a file system and mount a read-write layer outside the read-only mirror layer
  • Bridge a virtual interface from the host host’s configured bridge interface to the container
  • Configure an IP address from the address pool to the container
  • Execute the user-specified application
  • The container is terminated after execution

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

5.1 Using image to create a Container, run the corresponding command, and stop the container

# docker run ubuntu echo "hello world"
hello word
Copy the code

This is the easiest way to do it, and it hardly feels any different from simply executing echo ‘Hello World’ locally, when in fact it will start from the local Ubuntu: Latest image to a container, execute the print command and exit (docker ps -l can be viewed). Note that there is a default –rm=true parameter, which stops the container and removes it from the file system. Because Docker containers are so lightweight, most of the time users delete and create containers at any time. After the CONTAINER is started, it automatically generates a CONTAINER ID, which can be changed to an IMAGE ID after the commit command is executed

Create a container using image and enter interactive mode. The login shell is /bin/bash

# docker run -i -t --name mytest centos:centos6 /bin/bashBash - 4.1 -#
Copy the code

The –name argument above can specify the name of the container after startup, otherwise Docker will pick a name for us. Centos: Mirror centos can also use IMAGE ID (68edf809afe7), and will start a dummy terminal, but we can only see one or two processes through ps or top command, because the core of the container is the application being executed, and the resources required are necessary for the application to run. In addition, there are no other resources, which shows that Docker has a very high utilization rate of resources. After exit or Ctrl+D, the container will disappear. Which TAG will be used to start images with different tags and the same IMAGE ID

5.2 Running a Container To Run in the Background

# docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 2; done"
ae60c4b642058fefcc61ada85a610914bed9f5df0e2aa147100eab85cea785dc
Copy the code

It will suspend the started Container and run it in the background (this is called saas). It will output a Container ID, and you can see the container information through Docker PS. Docker logs AE60C4B64205 can also be attached to the running terminal by Docker attach AE60c4B64205. At this point, when Ctrl+C exits the container, the container disappears. Press Ctrl-p ctrl-q to exit the host while the Container is still running. In addition, if -d starts but the following commands stop, such as /bin/bash or echo test, the Container will still terminate when it is finished. And -d cannot be used with –rm. You can run memcached, Apache, etc.

5.3 Mapping host Ports and Directories to Containers

It is useful to map the host to the container’s port. For example, if you run memcached in a Container on port 11211, the host running the container can connect to internel_IP :11211 of the container. If you need to access memcached from another host, you can use the -p option, such as -p

, in the following ways:

-p 11211:11211 this is the default, To bind port 11211 of all network adapters on a host (0.0.0.0) to port 11211 of a container, run the following command: -p 127.0.0.1:11211:11211 To bind only port 11211 of interface localhost -p 127.0.0.1::5000 -p 127.0.0.1:80:8080Copy the code

In fact, directory mapping is to “bind and mount” the host path to the directory of container, which is convenient for transferring files inside and outside. In the section of private server building, in order to prevent images saved after private server container stops being deleted, submitted images should be saved to the mounted host directory. -v

is easy to use. If multiple directories are bound, add -v.

-v /tmp/docker:/tmp/docker
Copy the code

Alternatively, you can establish a link between two Containers -link, as described in the advanced section or official documentation. Here’s an example:

# docker run --name nginx_test \> - v/TMP/docker: / usr/share/nginx/HTML: ro \ > p - 80-80 - d \ > nginx: 1.7.6Copy the code

Create index.html under/TMP /docker on the host and you can access it at http://localhost:80/ or http://host-ip:80.

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

When we create our own image, we install tools and change the configuration in the Container. If we do not commit, these changes will disappear when the Container stops and starts again. Docker commit

[repo:tag] repo:tag Optional

View the container that you just ran# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9fdf26326c9 nginx:1 nginx -g.. 3 hours ago Exited (0).. Nginx_test starts an existing container (run from image). Docker start nginx_test can also be used instead of [root@hostname docker].# docker start c9fdf26326c9
c9fdf26326c9
 
 
docker run -i -t --sig-proxy=false21ffe545748baf /bin/bash The nginx service is not started# docker commit -m "Some Tools installed" fcbd0a5348ca seanlook/ Ubuntu :14.10_tutorial
fe022762070b09866eaab47bc943ccb796e53f3f416abf3f2327481b446a9503
Copy the code

-a “[email protected]” Note that when you commit a container repeatedly, you will get a new IMAGE ID each time. The repository:tag of the previously submitted image will become < None >:< None >, so avoid repeated submissions. Also, observe the following:

  • Commit Container only pauses the container to ensure file system consistency, but does not stop the container. If you want to make other changes to the container:

    • You can resubmit to get a new image2, delete the new image e1
    • You can also close the container and start with a new image1, continue to modify, and delete image1 after submitting image2
    • Of course this is very painful, so it is generally usedDockerfiletobuildTo obtain the final image, refer to []
  • Although a new image is created and you can see that it is 100MB in size, you quickly learn from the commit process that it is not actually taking up 100MB of disk space on its own, but is simply modifying the old image, which shares most of the common “slice”.

Under the

1. Start/Stop /restart The Container (start/stop/restart).

A container can run a new one using run or restart a stopped container, but start cannot specify the instructions to run when the container is started, because docker can only have one foreground process. When the container stops (or Ctrl+D), it exits after saving the current state of the container, and the next time it starts, it keeps the changes it made when it was last closed. And the attach screen is the same every time you enter it, the same as when the first run started or when the commit was committed.

CONTAINER_ID=$(docker start <containner_id>)
docker stop $CONTAINER_ID
docker restart $CONTAINER_ID
Copy the code

These commands can be used with a complete example of how Docker creates a container that runs a daemon and provides a shell terminal.

2. Connect to a running Container (attach)

The container to attach must be running and can be attached to the same container to share the screen (similar to attach with the screen command). The official documentation says that you can detach using Ctrl-C after attaching, but in fact, in my tests, if the Container is currently running bash, Ctrl-C is naturally the input on the current line and does not exit. If the Container is currently running processes in the foreground, such as outputting nginx’s access.log, Ctrl-C will not only exit the container, but also stop it. This is not what we want, detach is supposed to mean detach from the container terminal, but the container still runs. Luckily, attach can be attached with –sig-proxy=false to ensure ctrl-D or Ctrl-C does not close the container.

# docker attach --sig-proxy=false $CONTAINER_ID
Copy the code

3. View the underlying information of image or container (inspect)

Inspect objects can be an image, a running Container, or a stopped Container.

View the internal IP of the container# docker inspect --format='{{.NetworkSettings.IPAddress}}' $CONTAINER_ID
172.17.42.35
Copy the code

4. Delete one or more Containers and images (rm, RMI).

You may build or commit many images during use, and useless images need to be removed. However, there are some conditions for deleting these mirrors:

  • The sameIMAGE IDThere could be more than oneTAGFirst, you need to remove tags based on these image names. When the last tag is removed, the image is automatically deleted.
  • Bearing, if you want to delete more than oneIMAGE NAMEIn the sameREPOSITORY, can be accessed throughdocker rmi <image_id>To delete the rest at the same timeTAG; You still need to manually delete them one by one if they are in different REPOSTAG;
  • When a Container started by the image exists (even if it has stopped), the image cannot be deleted.

How does to-do check the dependency between an image and a container

Docker rm

Docker rm $(docker ps-a-q)Copy the code

Docker rmi

Here is a complete example:

# docker images <==Ubuntu 13.10 195eb90b5349 4 months ago 184.6 MB Ubuntu Saucy 195eb90b5349 4 months ago 184.6 MB Seanlook/Ubuntu RM_test Use 195eb90B5349 to start or stop a container and delete the image# docker rmi 195eb90b5349
Error response from daemon: Conflict, cannot delete image 195eb90b5349 because it is 
tagged inmultiple repositories, use -f to force 2014/11/04 14:19:00 Error: Failed to remove one or more images from seanlook repository# docker rmi seanlook/ubuntu:rm_testUntagged: Seanlook/Ubuntu: RM_test If the image is deleted, rmI will not be allowed due to the existing Container# docker rmi 195eb90b5349Error response from daemon: Conflict, cannot delete 195eb90b5349 because the container eef3648a6e77 is using it, Use -f to force 2014/11/04 14:24:15 Error: Failed to remove one or more images <==# docker rm eef3648a6e77Delete the mirror <==# docker rmi 195eb90b5349
Deleted: 195eb90b534950d334188c3627f860fbdf898e224d8a0a11ec54ff453175e081
Deleted: 209ea56fda6dc2fb013e4d1e40cb678b2af91d1b54a71529f7df0bd867adc961
Deleted: 0f4aac48388f5d65a725ccf8e7caada42f136026c566528a5ee9b02467dac90a
Deleted: fae16849ebe23b48f2bedcc08aaabd45408c62b531ffd8d3088592043d5e7364
Deleted: f127542f0b6191e99bb015b672f5cf48fa79d974784ac8090b11aeac184eaaff
Copy the code

Note that the deletion process I used above is quite special – the image is tagged in multiple repositories, and there are also started containers. Follow the order indicated by <==.

5. Docker build uses this configuration to generate new images

The build command can create an image from a Dockerfile and context: Docker build [OPTIONS] PATH | | – the above URL PATH or URL of the file is called the context, the build process of the image will look over these documents transmitted to the server of a docker. If the PATH is a separate Dockerfile, you don’t need the context; If the URL is a Git repository address, Git will clone a copy of the image to a local temporary directory during the image creation process, which becomes the context for the build. No matter what PATH is specified, the Dockerfile is essential, see the Dockerfile Reference. Look at the following example:

# cat Dockerfile 
FROM seanlook/nginx:bash_vim
EXPOSE 80
ENTRYPOINT /usr/sbin/nginx -c /etc/nginx/nginx.conf && /bin/bash
 
# docker build -t seanlook/nginx:bash_vim_Df .Sending build context to Docker daemon 73.45MB Sending build context to Docker daemon Step 0: FROM seanlook/nginx:bash_vim ---> aa8516fa0bb7 Step 1 : EXPOSE 80 ---> Using cache ---> fece07e2b515 Step 2 : ENTRYPOINT /usr/sbin/nginx -c /etc/nginx/nginx.conf && /bin/bash ---> Runningin e08963fd5afb
 ---> d9bbd13f5066
Removing intermediate container e08963fd5afb
Successfully built d9bbd13f5066
Copy the code

The PATH above is., so all files in the current directory (except.dockerignore) will be tarred and sent to the Docker Daemon (usually on the local machine), from which we can send build Context… And finally, to remove an Intermediate container, you can keep the container by –rm=false. To-do docker build github.com/creack/docker-firefox failed.

6. Tag the image

The tag serves two main purposes: it gives the image a name that is easy to understand, and the docker tag can be used to respecify the repository of the image so that it is automatically committed to the repository when pushed.

Merge all tags of the same IMAGE_ID into a new one# docker tag 195eb90b5349 seanlook/ubuntu:rm_testCreate a new tag and keep the old record# docker tag Registry/Repos:Tag New_Registry/New_Repos:New_Tag
Copy the code

7. View the information about the container container (ps)

The docker ps command displays the CONTAINER ID, NAME, IMAGE NAME, port start and binding, and COMMNAD executed after the CONTAINER is started. You can always find CONTAINER_ID using Photoshop. Docker ps -l docker ps -l docker ps -l docker ps -l docker ps -l

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

The /bin/bash terminal is not required to run the top command in the container. It is not required to run the top command in the container. Docker top

Actually used on the host ps – ef | grep docker also can see a group of similar process information, the process in the container as the host start the docker’s child process.

9. Other commands

Docker also has some less common commands, such as login, cp, logs, export, import, load, kill, etc., which are relatively simple, please refer to the official website.

reference