Summary of Docker configuration container location and tips

Summary of Docker configuration container location and tips

Tips for using Docker

1. Clean up all stopped docker containers

Sometimes we have a lot of stopped containers or containers that cannot be used due to forced exit errors, then we need to delete them, but it is troublesome to delete them one by one. We need to rm as many times as there are containers. We can find out the IDs of all containers according to docker ps -qa and delete them all at once. Don't worry about deleting running containers. rm cannot delete running containers. In this way, we can delete all stopped containers at once.

# Only applicable to Linux environment docker rm $(docker ps -qa)

2. View the environment variables in the image

When we make an image or get an image, if we want to know its environment variables, the first thing we think of is to create a container and see it. In fact, we don’t have to. We can directly view it through env

docker run nginx env 

3. Differences between Windows and Linux

The main reason for the difference between these two environments is the issue of Docker support. We all know that Docker was first released on Linux and depends on the Linux kernel, but it is not available on Windows. So how do we use it now? Virtual machines. If it is a version below Win10, it is basically installed with VirtualBox. Many Win10 users will choose the desktop version and use Hyper-V, which is also a type of virtual machine. Docker can only be used on Windows after a virtual machine is available. Here we should know the two differences.

On Linux, Docker is directly on the Linux system, but it is different on Windows. The Windows system is a virtual machine, and Docker is on the virtual machine.

Linux system<< docker container

win system << virtual machine << docker container

This is the reason why we have slightly different usage. The most common one is when opening the port.

Linux system: Docker container port is directly mapped to Linux system

Windows system: Docker container port is mapped to the virtual machine, and then mapped to our windows by the virtual machine

Note: When selecting the network in our virtual machine, select Network Address Translation (NAT), so that we don’t have to worry about the port mapping problem from the intermediate virtual machine to the Windows layer.

4. Mount

When using Docker, many people like to make images, package the application directly into the image, and start the image directly. Everything is OK, but it is inevitable that we sometimes make some minor modifications, especially configuration files or minor modifications in some projects. At this time, do we still have to make a new image? The answer is no, there is no need

We use nginx image deployment on the front end, but after publishing, we find that there is a style that needs to be fine-tuned. Do we need to re-make an image? Will this be troublesome for us (at least I will find it troublesome). Think about it, we only need to overwrite the files in the container with the modified files. How can we directly overwrite the files in the container with the modified files? There are two ways: first, directly cp the files to the container. In this process, we still have to operate the container. How can we not move the container? Mounting We can mount the things that will be modified later directly to the host when starting the container, so we don't have to move the container. Use the -v parameter to mount the host file or directory to the container when starting

docker run -d -p 80:80 -v /c/Users/SunArmy/Desktop/html:/usr/share/nginx/html nginx 

Let's just write an index.html and write welcome nginx in it

Overwrite this file with index.html in /opt/docker/html/

Revisit

No need to restart, just click OK

Why not choose to cp directly into the container

There is such a situation, if you need to modify the configuration file, it must be restarted, but when modifying it, you accidentally write the wrong configuration file, then your container cannot start up, and you cannot modify the configuration in the container (unless you recreate the container). In other words, if we mount the configuration file outside, when you cannot start it, you can directly modify it to the correct configuration on the host and restart it. In summary, the mounting method is more convenient and safer than the cp method to the container.

1. jq tool

Finally, let's introduce a tool for operating json. This is used when viewing container configuration. We use docker inspect ID to view container configuration, which often filters out some information we are interested in. Generally, we will choose docker inspect --format= or use grep

But no matter which one you use, it is not as good as the json operation method we are most familiar with.

The jq tool needs to be installed separately yum install jq

Use jq to filter docker inspect to get the address

docker inspect ac |jq -r .[0].NetworkSettings.IPAddress

. represents the output in front of the pipeline, and the [0] after that is the first element of the array. The . after that is similar to pointing out the attributes in the object, which is the same as when we use json normally. jq can also conveniently format and view json files, which is a great tool for viewing json files in the command line.

2. Modify the storage location of Docker local images and containers

Many people only know that a container has been created, but have never paid attention to where the created container is. Where is the local mirror?

However, when you have to pay attention, it is often because your Docker images and containers fill up your disk.

View where Docker images and containers are stored

docker info | greo Docker

The default location is: /var/lib/docker

There are two ways to modify the storage location:

1): Move /var/lib/docker to another place by establishing a soft link, and establish a soft link to here

# Stop Docker
 service docker stop
# Move /var/lib/docker to /usr/local/
 mv /var/lib/docker /usr/local/
# Create a soft connection ln -s /usr/local/docker /var/lib/docker
# Start Docker
 service docker start

At this time, we have modified it, but when we check the location, we still see /var/lib/docker

But this is a link. You can check the size and find that it is empty. The actual storage location has become /usr/local/docker

# View the size of the /var/lib/docker directory du -dh /var/lib/docker

2): Modify the configuration file

The default configuration file is /etc/docker/daemon.json

If not, create your own

{
 "registry-mirrors": ["http://hub-mirror.c.163.com"],
 "graph":"/opt/docker"
}

Just modify the graph value to your location and restart, OK

start up

service docker stop

stop

service docker start

Restart

service docker restart

Check the storage location of docker again, it has been modified successfully

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Docker configuration Alibaba Cloud Container Service operation
  • Detailed explanation of Docker container network port configuration process
  • Detailed explanation of common commands for network configuration of containers in Docker
  • Detailed explanation of Docker fast build and Alibaba Cloud container acceleration configuration under Windows 7 environment
  • Implementation of modifying configuration files in Docker container
  • nginx automatically generates configuration files in docker container
  • Docker modifies the configuration information of an unstarted container

<<:  Detailed explanation of filters and directives in Vue

>>:  How to set password for mysql version 5.6 on mac

Recommend

How to install and configure MySQL and change the root password

1. Installation apt-get install mysql-server requ...

How to disable ads in the terminal welcome message in Ubuntu Server

If you are using the latest Ubuntu Server version...

In-depth explanation of modes and environment variables in Vue CLI

Preface In the development of actual projects, we...

Detailed steps to install mysql5.7.18 on Mac

1. Tools We need two tools now: MySQL server (mys...

How to use Greek letters in HTML pages

Greek letters are a very commonly used series of ...

Centos7.5 configuration java environment installation tomcat explanation

Tomcat is a web server software based on Java lan...

A brief talk about MySQL semi-synchronous replication

Introduction MySQL achieves high availability of ...

Detailed explanation of the use of this.$set in Vue

Table of contents Use of this.$set in Vue use Why...

Native js custom right-click menu

This article example shares the specific code of ...

Summary of Css methods for clearing floats

Float is often used in web page layout, but the f...

How to calculate the value of ken_len in MySQL query plan

The meaning of key_len In MySQL, you can use expl...

How to configure tomcat server for eclipse and IDEA

tomcat server configuration When everyone is lear...

Command to remove (delete) symbolic link in Linux

You may sometimes need to create or delete symbol...

Detailed explanation of using echarts map in angular

Table of contents Initialization of echart app-ba...

How to deploy nextcloud network disk using docker

NextCloud You can share any files or folders on y...