Docker-3 basic commands and docker-3 commands
Create an image
There are three ways to create an image:
- Create based on existing containers
- Import based on local Template
- Based on dockerfile
Create based on existing containers
Mainly use the docker commit command. Command Format:
Docker commit [OPTIONS] CONTAINER [REPOSITORY [: tag], mainly including:
-A, -- author = "" author information
-M, -- message = "" Submit a message
-P, -- pause = true: pause the container when submitting
For example:
# Docker run-it centos/bin/bash
[Root @ d7e7ac1cbca/] # touch test
[Root @ d7e7ac1cbca/] # ls
Anaconda-post.log bin dev etc home lib lib64 lost + found media mnt opt proc root run sbin srv sys test tmp usr var
# Docker commit-m "add a file"-a "kafeikele" de6 centos_copy
Bytes
# Docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Centos_copy latest 5d318afa9e6f 13 seconds ago 196.7 MB
Import based on local Template
We recommend that you use the template provided by openVZ to create
Https://openvz.org/Download/templates/precreated
# Cat centos-7-x86_64-minimal.tar.gz.crdownload | docker import-centos: latest
Store and Import images
# Docker images
Centos 7.1.1503 47a77536ad4c 8 weeks ago 212.1 MB
# Docker save-o centos_7.1.tar centos: 7.1.1503
# Docker load -- input centos_7.1.tar
# Docker load <centos_7.1.tar
Based on dockerfile
Subsequent content
Run the first docker container
# docker run centos echo "hello world"Unable to find image 'centos:latest' locallylatest: Pulling from centos47d44cb6f252: Pull complete168a69b62202: Pull complete812e9d9d677f: Pull complete4234bfdd88f8: Pull completece20c473cd8a: Pull completecentos:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.Digest: sha256:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5Status: Downloaded newer image for centos:latesthello world
Command description:
Docker run: standard container startup command
Centos: image name. The default value is latest.
Echo and the following content: commands executed after the container is started
Start an interactive container
docker run -it centos /bin/bash
* Note: The-t mark specifies a Pseudo Terminal or terminal in the container. The-I mark allows us to interact with STDIN in the container.
Start a docker container as a service
If you test it, it is estimated that the first "hello world" container exits after running the echo command, and the second interactive container, as long as the user exits the bash of the current container, the container also exits. This obviously cannot meet the requirement of a service that runs for a long time. It is recommended that docker run provide the '-d' parameter to start the container as a daemon process.
docker run -d centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2;
179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33
The long string is called the container ID. It is the unique identifier of a container, so we can use it to operate containers, such as viewing logs, stopping or deleting containers.
dock logs 179fc7f17c358834364d
Why is an endless loop used for output?
If it is not an endless loop, the process in the container will end after one output. The container stops when the only process of the container ends. Therefore, to run a specific service in the container, the service itself must also run in the daemon process mode.
Docker run [OPTIONS] IMAGE [COMMAND] [ARG...] main options:-d: run the container in the future-t: provide a Pseudo Terminal-I: provide interactive input, generally used with "-t, if you only provide the "-I" option, the container cannot exit after startup-v: maps a volume to the container, such as-p/data/www: /var/www/html-p: map the container port to the host machine, for example,-p 8080: 80
More command operations
# Docker images List all local images # search for The image name description stars official AUTOMATEDcentos The official build of centos from The default image repository in docker search CentOS. 2767 [OK] ansible/centos7-ansible Ansible on Centos7 90 [OK] jdeathe/centos-ssh CentOS-6 6.8 x86_64/CentOS-7 7.2.1511 x8... 42 [OK] jdeathe/centos-ssh-apache-php CentOS-6 6.8 x86_64-Apache/PHP M... 21 [OK] nimmis/java-centos This is docker images of CentOS 7 with dif... 17 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 14 [OK] # download the image from docker pull centos to the local machine # docker create-it ubuntu: latest create a container Unable to find image 'ubuntu: latest 'locallylatest: Pulling from ubuntu58488e45273c: pull complete merge: Pull complete 6571ba684f54: Pull complete 6ed49a73d8f0: Pull complete c53777cbfc31: Pull complete Digest: sha256: Digest: Downloaded newer image for ubuntu: the container CREATED by the container create COMMAND is in the stopped state and needs to be started with docker start # docker ps-aCONTAINER id image command created status ports NAMES1330233e50ab ubuntu: latest "/bin/bash" About a minute ago happy_engelbartdocker run Command is equivalent to executing the docker create Command first, then run the docker start command # docker run ubuntu/bin/echo "hello world" hello world
Enter container
Method 1:
# Docker attach a54615a88787 is followed by the container name or id. After exiting, The docker container also exits and is not commonly used.
Method 2:
# Docker exec-it a54615a88787/bin/bash is followed by the container name or id
Method 3:
Yum-y install util-linux
# Docker inspect -- format "{. State. Pid}" stupefied_cray end with the container name
4899
# Nsenter -- target 4899 -- mount -- uts -- ipc -- net -- pid
Script
#!/bin/bashCNAME=$1CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME)nsenter --target $CPID --mount --uts --ipc --net –pid
01:00:26