Docker was briefly introduced above, and the use of docker is recorded here.

Docker start and stop

1. Start Docker

systemctl start docker
Copy the code

2. Close Docker

systemctl stop docker
Copy the code

3. Restart docker

systemctl restart docker
Copy the code

Second, run the Docker application

Docker run --name Container name --rm it -p [IP :] Host port: container port mirroring name [command]Copy the code

Run: If there is a local image, run it directly. If there is no local image, obtain it from the image repository. The default is docker Hub.

More commands can be viewed using docker run –help.

Analysis of some common parameters:

1. Specify the container name. If default, the container name will be randomly assigned

Name - the name containerCopy the code

2. Start the interaction

I indicates interactive, and T indicates terminal -itCopy the code

3. Run container in the background

-d
Copy the code

4. Specify the network mode

--network: specifies the network to which containers connected to the same network can be interconnectedCopy the code

To connect multiple applications to form an APP, you need to set the network mode to Container

Docker run --network=container: id container name [:tag]Copy the code

5. Configure DNS

--dns=ip
Copy the code

Such as:

Docker run - DNS = 4.4.4.4Copy the code

Configure the container’s search domain

--dns-search=DOMAIN
Copy the code

When the search domain is set to.example.com, when searching for a host named host, DNS searches not only for host but also host.example.com.

6. Set the host name of the container

- h the hostname or - h = the hostnameCopy the code

7. Specify the mapping host port

-p [IP :] Host port: indicates the internal port of a containerCopy the code

For example, map port 80 inside the container to port 8080 on the host

Docker run -p 8080:80Copy the code

You can also use capital P

-P
Copy the code

This is a random mapping of host ports

8. After the container is stopped, the container is automatically deleted

--rm
Copy the code

9. Set environment variables

-e key1=value1
Copy the code

The environment configuration can also be read from the specified file

--env-file=[]
Copy the code

10. Set the user to run

-u usernameCopy the code

If you have the user in the container

11. Set the maximum memory that the container can use

-m Specifies the memory sizeCopy the code

12. Set the working directory

- w directoryCopy the code

13. Bind the data volume

-v [Host directory :] Container directoryCopy the code

If there is only one directory, the directory in the specified container will be mapped randomly to the host directory. The files in the host directory and the container directory are common, meaning that whenever either side adds or changes a file, the other side senses it.

You can also reuse volume Settings from other containers

-volumes-from Specifies the name or ID of another containerCopy the code

If you do not bind data volumes, data will be deleted when the container dies when it is closed. Therefore, you need to bind data volumes to save data.

Three, mirror use

1. View an existing local image

docker images
Copy the code

2. Find the image from docker Hub

Example: docker search nginx docker search nginx:1.19.1Copy the code

For example, find nginx

OFFICIAL indicates whether it is provided by an OFFICIAL, as there are many images provided by third parties on docker Hub.

3. Pull the mirror

Docker pull image name [: version (tag)]Copy the code

For example, if a mysql database is not tagged, the latest image is pulled by default

Once pulled down, you can see the mysql image locally

4. Delete the mirror

Docker rmI image name [: version (tag)]Copy the code

For example, delete the mirror whose name is A and tag is latest

5. Set the image label

Docker tag Image ID or original image name: original tag Image name :tag Example: Docker tag 0d64f46ACfd1 first_mysql:first docker tag mysql:latest first_mysql:firstCopy the code

Setting a label will add an image. The existing image will not be deleted or changed

6. Create an image

There are two ways:

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

Note: the first kind of command is introduced here. The second kind of Dockerfile is described separately

Start an Nginx that maps to port 8080 on the host machine. Then visit the nginx home page. The default is as follows

Docker run –name nginx -p 8080:80 -d nginx

I need to go into the container and change it to another page and save it as a mirror.

Docker exec -it nginx /bin/bash

Create the lsy directory in the root directory and create the lsy.html file

(3) Modify the nginx.conf file

The vim tool is not available for the image by default. Therefore, you need to run the following two commands

Install vim apt-get install vim apt-get install vimCopy the code

1. Modify the configuration file and set the home page to custom lsy.html.

2. Submit the container

Docker commit -m=" Description "-a=" mirror author" Container ID The name of the target image to be createdCopy the code

3. Start the new container and visit the home page

Docker run it -p 8080:80 -d --name my_nginx my_own_nginx:v1.0Copy the code

Iv. Use of containers

1. Look at the container

(1) In operation

docker ps
Copy the code

(2) All running or running containers

docker ps -a
Copy the code

(3) The container of the last run

docker ps -l
Copy the code

2. Start and stop the container

The container ID and container name are as follows: docker ps -a

(1) Start the running container (that is, docker ps-a obtained)

Docker start Container ID or container nameCopy the code

(2) Stop the running container

Docker Stop container ID or container name docker kill container ID or container nameCopy the code

(3) Restart the container

Docker restart Container ID or container nameCopy the code

3. Enter the container in background operation

Docker exec -it 243c32535da7 /bin/bash docker exec -it /bin/bash docker exec -itCopy the code

4. Export and import containers

(1) Export

Docker export Container ID > export file name.tarCopy the code

(2) Import

Docker import Image name: versionCopy the code

5. Delete the container

Docker rm -f Container ID or container nameCopy the code

Delete all containers that have been stopped

docker container prune
Copy the code

6. View the container mapping host port

Docker port Container ID or container nameCopy the code

7. View the container logs

Docker logs Container ID or container nameCopy the code

8. Check the container process ID

Docker Top Container ID or container nameCopy the code

9. View all container information

Docker inspect Container ID or container nameCopy the code

Create a Docker network

Docker network create -d Docker network type Network nameCopy the code

-d: Specifies the Docker network type

Network Mode Configuration Description host-net =host The container shares a Network namespace with the host. Container -net = Container: indicates the name or ID of a container. A container shares a Network namespace with another container. A POD in Kubernetes is a Network namespace shared by multiple containers. None — net= None The container has a separate Network namespace, but does not perform any Network Settings on it, such as assigning Veth pairs and Bridges, configuring IP addresses, etc. Bridge — net=bridge (default mode)

11. Configure DNS

You can add the following content to the /etc/dock/daemon. json file on the host to set DNS for all containers

{" DNS ": ["114.114.114.114", "8.8.8.8"]}Copy the code

After this setting, the DNS of the startup container is automatically configured to 114.114.114.114 and 8.8.8.8. You need to restart the Docker for the configuration to take effect

Check whether DNS takes effect:

Docker run -it Image name [:tag] cat etc/resolv.confCopy the code

12, real-time view docker host CPU and memory information

docker stats
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

I’m Liusy, a programmer who likes to work out.

For more dry goods and the latest news, please follow the official account: Ancient False God

If it is helpful to you, a point of attention is my biggest support!!