For the basic concept of Docker, please check the continuous integration of Docker + Gitlab CI (I).
Install Docker
Docker installation tutorial many, git address search download there are many ways to download, here is not described, also can use free installation package after the installation of the check version
docker version
Copy the code
Print a message like the following, congratulations, your journey of a thousand miles has begun
[root@VM_0_12_centos ~]# docker version
Client:
Version: 17.05.0-ce
API version: 1.29
Go version: go1.7.5
Git commit: 89658be
Built: Fri May 5 15:36:11 2017
OS/Arch: linux/amd64
Copy the code
Start the first container
1. Find the required image (take nginx as an example)
docker search nginx
Copy the code
There will be many related images in the Docker market. If there is no special requirement, you can use the official one
[root@VM_0_12_centos ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 9563 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1400 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 611 [OK] jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as... 405 [OK] kong Open-source Microservice & API Management ... 224 [OK] webdevops/php-nginx Nginx with PHP-FPM 113 [OK] kitematic/hello-world-nginx A light-weight nginx container that demons... 108Copy the code
2. Download iAMGE
docker pull nginx
Copy the code
Nginx can specify a version number, written as
Docker pull nginx: version numberCopy the code
If this parameter is not specified, the latest version is displayed by default
3. View the downloaded image
docker images
Copy the code
[root@VM_0_12_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c82521676580 6 weeks ago 109MB
Copy the code
4. Start the container
docker run -d -p 80:80 --name nginx -v $PWD/dist/:/usr/share/nginx/html nginx
Copy the code
- Docker run creates a container in which to run an image
- -d containers can run in the background, that is, daemons
- -p Maps the external ports of the container to the ports of the host. The access to port 80 of the host is actually the access to port 80 of the container
- – Name Specifies the name of the container. If this parameter is not specified, a random name is automatically generated
- -v refers to the mapping of the container path. The host path before and after the colon (:) accesses the contents of /usr/share/nginx/html in the container. When the container is started, it is automatically synchronized from the host path to the container
- Nginx is a good image of pull
5. View the list of started containers
docker ps
Copy the code
[root@VM_0_12_centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d94ee9b43d3 nginx "nginx -g 'daemon ..." 2 weeks ago Up 2 weeks 0.0.0.0:80->80/ TCP nginxCopy the code
At this point, the access server IP is already accessible, but there are no files in the root directory of nginx, indicating that nginx is ready
The above mentioned directly use the image provided by the warehouse, so how to make your own image?
3. Make a mirror image
1, create a Dockerfile in the root directory of the project that needs to make an image. Here we use node+koa service project as an example to write the following contents
FROM node:latest
RUN mkdir -p /home/Service
COPY . /home/Service
WORKDIR /home/Service
EXPOSE 8000
RUN npm install
CMD ["npm"."start"]
Copy the code
- FROM is the base image
- RUN Runs the command
- COPY Copies the current contents to the folder
- WOEKDIR Working directory of the docker
- EXPOSE External port
- CMD Execution script
2. Run the build command in the Dockerfile directory
docker build -t testImage .
Copy the code
After the build is complete, run the command to view the image
docker images
Copy the code
Let’s start it up
docker run -d -p 5000:8000 --name testimage testimage
Copy the code
You can also follow my blog the silent blog