Install Docker

  1. Install the Docker dependency library.
yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code
  1. Add Docker CE software source information.
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code
  1. Install Docker CE.
yum makecache fast
yum -y install docker-ce
Copy the code
  1. Start the Docker service.
systemctl start docker
Copy the code
  1. Modify the/etc/docker/daemon.jsonTo use the accelerator
tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": ["https://xxx.com"]
}
EOF
Copy the code
  1. Reload the service configuration file
systemctl daemon-reload
Copy the code
  1. Restart the Docker service
systemctl restart docker
Copy the code

Install the Node.js environment

  1. Download the Node.js package
Wget HTTP: / / https://npm.taobao.org/mirrors/node/v12.4.0/node-v12.4.0-linux-x64.tar.xzCopy the code
  1. Unpack the
The tar XVF - node - v12.4.0 - Linux - x64. Tar. XzCopy the code
  1. Move and rename the Node.js installation directory
Node - mv v12.4.0 - Linux - x64 / / usr /local/node
Copy the code
  1. Add the node.js executable directory to the system environment variable
echo "export PATH=$PATH:/usr/local/node/bin" >> /etc/profile
Copy the code
  1. Make the configured environment variables take effect immediately, and check the Node and NPM versions
source /etc/profile

# check version
node -v
npm -v
Copy the code

Deploy the Node project using Docker

(1) The way of mapping host directory

  1. Pull the latest Node image
docker pull node:latest
Copy the code
  1. Run the container, assuming the project directory:/usr/app/Helloworld
docker run -it -v /usr/app/Helloworld/:/usr/app/src/ -w /usr/app/src/ -p 8080:8080 --name Helloworld -d node:latest npm run serve
# -i (interactive) Runs the container in interactive mode
# -t (terminal) reassigns a pseudo-input terminal to the container
# -v (volume) Directory mapping host: inside the container
# -w (workdir) Specifies the working directory within the container
# -p (port) Port mapping
# --name Container name
# -d () background run container
Rm is deleted when the container is pushed out
# image is followed by the command to execute
Copy the code

(2) Create a mirror

  1. Create a Node project
npm init -y

npm i express -D

touch server.js

Write a simple Web application
vim server.js
Copy the code
  1. Create a Dockerfile file in the project root directory
touch Dockerfile
Copy the code
  1. Write Dockerfile
# Base image
FROM node:latest
# specify the working directory
WORKDIR /usr/src/app
# copy package.json and lock files
# copy 'package.json' file instead of the whole working directory. This allows us to take advantage of caching the Docker layer
COPY package*.json . /
# install dependencies
RUN npm install
# copy all files (. Stands for context passed in at docker build)
COPY . .
# port
EXPOSE 8080
# execute command
CMD ["npm"."start"]
Copy the code
  1. . Dockerignore file
node_modules
npm-debug.log
Copy the code
  1. Execute build in the application root directory
Docker build. -t <your username>/node-web-app # build is the context for the Dockerfile to use # -t to mark the image, so that 'docker images' can be foundCopy the code
  1. Run the image after the image is created
docker run -p 49160:8080 -d <your username>/node-web-app
Copy the code

Install Jenkins using Docker

  1. Download Jenkins Mirror
docker pull jenkins/jenkins
Copy the code
  1. Start Jenkins container
$mkdir /var/jenkins_home $sudo chown 777 /var/jenkins_home # $sudo chown -r 1000:1000 $docker run -d -p 8080:8080 -p 505:50000 -v /var/jenkins_HOME :/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v /etc/localtime:/etc/localtime --name Jenkins docker. IO/Jenkins Jenkins - d background image - p, 8080:8080 will mirror the port 8080 port 8080 is mapped to the server (server port: The JNLP-based Jenkins agent communicates with the Jenkins Master through TCP port 50000, so it also maps -v /var/jenkins_home: The /var/jenkins_home directory is the Jenkins working directory. We mounted a directory on the hard disk to this location so that we could continue to use the original working directory after updating the image. - v/var/run/docker. The sock: / var/run/docker. The sock Jenkins container docker are needed to perform the task, So here will docker command also mapped to container - v/usr/bin/docker: / usr/bin/docker will docker commands mapped to a container - v/etc/localtime: / etc/localtime Let the container use the same time Settings as the server. - the name myjenkins to name the container an alias docker. IO/Jenkins Jenkins using just download a mirror image of the installation errors that may occur: 1, "docker run" requires the at 1 argument further. Reason: command less parameters, namely the command need to face with the Jenkins installation source: docker. IO/Jenkins Jenkins 2, the Error response from the daemon: driver failed programming external connectivity on endpoint jenkins (db9d8092ddb37d4243647cad8c63555e090b9 161d1f3c90db8d8c43281e9c86e): (iptables failed: Iptables --wait -t NAT -a DOCKER -p TCP -d 0/0 --dport 8081 -j DNAT --to-destination 172.17.0.2:8080! -i docker0:iptables: No chain/target/match by that name. The reason: Port mapping fails. $sudo systemctl restart Docker https://cao0507.github.io/2018/12/05/docker%E5%AE%B9%E5%99%A8%E5%90%AF%E5%8A%A8%E6%97%B6%E7%AB%AF%E5%8F%A3%E6%98%A0%E5%B 0% 84% B4 E8 B1 A4 E5% % % % % A5/3, the docker: Command not found Jenkins There is no docker command in the container, so the docker command can be found inside the container by hanging in the docker command directory, and the host docker can execute related commands by mounting docker.sockCopy the code