# docker
1. Introduction
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, which can then be distributed to any popular Linux machine, and can also be virtualized. Containers are completely sandboxed and do not have any interface with each other.
2. Installation
# cents 7 yum install -y docker #start up /etc/init.d/docker start
3. Daily operation
#a, download mirror docker pull centos #b, view the local mirror docker images # Search for docker images from the docker repository docker search centos # start a container docker run -it centos /bin/bash #Exit the container, close the container exit # Exit the container without closing Ctrl+d # View running containers, -a stopped containers are also displayed docker ps -a # stop easy docker stop container id # log in to a container docker exec -it containerid /bin/bash # delete a container docker rm container id # save a container image docker commit -m "description information" -a 'author information' container id gary234:latest # Create a container to start directly docker run -itd centos bash Download mirror address http://download.openvz.org/template/precreated/centos-6-x86_64.tar.gz
Fourth, disk data management
-- Pay special attention to turn off the selinux permission, otherwise there is no permission to modify the directory,
Start a container and mount the machine's directory to a directory in the Docker container
docker run -itd -v /data/:/data/ centos bash
When starting a container, share the directory of the previous docker container to realize data sharing between two docker containers
docker run -itd --volumes-from big_shirley centos bash # big_shirley container name, last field # centos image name
5. Mapping the object machine port to the docker container
docker run -p 5123:80 -itd gary_centos bash # Multiport mapping docker run -itd -p 123-155:123:155 gary_centos bash
6. Backup containers and images
backup image
docker save -o mysql.tar mysql # mysql.tar saved file # mysql image name
export container
docker export 7e8a0dc1cb88 > mysql.tar
restore image
cat mysql.tar | docker import - mysql:latest
Seven, docker bridge network (bridging operation will disconnect the temporary port network, it is recommended to write a script to run or operate in a virtual machine)
7.1 cents 7
# stop docker server systemctl stop docker # Use ifconfig to see if docker has a network card, if there is a network card, delete it first ip link set dev docker0 down brctl delbr docker0 ## brctl to install # Create a new bridge virtual network card br0 brctl addbr br0 ip link set dev br0 up ip addr add 192.168.1.210/24 dev br0 #Assign the ip address of the host to br0 ip addr del 192.168.184.123/24 dev ens0 #Clear the Ip address of the host brctl addif br0 ens0 # Attach the host network card to br0 ip route add default via 192.168.1.1 dev br0 #Set routing for br0 # Modify the docker configuration file, Centos7 is in /etc/sysconfig/docker, other servers go to this URL to find https://docs.docker.com/installation/#installation vim /etc/sysconfig/docker #Modify the OPTIONS='--selinux-enabled' line to OPTIONS='--selinux-enabled -b=br0', that is, use the br0 network card for bridge when the docker service starts #start docker systemctl start docker # Download the pipeline command for bridging git clone https://github.com/jpetazzo/pipework cp pipework/pipework /usr/local/bin/ # Start a docker container with no network docker run -itd --net=none --name=gar123 centos bash # Bridge 192.168.1.211 container IP, 192.168.1.210 host IP pipework br0 gar123 192.168.1.211/[email protected] ### At this point, the container bridge is completed, and the docker container can be directly accessed through other machines # install sshd service ## install software yum -y install openssh-server ## Generate the key file needed to start the sshd server ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_ecdsa_key ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_ed25519_key ## start sshd server /usr/sbin/sshd
Important: dockerfile creates redis container
############## # DockerFile ############## from centos MAINTAINER gary [email protected] RUN yum install -y gcc make openssl-devel automake make ADD redis /opt/soft/redis WORKDIR /opt/soft/redis RUN tar xvzf redis-3.2.8.tar.gz WORKDIR redis-3.2.8 # install redis RUN make #RUN make PREFIX=/usr/local/redis install RUN make install RUN cp redis.conf /etc/ RUN sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf EXPOSE 6379 ENTRYPOINT ["redis-server"] #CMD "/usr/local/redis/bin/redis-server" "/etc/redis.conf"
run dockerfile
docker build -t gary_centos /usr/local/sbin/docker/
start the container
docker run -d -p 3004:6379 hp --bind 0.0.0.0