This is the first day of my participation in the More text Challenge. For details, see more text Challenge
Docker setup and basic commands
Install Docker on Centos8
Docker version requirements
1. The kernel version of the CentOs must be later than 3.10
uname -r
Copy the code
Update the yum!
To install Docker CE, which is the community free version, first install the necessary packages and install yum-utils, which provides a yum-config-Manager unit, Device-mapper-persistent-data and LVM2 are installed together to store the two software packages required for devicemapper.
sudo yum update
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code
This is followed by the configuration of a stable repository, which is saved in /etc/yum.repos.d/docker-ce.repo. Here we use Aliyun.
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code
Update the Docke packages installed with Yum
Sudo yum makecache fast#Copy the code
The following error occurs:
Yum Makecache: error: argument timer: invalid choice: ‘fast’ (choose from ‘timer’)
An error occurred during index update, that is, centos8 does not have this parameter. The solution is to remove the fast parameter
View the repository version and specify the version to install
Check the version
yum list docker-ce --showduplicates | sort -r
Copy the code
The installation
Yum install docker-ce#Copy the code
Error: Problem: Package Docker-CE-3:18.09.9-3.el7.x86_64 requires Containerd. IO >= 1.2.2-3, but none of the providers can be installed
If the version of containerd. IO is >= 1.2.2-3, perform the following operations
This error is also possible
Yum erase podman buildah
(1) Install the WGET instruction
yum install wget
Copy the code
(2) Obtain the RPM package
wget http://docker-release-purple-prod.s3-website-us-east-1.amazonaws.com/linux/centos/8/x86_64/edge/Packages/containerd.io-1 . 3.7 to 3.1. El8. X86_64. RPMCopy the code
(3) Upgrade containerd. IO by installing the RPM package.
Yum -y install containerd. IO - 1.2.6-3.3 el7. X86_64. RPMCopy the code
(4) Reinstall Docker-CE
yum install docker-ce
Copy the code
Start the docker
(1) Set automatic startup upon startup
sudo systemctl start docker
sudo systemctl enable docker
Copy the code
(2) Start
sudo systemctl start docker
Copy the code
(3) Verification
docker version
Copy the code
The installation is successful.
or
sudo docker ps
Copy the code
The installation is successful.
(4) Check the running status of Docker
systemctl status docker
Copy the code
Common Docker commands
1. Pull the mirror
docker pull ubuntu
Copy the code
2. Start the image for the first time
docker run -it --name ubuntu-test ubuntu /bin/bash
Copy the code
Parameter description:
- -i: indicates interactive operations.
- – t: terminal.
- P: port.
- – IP: specifies the IP address.
- -v: indicates a data volume. For example, -v /data:/data:ro. Ro indicates that the container is read-only.
- –volumes-from: Data volumes come from an existing container.
- -name: indicates the container name.
- Ubuntu: Ubuntu image.
- /bin/bash: After the image name is the command, here we want an interactive Shell, so we use /bin/bash.
3. Start the existing containers
Docker restart docker restart docker id1 [docker ID2] [... # to restartCopy the code
4. List of running Docker containers
docker ps
Copy the code
Docker ps - aCopy the code
Stop the container
Docker stop < container ID/Name>Copy the code
6. Enter the container
docker exec -it name /bin/bash
Copy the code
Exiting from this container does not cause the container to stop. This is recommended
Check the PID of some processes in the container
doker top name
Copy the code
8. Delete the container
docker rm name
Copy the code
9. Mirror list
docker images
Copy the code
10. Start all containers
docker start $(docker ps -a -q)
Copy the code
Copy the file to the container
docker cp sentinel.conf redis-master:/usr/local/redis
Copy the code
12. Specify the IP address when creating the container
When creating containers, Docker uses the bridge network by default and assigns its own IP address. It is not allowed to specify its own IP address.
In actual deployment, we need to specify the IP address of the container. It is not allowed to assign an IP address by itself, especially when setting up a cluster, fixed IP address is necessary.
We can create our own bridge network: mynet. When creating the container, specify the network as Mynet and specify the IP address.
Viewing network Mode
docker network ls
Copy the code
Create a new Bridge network
Docker network create --driver Bridge --subnet=172.19.0.0/16 --gateway=172.19.0.1 dockercomposeCopy the code
Viewing Network Information
docker network inspect dockercompose
Copy the code
Create the container and specify the container IP
Docker run-it --name sentinel1 --network=dockercompose -- IP 172.19.0.6 -v /data/docker_redis/sentinel/sentinel-slave1.conf:/usr/local/redis/sentinel.conf -d -p 26380:26379 redis /bin/bashCopy the code
13. Create a snapshot
View existing containers
docker ps/docker ps -a
Copy the code
Creating a Container Snapshot
docker export ID > test.tar
Copy the code
Import and verify
Cat snapshots. Tar | docker import - centos: the latestCopy the code
Docker run-it ID(image) /bin/bashCopy the code
14. View information about the data volume
Sdocker inspect -f {{.volumes}} Name of the containerCopy the code
Building a container (nginx as an example)
First, build manually
1. Pull the mirror
docker pull centos
Copy the code
2. Start the image
docker run --name nginx-man -it centos
Copy the code
3. Install dependency packages
yum install -y wget gcc gcc-c++ make openssl-devel
Copy the code
Install the Nginx dependencies
Mv nginx wget http://nginx.org/download/nginx-1.9.3.tar.gz - 1.9.3. Tar. Gz/usr/local/data/nginx - 1.9.3. Tar. Gz CD /usr/local/dataCopy the code
5, decompression
The tar - ZXF nginx - 1.9.3. Tar. GzCopy the code
Create a WWW user
useradd -s /sbin/nologin -M www
Copy the code
Start nginx
./configure --prefix=/usr/local/data/nginx --user=www --group=www --with-http_ssl_moudle --with-http_stub_status_module - with - pcre = / usr/local/SRC/pcre - 8.37Copy the code
8 the make.
make
make install
Copy the code
9. Configure the startup container to run nginx directly
vi /usr/local/data/nginx/conf/nginx.conf
#Add, make it run in the foreground
daemon off;
Copy the code
10, validation,
exit#Launch containerDocker commit -m "my nginx" container ID my-nginx1:vl doker images#A new mirror has been found
docker run -it -p 92:80 my-nginx1:v1 /usr/local/data/nginx/sbin/nginx
#You can loop through adding as many containers as you wantOpen IP:92 in your browser to see nginxCopy the code
11. The container opens itself
Docker Container update --restart= Always container nameCopy the code
2. Automatic Dockerfile build
- Basic Mirror Information
- Maintainer information
- Mirror Operation Command
- Instructions are executed when the container starts
Create a file
mkdir /opt/docker-file/nginx cd /opt/docker-file/nginx pwd wget Wget HTTP: / / http://nginx.org/download/nginx-1.9.3.tar.gz ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz vim DockerfileCopy the code
The contents are as follows:
#This is My first Dockerfile
Image # base images
From centos
# MAINTTAINER maintainers
MAINTAINER WangScaler
#ADD to the container
ADDPcre 8.37. Tar. Gz/usr/lcoal/SRC
ADDNginx - 1.9.3. Tar. Gz/usr /local/src
#RUN RUN command
RUN yum install wget gcc gcc-c++ make openssl-devel
RUN useradd -s /sbin/nologin -M www
#WORKDIR
WORKDIR /usr/local/ SRC/nginx - 1.9.3
RUN./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_moudle --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.37
RUN echo "daemon off;">>/usr/local/src/nginx/conf/nginx.conf
EXPOSE Mapping port
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
Copy the code
Note: ADD files must be in the same folder as Dockerfile
run
docker build -t nginx-file:v1 /opt/dockerfile/nginx/
Copy the code
Docker image to view
Create a Python container using Dockerfile
The dependencies you need to write your application
The contents of requirements. TXT are as follows
vim requirements.txt
Copy the code
WTForms==2.21.
WTForms-JSON==0.33.
wtforms-tornado==0.02.
PyJWT==1.71.
redis==3.51.
motor==2.1. 0
tornado==6.03.
tornado-swagger==1.24.
pytz==2020.1
paho-mqtt==1.5. 0
requests==2.22. 0
Copy the code
Write Dockerfile
vim Dockerfile
Copy the code
#This is My first Dockerfile
#Mirror base imagesThe FROM python: 3.7.4#MAINTTAINER maintainers
MAINTAINER WangScaler
#ADD something to a container
ADD requirements.txt /usr/local/python-app/requirements.txt
#RUN Execution instruction
WORKDIR /usr/local/python-app
RUN pip install -r requirements.txt
#EXPOSE mapping port
EXPOSE 8090
Copy the code
Create a mirror image
docker build -t python-apps .
Copy the code
Viewing network Mode
docker network ls
Copy the code
Create a new Bridge network
Docker network create --driver Bridge --subnet=172.19.0.0/16 --gateway=172.19.0.1 dockercomposeCopy the code
Viewing Network Information
docker network inspect dockercompose
Copy the code
Create the container and specify the container IP
Docker run-it --name python-app --network=dockercompose -- IP 172.19.0.2 -c /usr/local/python-app:/usr/local/python-app -d -p 8090:8090 python-apps /bin/bashCopy the code
Into the container
docker exec -it python-app bash
Copy the code
Create python project image in Dockerfile
Write Dockerfile
vim Dockerfile
Copy the code
#Base image based onThe FROM python: 3.6.8#Maintainer information
MAINTAINER [email protected]
#Add the code to the code folder
ADD / /ssm
#Set the code folder to be the working directory
WORKDIR /ssm
#Installation supportRUN cd /ssm && ls RUN ls RUN pip install --user -r /ziot/requirements.txt -i https://pypi.doubanio.com/simple/ RUN python CreateLicense.py 02:42:ac:22:00:02 RUN python setup.py RUN python -m compileall -b build/ RUN find . -name "*.py" -type f -print -exec rm -rf {} \; CMD ["python", "build/server.pyc"]Copy the code
Each instruction creates one layer:
FROM
creates a layer from theUbuntu: 18.04
Docker image.COPY
Adds files from your Docker client’s current directory.RUN
builds your application withmake
.CMD
specifies what command to run within the container.