Use of docker command

Keywords: Java Linux Operation & Maintenance Docker

1,Docker

  • Docker is an open source application container engine, which is based on Go language and complies with Apache 2.0 protocol.
  • Docker allows developers to package their applications and dependent packages into a lightweight and portable container, and then publish them to any popular Linux
    On the machine, virtualization can also be realized.
  • Containers completely use the sandbox mechanism, and there will be no interfaces between them (similar to iPhone app s). More importantly, the performance overhead of containers is very low.

After version 17.03, Docker is divided into CE (Community Edition) and EE (Enterprise Edition). We can use the Community Edition.

1.1 Docker application scenarios

  • Automated packaging and publishing of Web applications.
  • Automated testing and continuous integration and release.
  • Deploy and adjust databases or other background applications in a service-oriented environment.
  • Compile or extend the existing OpenShift or Cloud Foundry platform from scratch to build your own PaaS environment.

1.2 advantages of docker

  • Deliver your applications quickly and consistently
  • Responsive deployment and expansion
  • Running more workloads on the same hardware

1.3 Docker architecture

Docker includes three basic concepts

  • Image: Docker image is equivalent to a root file system. For example, the official image ubuntu:16.04
    It contains a complete set of root file system of the smallest system of Ubuntu 16.04.
  • Container: the relationship between an Image and a container is like a class and instance in object-oriented programming. An Image is a static definition, and a container is an entity when the Image runs. Containers can be created, started, stopped, deleted, paused, and so on.
  • Repository: a repository can be regarded as a code control center for storing images.

2. Using docker image

The command of docker image is as follows:

login /logout login and logout commands
pull pulls or updates the specified image from the image warehouse
search find images from Docker Hub
Images lists the local images
rmi Deletes one or more local mirrors
tag marks the local image and classifies it into a warehouse

2.1 login /logout login and logout commands

u: login user name
-p: login password

example:
docker login -u user name -p password

2.2 pull to pull or update the specified image from the image warehouse

a: pull all tagged images
– disable content trust: ignore the verification of the image. The default instance is docker pull java
– format: Specifies the template file of the return value;
– no TRUNC: display complete image information;
-q: only the image ID is displayed.

example:
docker images

2.5 rmi delete one or more local mirrors

f: forced deletion;
– no prune: the process image that does not remove the image is removed by default;

example:
docker rmi -f runoob/ubuntu:v4

2.6 tag the local image and classify it into a warehouse

 docker tag ubuntu:15.10 runoob/ubuntu:v3
Other examples:
docker pull httpd
docker images
docker search httpd
docker rmi hello-world
//Set mirror label
docker tag 860c279d2fec runoob/centos:dev

3. Docker container usage

  • run creates a new container and runs a command
  • start/stop/restart
  • kill kills a running container
  • rm Deletes one or more containers pause pauses all processes in the container
  • unpause restores all processes in the container
  • create creates a new container without starting it
    exec executes commands in the running container
    ps list containers
    top view the process information running in the container, and support ps command parameters
    Logs gets the logs of the container
    Port lists the port mappings for the specified container, or the lookup will be PRIVATE_PORT NAT to public facing port
    network

3.1 run creates a new container and runs a command

d: Run the container in the background and return the container ID;
-i: Run the container in interactive mode, usually at the same time as - t;
-P: Random port mapping: the internal port of the container is randomly mapped to the port of the host
-p: Specify the port mapping in the format: host (host) port: container port
-t: Reassign a pseudo input terminal to the container, usually used with - i;
– name = "nginx LB": specify a name for the container;
-e username = "ritchie": set environment variables;

example:
docker run -itd --name test1 --network test-net ubuntu /bin/bash
docker run -p 80:80 -v /data:/data -d nginx:latest
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
docker run -it nginx:latest /bin/bash
docker run -d --name nginx -p 83:80 -v  /home/nginx/nginx.conf/:/etc/nginx/nginx.conf/ -v /home/nginx/logs:/var/log/nginx -v /home/nginx/html:/usr/share/nginx/html -v /home/nginx/conf:/etc/nginx/conf.d --privileged=true nginx

3.2 start/stop/restart

3.3 kill a running container

s: Send a signal to the container

docker kill -s KILL mynginx

3.4 rm delete one or more containers

f: forcibly delete a running container through SIGKILL signal.
-l: remove the network connection between containers, not the container itself.
-v: delete the volume associated with the container.

example:
docker rm -f db01 db02
docker rm $(docker ps -a -q)

3.5 pause pauses all processes in the container

3.6 unpause restores all processes in the container

3.7 create creates a new container without starting it

docker create  --name myrunoob  nginx:latest 

3.8 exec executes commands in the running container

d: separation mode: running in the background
-i: keep STDIN open even if there is no attachment
-t: assign a pseudo terminal

docker exec -it mynginx /bin/sh /root/runoob.sh
//Interactive access to containers
docker run -it ubuntu /bin/bash
docker attach
docker exec //It is recommended that you use the docker exec command, because this exit from the container terminal will not cause the container to stop.

2.9 ps list containers

a: display all containers, including those not running.
-f: filter the displayed content according to conditions.
– format: Specifies the template file for the return value.
-l: displays recently created containers.
-n: list the n recently created containers.
– no TRUNC: do not truncate the output.
-q: silent mode, only the container number is displayed.
-s: displays the total file size. Output details:
CONTAINER ID: CONTAINER ID.
IMAGE: IMAGE used.
COMMAND: the COMMAND that runs when the container is started.
CREATED: the creation time of the container.
STATUS: container STATUS.
There are 7 states:
created
restarting
running
removing (migrating)
paused
exited
dead
PORTS: the port information of the container and the connection type used (tcp\udp).
NAMES: automatically assigned container name.

example:
Lists the information of the five containers that were recently created  docker ps -n 5
 Lists all created containers ID docker ps -a -q

3.10 view the process information running in the container and support ps command parameters.

3.11 logs get the logs of the container

f: trace log output
– since: displays all logs for a certain start time
-t: display timestamp
– tail: only the latest N container logs are listed

example:
docker logs -f mynginx

3.12 port list the port mapping of the specified container, or find the port that will be PRIVATE_PORT NAT to public facing port

docker port mymysql

3.13 network

//Create a new bridge network
docker network create -d bridge test-net
//-d: Parameter specifies the Docker network type, including bridge and overlay.
docker network ls
docker run -itd --name test1 --network test-net ubuntu /bin/bash
docker run -itd --name test2 --network test-net ubuntu /bin/bash
apt-get update
apt install iputils-ping
//Interactive access to containers
docker run -it ubuntu /bin/bash
docker attach
docker exec //It is recommended that you use the docker exec command, because this exit from the container terminal will not cause the container to stop.
//View all containers
docker ps -a
docker start
docker stop
//View network
docker port 
//view log
docker logs
//View process
docker top
docker run
-P :Is the high port that the internal port of the container is randomly mapped to the host.
-p : Is the container's internal port bound to the specified host port.
127.0.0.1:5001:5000
127.0.0.1:5001:5000/tcp
127.0.0.1:5001:5000/udp

New network

//Create a new bridge network
docker network create -d bridge test-net
//-d: Parameter specifies the Docker network type, including bridge and overlay.
docker network ls
docker run -itd --name test1 --network test-net ubuntu /bin/bash
docker run -itd --name test2 --network test-net ubuntu /bin/bash
apt-get update
apt install iputils-ping

Deploy nginx

docker run -d --name nginx -p 83:80  nginx

Deploy redis

docker run -itd --name redis-test -p 6379:6379 redis

Deploy mysql

$ docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

4,Docker Dockerfile

What is Dockerfile?

Dockerfile is a text file used to build an image. The text content contains instructions and instructions required to build an image. Customizing images using dockerfile
Here we only explain how to run the Dockerfile file to customize an image, specifically Dockerfile
Detailed instructions in the document will be introduced in the next section. Here you only need to know the construction process

Posted by AbraCadaver on Wed, 29 Sep 2021 11:29:51 -0700