docker Foundation
Preface
This is an article I wrote after three weeks of spare time study. My understanding of docker is still at the beginning stage. I hope this article can help some friends who want to learn docker get started quickly. The exercises and actual combat codes are in the github repository. If my article can help you, you can give it to me. docker project Give me a compliment.
introduce
Docker is an open source application container engine. Developers can package their own applications in the container and then migrate to docker applications on other machines to achieve rapid deployment. If a failure occurs, the service can be quickly restored by mirroring.
For example, companies generally have multiple environments, so how to keep multiple environments consistent, docker can be used at this time. And when you need to add an environment, you don't need to install and configure one environment after another on a new server. Just run the docker. Officials also provided Docker Hub It has a large number of high-quality official images. You can upload your own image. It's a bit like github.
install
Officials have provided installation tutorials, which are very detailed. Official Installation Course
docker starts
Step 1: The docker is successfully installed under the docker-v confirmation
If installed successfully, the command line will output the version number of docker. As follows:
Docker version 18.09.2, build 6247962
The whole life cycle of docker can be roughly divided into:
- image
- container
- Warehouse
Here, take ubuntu mirror as an example to introduce the lower mirror
Before downloading the ubuntu image, run docker images to view the local image. If you haven't downloaded the mirror yet, of course it will be empty. Here's my local mirror.
➜ study-docker git:(master) ✗ docker images REPOSITORY TAG IMAGE ID CREATED SIZE todolist_static latest de5e325037e9 2 hours ago 1.05GB todolist_nodejs latest 53efd80e03e1 2 hours ago 898MB ubuntu 18.04 7698f282e524 4 weeks ago 69.9MB mysql latest 990386cbd5c0 5 weeks ago 443MB node 8 a5c31320f223 6 weeks ago 895MB mysql 5.6 73829d7b6139 6 weeks ago 256MB
Use the pull mirror command docker pull to pull the Ubuntu image: docker pull ubuntu. When you do not specify a version, the latest version is pulled by default.
➜ study-docker git:(master) ✗ docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 5b7339215d1d: Pull complete 14ca88e9f672: Pull complete a31c3b1caad4: Pull complete b054a26005b7: Pull complete Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c Status: Downloaded newer image for ubuntu:latest ➜ study-docker git:(master) ✗ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 4c108a37151f 12 hours ago 64.2MB
Specified version mirrors can also be installed: docker pull ubuntu:18.04
Next, start a container based on the ubuntu image
docker run --name first -it ubuntu bash
- --name is used to specify the container name
- It is used for interactive command-line operations. After running the following example, it opens the container's command line
- The Ubuntu above refers to the mirror image, which is based on latest by default. Unless specified version such as ubuntu:18.04
After running the above command, the command line tool automatically enters the container's command line. If you want to exit the command line interface, enter exit to exit.
➜ study-docker git:(master) ✗ docker run --name first -it ubuntu bash root@b7862a018c2c:/#
If you want the container to run in the background, you can add-d configuration to make the container run in the background. The command line tool does not enter the container when running in the background.
Use docker ps to view currently running containers.
➜ study-docker git:(master) ✗ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf8375f48225 ubuntu "bash" 15 seconds ago Up 14 seconds first
Use - d to let the container run in the background
➜ study-docker git:(master) ✗ docker run --name first -itd ubuntu bash 6df29a09d1f1bb0041b7eb59b5288162471ed8a663007f88c6a30e3fd1f4fbe2
The command line returns the container id
Use docker containers to view a list of all containers (excluding stopped containers)
➜ study-docker git:(master) ✗ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf8375f48225 ubuntu "bash" About a minute ago Up About a minute first
Stop the container using docker stop <container id or container name>.
➜ study-docker git:(master) ✗ docker stop 6df29a09d1f1 6df29a09d1f1
When the command is executed, the id of the container you just entered is returned. The container id above does not need to be filled in. Just like git's commit id.
At this time, the container information can not be found through docker container ls. You need to use docker container ls-a to view.
➜ study-docker git:(master) ✗ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6df29a09d1f1 ubuntu "bash" 5 minutes ago Exited (0) 4 minutes ago
You can see in the STATUS column that the container is in a stop state.
Use docker RM < container id or container nickname >
➜ node git:(master) docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a217eea7188f ubuntu "/bin/bash" 11 seconds ago Exited (0) 10 seconds ago dreamy_ishizaka
Execute docker rm a21 (id can not be lost)
➜ node git:(master) docker rm a21 a21
After execution, the command line returns the container id entered before.
Use docker container prune to empty the container in the deactivated state.
Use the docker exec command to enter the running container
If you want to enter the interactive interface of the container just running in the background: docker exec-it < container name or container ID > Bash
➜ study-docker git:(master) ✗ docker exec -it first bash root@2a87b2f62a6e:/#
Want to see more about docker commands, click here
Dockerfile
An example of node mirroring
Create a new folder
I'll create a new folder called node, which can refer to my github project. node directory
// index.js const express = require('express'); const app = express(); app.get('/', (req, res) => { res.end('success') }) app.listen(6001)
// Dockerfile FROM node:8 WORKDIR /home/node COPY ../ ../ RUN npm install CMD npm start
Introduction to Directives
- FROM My node example is based on node8 mirroring
- WORKDIR specifies the workspace.
- COPY Copies Local Directory Files into docker
- RUN runs a container, and each RUN generates a container
- CMD executes commands, similar to RUN
Note that it's necessary to add a. dockerignore file, which can fill in files that you don't want to pack into containers. Similar to. gitignore
// .dockerignore /node_modules package-lock.json
Detailed explanation, see dockerfile
The docker build command is used to create images using Dockerfile
Execution: docker build-t mynode.
- - t: The name and label of the image, usually in name:tag or name format; multiple labels can be set for a single image in a single build. Tag does not write the latest version by default
Note the following. This indicates that the Dockerfile file is in the current directory.
After the successful construction of the mirror:
➜ node git:(master) ✗ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mynode latest 3cd10521f802 10 hours ago 898MB
Next, run a node container based on the image:
docker run --name mynode -p 4001:6001 mynode
- --name: denotes the container's anonymity
- - p: Represents port mapping, because the host's IP is different from the container's ip, it is necessary to map the container's services to 0.0.0:the host port set by itself. The host does not fill in the defau lt of 0.0.0.0. Host Port >: <Port in Container >;
➜ node git:(master) ✗ docker run --name mynode -p 4001:6001 mynode > example2@1.0.0 start /home/node > node index.js
The browser accesses localhost:4001, and the page shows the success string of the node response.
After building the image, you don't think you need the image. What if you want to delete it?
First execute: docker images list mirrors
➜ node git:(master) ✗ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mynode latest 3cd10521f802 10 hours ago 898MB
Use docker RMI < image ID > to delete the image: docker rmi 3cd10521f802. If you remind the image to be occupied by the container, you need to delete the container first (refer to the command described above).