best practices
config default data directory
default data directory
/var/lib/docker
change default directory
1
2
3
4
5
6
7
8
9
10
11
12step1
vim /etc/docker/daemon.json
{
"data-root": "/path/to/directory"
}
step2
systemctl restart docker
check modify available
docker info | grep "Docker Root Dir"
non-root user manage docker
Create the
docker
group.1
sudo groupadd docker
Add your user to the
docker
group.1
sudo usermod -aG docker $USER
Log out and log back in so that your group membership is re-evaluated.
If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.
On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.
On Linux, you can also run the following command to activate the changes to groups:
1
$ newgrp docker
Verify that you can run
docker
commands withoutsudo
.1
$ docker run hello-world
migration
reference
5 ways to move Docker container to another host
Build a Docker Image with MySQL Database
Plan A
- Step1 create an Image From a Container
Create a new image from a container’s changes
1 | sudo docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] |
options
Name, shorthand | Default | Description |
---|---|---|
--author , -a |
Author (e.g., “will brook”) | |
--change , -c |
Apply Dockerfile instruction to the created image | |
--message , -m |
Commit message | |
--pause , -p |
true |
Pause container during commit |
- Step 2 export the image to a file
1 | sudo docker save -o /path/to/your_image.tar your_image_name |
- Step 3 load the Docker image file
1 | sudo docker load -i your_image.tar |
Plan B
- Step 1
First save the new image by finding the container ID (using docker container ls
) and then committing it to a new image name. Note that only a-z0-9-_.
are allowed when naming images:
1 | create image from container |
- Step 2
tag the image with the host name or IP address, and the port of the registry:
1 | re-tag repository:tag info about image |
- Step 3
log in from Docker client:
1 | docker login <harbor_address> |
- Step 4
push the image to the registry using the image ID.
In this example the registry is on host named registry-host
and listening on port 5000
. (harbor默认配置端口80,详见harbor.yml)
1 | push repository:tag, |
Pull Image from Harbor
- Step 1
add the option --insecure-registry
to your client’s Docker daemon. By default, the daemon file is located at /etc/docker/daemon.json
.
1 | { |
Restart Docker Engine.
1 | systemctl restart docker |
- Step 2
1 | docker pull hostAddress/library/REPOSITORY:TAG |
troubleshoot
- Failed to load listeners: no sockets found via socket activation: make sure the service was started by systemd
1 | edit the docker’s systemd unit file and change the ExecStart: |
- Error creating default “bridge” network: cannot create network (docker0): conflicts with network (docker0): networks have same bridge name
1 | sudo rm -rf /var/lib/docker/network |
- accident delete /var/run/docker.sock
1 | sudo systemctl stop docker |