This list is constantly being updated, last updated on 2019-2-7
Execute the command
Sometimes you need to execute commands in a container, or simply start a bash – a one-size-fits-all tool to get things done.
The commands are described below by Docker and Docker Compose, respectively.
Docker
First of all, you need to know the name of the container, if you don't know, you can run the 'docker ps' command to list all the running containers and get the container name from it.
Here we use the 'docker exec' command to execute the command and cooperate '-i' interaction and '-t' terminal option parameter to open the terminal and maintain interaction.
$ docker exec -i -t container_name /bin/bash
$ docker exec -i -t container_name /bin/bash
The '-i' and '-t' option arguments can be combined into '-it', and multiple commands can be executed directly.
$ docker exec -it container_name sh -c "echo a && echo b"
$ docker exec -it container_name sh -c "echo a && echo b"
Docker Compose
If you use Docker Compose, whether it is the 'run' command or the 'exec' command, it will automatically assign a terminal. Therefore, it is only necessary to simply execute the following command.
$ docker-compose exec container_name /bin/bash
$ docker-compose exec container_name /bin/bash
The same method can be used to execute commands directly.
$ docker-compose exec container_name command
$ docker-compose exec container_name command
But if you need to execute a series of commands, you need to use the 'sh' command to connect them.
$ docker-compose exec container_name sh -c 'echo a && echo b'
$ docker-compose exec container_name sh -c 'echo a && echo b'
Go to the command line of Docker for Windows
When using the Windows version of Docker, you actually run a customized Linux on Hyper-V, which uses the LinuxKit distribution. However, since this Linux is not a complete distribution, the functionality is limited, and in fact, there is rarely a need to enter directly. But if you really need this, just use the following command.
docker run -it --rm --privileged --pid=host justincormack/nsenter1
docker run -it --rm --privileged --pid=host justincormack/nsenter1
Reference
Transfer images from one host to another
Docker Hub
This is a public Docker image distribution center, but it also provides paid enterprise version image functionality, which can be disclosed.
After executing the 'docker login', you can use the following command to push the image to Docker Hub
$ docker publish
$ docker publish
Self-built image distribution center
We start with the following command Private , a self-built image distribution center
$ docker run -d -p 5000:5000 --name registry registry:2
$ docker run -d -p 5000:5000 --name registry registry:2
Use the following command to tag and link the image to the self-created image distribution center:
$ docker image tag myimage (ip-address):5000/myfirstimage
$ docker image tag myimage (ip-address):5000/myfirstimage
On the first host, use the following command to push the image:
$ docker push (ip-address):5000/myfirstimage
$ docker push (ip-address):5000/myfirstimage
On another host, use the following command to pull the image off.
$ docker pull (ip-address):5000/myfirstimage
$ docker pull (ip-address):5000/myfirstimage
There may be a hint of certificate problems during use, so please refer to the following reference URL.
Reference
Transfer via file
First, use the following command on the source host to store the image as a tar file:
$ docker save -o <save image to path> <image name>
$ docker save -o <save image to path> <image name>
Next, copy the tar file to the target host, which may use the 'cp' or 'scp' command. After the copy is complete, run the following command on the target host to restore the image on the host:
$ docker load -i <path to image tar file>
$ docker load -i <path to image tar file>
If two hosts can connect using 'ssh', perhaps the following quick method will be more convenient, Complete the whole process of packing, compressing, transferring and releasing the loading in sequence.
$ docker save <image> | bzip2 | \
ssh user@host 'bunzip2 | docker load'
$ docker save <image> | bzip2 | \
ssh user@host 'bunzip2 | docker load'
If you are using a system that contains the 'pv' command, you can also put it in it to monitor the entire process.
$ docker save <image> | bzip2 | pv | \
ssh user@host 'bunzip2 | docker load'
$ docker save <image> | bzip2 | pv | \
ssh user@host 'bunzip2 | docker load'
Reference
Clear up space
After Docker is used for a long time, it is easy to have the problem of taking up too much space, so clean up in the following different ways
Delete images that are no longer in use
In general, if you pull a lot of images, especially if you have compiled images locally multiple times, You can use the following command to delete these unused images.
$ docker rmi $(docker images -q --filter "dangling=true")
$ docker rmi $(docker images -q --filter "dangling=true")
Delete the stopped container
Containers that have been stopped without intending to retain their state can also be deleted.
$ docker rm `docker ps --no-trunc -aq`
$ docker rm `docker ps --no-trunc -aq`
Reference
- https://gist.github.com/ngpestelos/4fc2e31e19f86b9cf10b
- http://stackoverflow.com/questions/17236796/how-to-remove-old-docker-containers
Proxy on ubuntu
For 'Ubuntu 16.04 LTS' systems, the built-in 'systemd' can be used, as follows:
- Create a directory:
$ mkdir /etc/systemd/system/docker.service.d
$ mkdir /etc/systemd/system/docker.service.d
- Create the '/etc/systemd/system/docker.service.d/http-proxy.conf' file with the following contents:
[Service]
Environment="HTTP_PROXY=https://web-proxy.corp.xxxxxx.com:8080/"
Environment="HTTPS_PROXY=https://web-proxy.corp.xxxxxx.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,localaddress,.localdomain.com"
[Service]
Environment="HTTP_PROXY=https://web-proxy.corp.xxxxxx.com:8080/"
Environment="HTTPS_PROXY=https://web-proxy.corp.xxxxxx.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,localaddress,.localdomain.com"
- Application changes:
$ systemctl daemon-reload
$ systemctl daemon-reload
- Restart docker:
$ systemctl restart docker
$ systemctl restart docker