This article introduces the creation process of my first doccker application container
Mainly refer to the official tutorial sample application , which also encountered confusing questions, and found the answer through the Internet, but the official tutorial did not mention it.
Install docker
I installed docker: on windows 10. I don’t know which tutorial I’ve read. I installed boot2doker first, but it didn’t work. Later, it is an outdated method, just install it directly. The official tutorial docker studying there in
link
Or download the windows 10 version directly . There is a saying that the windows version can only be the windows pro version, and other versions need to be different. I don't see this statement here, but I am the windows pro version.
What the sample application looks like
Enter localhost:8080 on the local browser to have the following applications:
Enter the content in Add Item, then click the button, add a line of content below, and delete a red dot on the right side of the line
This application uses node.js. It doesn't matter if you don't understand the original text, I don't understand.
Get the app
First download the code compression file to get an app.zip file, and then unzip it to get an app directory.
The content of the app is: there are 2 files and 2 directories under the app.
In the original text, it is said that you can open and edit with your favorite ide, in fact, this part only needs to edit a text file.
The following is what you see when you open it with Visual Studio Code, where package.json is opened
You can use dir or file manager to look at the directory structure. It doesn't matter if you don't see it.
Create application container Image
In order to build the application, a Dockerfile is required. Dockerfile is just a text-based instruction script used to create container images.
Create a Dokerfile file. Note that there can be no extension at the end. If you edit with Notepad, it will add a txt extension to you. In case of this, change the name to ren and remove the extension.
The content of the file is:
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
After the Dockerfile is created, you can edit and build the container image. The command is:
docker build -t getting-started .
This command uses a Dockerfile to build a new container Image. You may have noticed that many "layers" are downloaded. This is because we instructed the builder to start with the node:12-alpine image. However, since the Image is not available on our computer, we need to download the Image.
After downloading the Image, we copied it into the application and installed the application's dependencies using yarn. The CMD directive specifies the default command to be run when the container is launched from this image.
Finally, -t marks our Image name. You can simply think of it as an easy-to-understand name for the final Image. Since we named the image starting-starting, we can refer to the Image when running the container.
Note that the. After the command line means the current directory. The content at the end of the docker build command tells Docker that it should look for the Dockerfile in the current directory.
Start the application container
Run the following command to start the application container:
docker run -dp 3000:3000 getting-started
Remember the -d and -p flags? We are running the new container in "detached" mode (in the background) and creating a mapping from port 3000 of the host to port 3000 of the container. If there is no port mapping, we will not be able to access the application.
I have a problem with this command, the error message is as follows:
C:\download\dockerapp> docker run -dp 3000:3000 getting-started
a13defcc0b4f65ddfb8290ee0493ef5c59a7530cf11f75fb8349313069acc4bc
docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:3000: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
Later, I searched for a solution on the Internet. I was stuck here for 2 days. I saw a person who said to check the banned information of the local port:
netsh interface ipv4 show excludedportrange protocol=tcp
My message is:
C:\download\dockerapp>netsh interface ipv4 show excludedportrange protocol=tcp
协议 tcp 端口排除范围
开始端口 结束端口
---------- --------
1692 1791
1792 1891
1892 1991
1992 2091
2180 2279
2480 2579
2580 2679
2790 2889
2890 2989
2990 3089
3090 3189
3190 3289
3490 3589
3590 3689
4014 4113
4114 4213
4363 4462
4499 4598
5275 5374
5375 5474
50000 50059 *
* - 管理的端口排除。
Indeed, the port is banned. Since the port is banned, use a port that is not banned.
I thought so, so I used the following command;
docker run -dp 8080:3000 getting-started
There is no error message this time, you can see the commands and messages are as follows:
C:\download\dockerapp> docker run -dp 8080:3000 getting-started
b461fd95b2211a463baabd89faba93ab9e0fe31b91d336f972f36e2f9c589dc8
I then type in the browser:
localhost:8080
Get the result of starting that application.
: The front is the local port number, and the back is the port number of the container, so that port mapping is realized.