Source: blog.csdn.net/weimenglala… Blog.csdn.net/wzwwzwwww/a… www.cnblogs.com/shenyixin/p…

Docker starts, restarts, and closes commands

Start the systemctlstartRestart sudo systemctl daemon-reloadRestart the Docker service. systemctl restart docker restart the Docker service. sudo service docker restart Stop the docker service systemctl stop dockerCopy the code

Docker installation

1. Check the kernel version

  • Docker requires a Centos kernel version later than 3.10
uname -r
Copy the code
  • Native kernel version: 3.10.0-327.el7.x86_64

2. Update yum packages to the latest

  • The time may be a little long, slowly wait……
yum update
Copy the code

3. Install required software packages.

  • Yum-util provides yum-config-manager functionality, the other two are required by the Devicemapper driver
yum install -y yum -utils device-mapper-persistent-data lvm2
Copy the code

4, view the docker version of the repository

yum list docker-ce --showduplicates | sort -r
Copy the code

5, installation docker

yum install docker-ce
Copy the code

6, start docker, set boot docker

  • Be sure to reboot after installation
systemctl startDocker systemctl enable docker systemctl stop dockerCopy the code

7. View the version

docker version
Copy the code

8. Check whether the startup is successful by using the search command

docker search mysql
Copy the code

9. View the log status

systemctl status docker.service
Copy the code

Docker change source

  • Address for obtaining Aliyun image: cr.console.aliyun.com/cn-hangzhou… Once logged in, you can see the exclusive accelerator address
  • You can use the accelerator by modifying the daemon configuration file /etc/docker-daemon. json

1, the daemon. Json modification

If you don’t have the file, you can create a new one, or use the following steps: Create a new folder

sudo mkdir -p /etc/docker
Copy the code

Write the contents of daemon.json

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["Your exclusive source address."]
}
EOF
Copy the code

Daemon restart

sudo systemctl daemon-reload
Copy the code

Restart the docker

sudo systemctl restart docker
Copy the code

2. Check whether the configuration is successful

docker info
Copy the code

3. Source switching of other systems

Uninstall the docker

1. View the installed packages

yum list installed | grep docker
Copy the code
  • X86_64, docker-client.x86_64, docker-common.x86_64

2, Delete the docker-related software package installed:

yum -y remove docker.x86_64

yum -y remove docker-client.x86_64

yum -y remove docker-common.x86_64
Copy the code

The basic concept

Docker consists of three basic concepts:

  • Image: Docker Image, which is equivalent to a root file system. For example, the official ubuntu:16.04 image includes a complete set of root files for ubuntu 16.04’s minimum system.
  • Container: The relationship between an Image and a Container is similar to that between a class and an instance in object-oriented programming. An Image is a static definition and a Container is an entity of the Image runtime. Containers can be created, started, stopped, deleted, paused, and so on.
  • Repository: A Repository looks at a code control center that holds images.

Docker uses a client-server (C/S) architecture pattern that uses remote apis to manage and create Docker containers

Docker container command

grammar

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Copy the code

The OPTIONS:

-a stdin: specifies the standard input and output types. The options are stdin, STDOUT, or STDERR. -d: Runs the container in the background and returns the container ID. -i: Runs the container in interactive mode, usually with -t. -p: indicates a random port mapping. Ports inside a container are randomly mapped to high-end ports on a host. -P: indicates a specified port mapping. --name="nginx-lb": Specifies a name for the container. --dns8.88.8.: Specifies the DNS server used by the container. The default DNS server is the same as the host server. --dns-search example.com: specify the container DNS search domain name, which is the same as the host by default. -h"mars": Specifies the hostname of the container. -e username="ritchie": Sets environment variables. --env-file=[]: reads environment variables from specified files; --cpuset="0-2" or --cpuset="0": binds the container to a specified CPU to run. -m: sets the maximum memory used by a container. --net="bridge"Specifies the network connection type, container support bridge/host/none/container: four types; --link=[]: Add link to another container; --expose=[]: Open a port or group of ports; --volume, -v: Binds a volumeCopy the code

Hello world output

Hello world docker run Ubuntu:15.10 /bin/echo "Hello world"
ubuntu:15.10To specify the image to run, Docker first looks up the image from the localhost to see if it exists. If not, Docker downloads the public image from the image repository Docker Hub. /bin/echo "Hello world": Command executed in the started containerCopy the code

Start the container

  • The background model
docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
Copy the code

Specify the container name

docker run -itd --name ubuntu-testUbuntu /bin/bash Note: Added-dParameters do not enter the container by default. To enter the container, use the docker exec directiveCopy the code

Into the container

Docker attach Docker exec: It is recommended to use the docker exec command, because this will not cause the container to stop.Copy the code

The attach command

docker attach 1e560fca3906 
Copy the code
  • Note: Exiting from this container will cause the container to stop.

The exec command

docker exec -it 243c32535da7 /bin/bash
Copy the code
  • Note: Exiting from this container does not stop the container, which is why it is recommended to use Docker Exec.

View the running container

docker ps
Copy the code

CONTAINER ID: indicates the ID of a CONTAINER. IMAGE: IMAGE used. COMMAND: The COMMAND that is run when the container is started. CREATED: time when the container was CREATED. STATUS: indicates the STATUS of the container. There are seven states:

  • Created (already created)
  • Restarting
  • Running
  • Removing (on the move)
  • Paused
  • Exited (stop)
  • He was dead.

PORTS: port information of the container and connection type used (TCP \ UDP). NAMES: automatically assigned container NAMES.

To view all containers, run the following command:

docker ps -a
Copy the code

Exit the terminal

exitOr CTRL DCopy the code

Stop the container

docker stop 2B1b7a428627 or docker stopCopy the code

Restart the container

Docker restart < container ID>Copy the code

Remove the container

Delete container using docker rm command:

docker rm -f 1e560fca3906
Copy the code

Clean up all terminating containers

docker container prune
Copy the code

Export and import containers

Export container

To export a local container, use the docker export command.

docker export 1e560fca3906 > ubuntu.tar
Copy the code
  • Export container 1e560fCA3906 snapshot to the local file ubuntu.tar.

Importing a Container Snapshot

You can use docker import to import the snapshot file ubuntu.tar into the image test/ Ubuntu :v1:

cat docker/ubuntu.tar | docker import - test/ubuntu:v1
Copy the code

Alternatively, it can be imported by specifying a URL or directory, for example:

docker import http://example.com/exampleimage.tgz example/imagerepo
Copy the code

Run a Web application

docker run -d -P training/webapp python app.py
-P: maps the network ports used inside the container to the hosts we use.Copy the code

We can also set different ports with the -p parameter:

docker run -d -p 80:5000 training/webapp python app.py //80It's the outer machine port,5000Is the container portCopy the code

You can view ports directly

docker port bf08b7f2cd89
5000/tcp -> 0.0.0.0:5000
Copy the code

View WEB application logs

docker logs -fBf08b7f2cd89 or Docker logs2b1b7a428627
Copy the code

View the processes of the WEB application container

We can also use Docker Top to see the processes running inside the container

runoob@runoob:~$ docker top wizardly_chandrasekhar
UID PID PPID ... TIME CMD
root 23245 23228.00:00:00 python app.py
Copy the code

Check the WEB application

Use Docker inspect to view the underlying information of docker. It returns a JSON file that records the configuration and status of the Docker container.

runoob@runoob:~$ docker inspect wizardly_chandrasekhar
[
    {
        "Id": "bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85"."Created": "The 2018-09-17 T01: the 174228707 z"."Path": "python"."Args": [
            "app.py"]."State": {
            "Status": "running"."Running": true."Paused": false."Restarting": false."OOMKilled": false."Dead": false."Pid": 23245."ExitCode": 0."Error": ""."StartedAt": "The 2018-09-17 T01: the 494185806 z"."FinishedAt": "0001-01-01T00:00:00Z"
        },
.
Copy the code

Query the last container created

docker ps -l 
Copy the code

Docker image operation

Listing mirrors

docker images 
Copy the code

Find the mirror

docker search httpd
Copy the code

Drag take mirror

docker pull httpd
Copy the code

Remove the mirror

docker rmi hello-world
Copy the code

Create a mirror image

When the image we download from the Docker image repository does not meet our needs, we can change the image in the following two ways.

  • Update the image from the created container and commit the image
  • Use the Dockerfile directive to create a new image

Update image

Before updating the image, we need to create a container using the image.

runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/#
Copy the code

Use apt-get update in the running container.

After completing the operation, type the exit command to exit the container.

At this time, the container with ID E218EDB10161 is the container changed according to our requirements. We can commit a container copy by using the docker commit command.

runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
Copy the code

Parameter description:

-m: Description to be submitted-aE218edb10161: container ID Runoob/Ubuntu :v2: Specifies the name of the target image to be createdCopy the code

We can use the Docker images command to view our new image runoob/ Ubuntu :v2:

runoob@runoob:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
runoob/ubuntu       v2                  70bf1840fd7c        15 seconds ago      158.5 MB
ubuntu              14.04               90d5884b1ee0        5 days ago          188 MB
php                 5.6                 f40e9e0f10c8        9 days ago          444.8 MB
nginx               latest              6f8d099c3adc        12 days ago         182.7 MB
mysql               5.6                 f2e8d6c772c0        3 weeks ago         324.6 MB
httpd               latest              02ef73cf1bc0        3 weeks ago         194.4 MB
ubuntu              15.10               4e3b13c8a266        4 weeks ago         136.3 MB
hello-world         latest              690ed74de00f        6 months ago        960 B
training/webapp     latest              6fae60ef3446        12 months ago       348.8 MB
Copy the code

Use our new image runoob/ Ubuntu to start a container

runoob@runoob:~$ docker run -t -i runoob/ubuntu:v2 /bin/bash                            
root@1a9fbdeb5da3:/#
Copy the code