Docker basic command details

  • 2020-05-17 07:19:16
  • OfStack

Basic concept of docker

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container and release them to any popular Linux machine.

Docker is an open platform that redefines the process of application development, testing, delivery, and deployment. Docker can be called build 1, run everywhere. This is what docker calls "Build once, Run anywhere".

Create a mirror image

There are three ways to create a mirror:

Create based on an existing container

Import based on local templates

Based on the dockerfile

Create based on an existing container

Mainly using docker commit command, command format:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:tag]]

-a,--author="" author information
-m,--message="" submit the message
-p,--pause=true pause the container when committing

Such as:


# docker run -it centos /bin/bash
[root@d7e7ac1cbca2 /]# touch test
[root@d7e7ac1cbca2 /]# 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
5d318afa9e6f7fdb755db97e29e3860b752f24b0b50e6bfa0b7e457450802c0e
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_copy latest 5d318afa9e6f 13 seconds ago 196.7 MB

Import based on local templates

It is recommended to create using the templates provided by openVZ


https://openvz.org/Download/templates/precreated
#cat centos-7-x86_64-minimal.tar.gz.crdownload | docker import - centos:latest

Save 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 the dockerfile

The following content is introduced in detail

Run the first docker container


# docker run centos echo "hello world"
Unable to find image 'centos:latest' locally
latest: Pulling from centos
47d44cb6f252: Pull complete
168a69b62202: Pull complete
812e9d9d677f: Pull complete
4234bfdd88f8: Pull complete
ce20c473cd8a: Pull complete
centos: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:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5
Status: Downloaded newer image for centos:latest
hello world

Command description:

docker run: standard container startup command

centos: image name, latest by default

echo and later: commands to execute after the container is started

Start an interactive container


docker run -it centos /bin/bash

* note: -t designation specifies a dummy terminal or terminal in the container, -i designation allows us to interact with STDIN in the container

Start one docker container as a service

If you actually test it, you might also find that the first "hello world" container exits after executing the echo command, and the second interactive container exits as soon as the user exits the bash of the current container. This obviously does not meet the requirement of a long running service, so please find that docker run provides the '-d' parameter, which can be used to start the container as a daemon.


docker run -d centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2; <br>179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33

This long string is called container ID. It is the 1-only identity of the container, so we can use it to manipulate the container, such as viewing the log, stopping or deleting the container, and so on.


dock logs 179fc7f17c358834364d

Why use an infinite loop to output?

Because if it's not a dead loop, after one output, the process in the container ends. The container stops when all of its singleton processes are finished. Therefore, if a concrete service is to be run in a container, the service itself must be run in the container as a daemon.


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

Main options:

-d: run the container in the back-end mode
-t: provides a dummy terminal
-i: provides interactive input, 1 normally works with "-t" 1. If only "-i" option is provided, the container cannot exit after startup
1 - v: mapping volume to container, such as: - p/data/www: / var/www/html
-p: maps the container's port to the host, as in: -p 8080:80

More command actions


# docker images  Lists all local mirrors 
# docker search centos  Searches for images from the default mirror repository 
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of 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 / 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]
#docker pull centos  Download the image locally 
#docker create -it ubuntu:latest  create 1 A container 
Unable to find image 'ubuntu:latest' locally
latest: Pulling from ubuntu
58488e45273c: Pull complete 
25810b66099e: Pull complete 
6571ba684f54: Pull complete 
6ed49a73d8f0: Pull complete 
c53777cbfc31: Pull complete 
56465e1e45d2: Pull complete 
Digest: sha256:312986132029d622ae65423ca25d3a3cf4510de25c47b05b6819d61e2e2b5420
Status: Downloaded newer image for ubuntu:latest
1330233e50aba7fca99e5914bd28dd89321bc86ec35fb36b4775d3424337c190
docker create  The container created by the command is in a stop state and needs to be used docker start  To start it 
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1330233e50ab ubuntu:latest "/bin/bash" About a minute ago happy_engelbart
docker run  A command is equivalent to executing first docker create Command,   And then execute it docker start  The command 
# docker run ubuntu /bin/echo "hello world"
hello world

Into the container

Method 1:

docker attach a54615a88787 is followed by the container name or id. docker container exits after exit, which is not commonly used

Method 2:

docker exec-it a54615a88787 /bin/bash follows the container name or id

Method 3:


yum -y install util-linux
# docker inspect --format "{{.State.Pid}}" stupefied_cray  At the end is the name of the container 
4899
# nsenter --target 4899 --mount --uts --ipc --net --pid

The script


https://openvz.org/Download/templates/precreated
#cat centos-7-x86_64-minimal.tar.gz.crdownload | docker import - centos:latest
0

Related articles: