docker command and operation notes
Choose a linux machine, or choose an ubuntu ec2 machine on aws cloud, and install MobaXterm or other interface tools to connect to remote linux machines on the local computer. Then execute the following command to get familiar with docker.
1. Update the source of ubuntu
sudo apt-get update
2. Install the necessary packages
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
3. Then download
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4. Then download
sudo add-apt-repository \
"deb [arch=amd64]" https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
5. Then execute
sudo apt-get update
6. Then execute the following command to install docker-ce, because docker-ce is a free version
sudo apt-get install docker-ce
7. Then download and start an image
docker pull hello-world or docker image pull hello-world (this hello-world image is an image on dockerhub)
sudo docker run hello-world
sudo groupadd docker (groupadd is to add the current user to the docker group)
8. Install nginx image
sudo docker image pull nginx:latest
9. View the image docker images downloaded now
10. Enter the root directory, so that each command is executed without adding the sudo prefix
sudo su -
11. Use conainer to start image
docker container run -itd --name web-server-nginx -p 8080:80 nginx:latest (where web-server-nginx is the name of the container you want to create and start this time, 8080 is the port of the host machine, 80 is the port of the container container, the browser access is through the 8080 port of the host machine, nginx:latest is the mirror image used to start this container) After executing the above command, a container id will be returned
, for 9b2daf782f1313985797588784ef03d323cf7aee8186820a07d975ee8cac3154
12. View all containers
docker ps -a
13. The three stages of docker
(1) use dockerfile to package build
(2) use docker image to transfer ship
(3) use docker run to run the image, that is, container run
14. The two most commonly used commands on the docker ci side are
docker run
docker pull
15. Create a new folder + Dockerfile
Open the Dockerfile with nano or vm,
add content
ARG CODE_VERSION=16.04
FROM ubuntu:${CODE_VERSION}
RUN apt-get update -y
CMD ["bash"]
16. Execute the above file to create an image
docker build -t img_from of all codes in the current directory. (img_from in this command is the name of the packaged image, and the dot at the end means all the files in the current directory where the dockerfile is located. That is to pack all the files in this directory into image)
17. Return the id of the image whose name is imageName
docker run -itd --name cont_run-env imageName (run the image whose name is imageName, and name the container running the image this time as cont_run-env)
18. docker image分层
cmd layer
expose layer
workdir layer
run layer
base image layer
bootfs
19. docker search python:3.6
You can see all the information about python 3.6 on dockerhub, including the release date, etc.
20. docker search --format "table { {.Name}}\t{ {.Description}}\t{ {.IsOfficial}}" registry
Filter queries with --format or filter
21. docker login
docker tag nginx:latest cerule/repo-nginx:cc-nginx (where cerule/repo-nginx is the repository name, cc-nginx is the name of the tag) this command should list the image of nginx:
latest , copy and paste a copy, and change the label to cerule/repo-nginx:cc-nginx
22. docker image push cerule/repo-nginx:cc-nginx
will be labeled as cerule/repo-nginx:cc-nginx image, push to github
23. View history or details history or inspect
docker image history cerule/repo-nginx:cc-nginx
docker image inspect --format "{
{json .Config}}" ubuntu > inspect_ubuntu.txt
docker image inspect ubuntu:latest
24. 删除image
docker image rm nginx:1-alphine-perl
docker image rmi 3666666(image id)
25. Create container
docker container create -it --name cc-busya busybox:latest (where busybox:latest is the image name, cc-busya is the name of the container)
Then execute the following sentence: run will be executed first, and then once the container is stopped, it will be automatically deleted
docker container run -itd --rm --name cc-busyb busybox:latest
26. docker container start cc-busya
docker run = docker create + docker start
docker container stop cc-busyb
docker container restart --time 5 cc-busyboxa (restart this container after 5 seconds)
docker container rename cc-busyboxa my-busybox( will not cause a restart or retime)
docker container attach my-busybox will cause it to stop.
The function of docker container attach is to enter the container to see, such as checking logs, performing some operations, etc.
/ #Enter
another window
exit
exit this window
docker exec -it my-busybox pwd
to get the result /, which is the root directory of this container, docker ps -a, you can find that this container is up
27. map port port
docker container run -itd --name cont_nginx -p 8080:80/tcp img_expose
docker container run -itd --name cont_nginxa -P img_expose
the same image, start two containers, the browser can pass two ports to access the same resource
28. Delete container
docker container rm cont_from
docker container rm 68xxxx 087xxx 915xxx (stopped container id)
29. docker container prune (delete all containers in stop state)
30. docker network drivers host, bridge, overlay (swarm or kubenetes) used to communicate between containers
31. docker network create --driver bridge my-bridge
返回id
docker network create --driver bridge --subnet=192.168.0.0/16 --ip-range=192.168.5.0/24 mybridge1
docker network ls
docker network ls --filter driver=bridge
docker start starts a container, myubuntu
and then executes docker network connect mybridge1 myubuntu
docker container inspect myubuntu
docker container run -itd --network host --name cont_nginx nginx:latest (where nginx:latest is the image name, cont_nginx is the container name)
docker container port cont_nginx
docker network inspect bridge
docker network disconnect mybridge myubunu
32. docker volume permanent storage
docker volume create vol-busybox
This command returns the name vol-busybox
docker run -d --volume vol-ubuntu:/tmp ubuntu will also create a volume, the name of the volume created by this command is vol-ubuntu , save the data to the /tmp path of the host machine, the startup image is ubuntu,
so you can view all volumes with docker volume ls
docker volume ls --filter "dangling=true"
to filter out volumes that are not mounted to any container
docker volume inspect vol-ubuntu
to view the details of this volume
docker volume rm vol-ubuntu
delete, if it is on the container on mout, refuse to delete
docker container rm dockernamess
33. Download docker compose
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr /local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker compose version Check if the installation is successful
34. The configuration version in the docker-compose.yaml file
: 3.3
services:
db:
image: mysql:5.7 (the name of the mirror image of the downloaded database)
container_name: mysql_database (the name of the container container)
volumes:
-db_data:/var/ lib/mysql (where db_data is the name of the volume, /var/lib/mysql is the path of the host machine)
restart: always (when the container hangs up, select always to automatically restart)
environment:
MYSQL_ROOT_PASSWORD: wordxxx
MYSQL_DATABASE: wxxx
MYSQL_USER: usxxx
MYSQL_PASSWORD: xxxx
mywebxx:
depends_on:
- db
image: mywebxx:latest
container_name: conxxx
volumes:
- mywebxx_files:/var/www/html (where mywebxx_files is the name of the volume, /var/www/html is the storage location on the host machine) ports: - "
8080
:80"
restart: always
environment:
MYWEBXX_DB_HOST: db:3306
MYWEBXX_DB_USER : userxx
MYWEBXX_DB_PASSWORD: xxx
volumes:
mywebxxx_files:
db_data:
Original link: https://blog.csdn.net/daiqinge/article/details/103144869