This is the 12th day of my participation in Gwen Challenge

Package based on Ubuntu images

Manual packaging

Pull the Ubuntu system and start the instance. If you do not specify the version number, the default version is Ubuntu 20.04.1 LTS

docker run -it ubuntu
Copy the code

When the container is started, it goes into Bash inside the Ubuntu container.

We installed some software in Ubuntu, such as: Node.js AND write a program that prints Hello World, then package the container again, AND package it into a new image. This means that you or someone else can use docker Run to pull back the loaded Node.js image AND run it as a new container.

Before installing Node.js, we changed the container’s image address to the faster Aliyun for Chinese.

apt install vim -y
vim /etc/apt/sources.list
Copy the code

Press GG to jump to the first line of the content, then press dG to clear the content of the file, press I to paste the following content

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
Copy the code

update

 sudo apt-get update
Copy the code

Install Node.js in Ubuntu

apt update && apt install nodejs -y
Copy the code

Once installed, open a new terminal and use Docker PS to see what containers are running

Enter the Docker Diff container ID to see a long list of files

  • A indicates the new file
  • C stands for updated file
  • D is the removed file

As we have just done, you can use this command to compare which files Ubuntu images have changed

Next we define the name of the mirror

Docker commit Container ID Your username/new image name: version number

Docker commit 5 ce lexinhu/ubuntu_node: 0.1Copy the code

Docker Images shows the image you just packaged

Run our newly packaged image

Docker run - it lexinhu/ubuntu_node: 0.1Copy the code

Enter the container and type node -v to view nodeJS

Write a program that prints Hello Wold

# node -v
v10.19.0
# mkdir /app
# cd /app
# pwd
/app
# echo "console.log('Hello World')" > hello-world.js
# lltotal 4 drwxr-xr-x. 2 root root 28 Dec 22 08:54 ./ drwxr-xr-x. 1 root root 29 Dec 22 08:53 .. / -rw-r--r--. 1 root root 27 Dec 22 08:54 hello-world.js# node hello-world.js 
Hello World
Copy the code

Exit the container, and the container terminates. The reason is that we did not start the container in the way of daemon, find it by docker ps -a, check the file changes by docker diff container ID

repack

Docker commit df3 lexinhu/ubuntu_node: 0.2Copy the code

Try to run

Docker run lexinhu/ubuntu_node:0.2 node /app/hello-world.jsCopy the code

This is the image packaged manually with docker Commit step by step

Automatic packaging (main)

Mainly generated by Dockerfile file

Create the ubuntu_node folder on your desktop and write a hello-world.js

Still in this folder, create the Dockerfile file

FROM ubuntu

RUN apt update && apt install nodejs -y

WORKDIR /app

COPY.

CMD ["node"."/app/hello-world.js"]
Copy the code
  • FROM ubuntuBased on ubuntu images
  • RUN apt update && apt install nodejs -yAn instruction to
  • WORKDIR /appDefining a working directory
  • COPY . .Copy files from the current folder to the working directory
  • CMD ["node","/app/hello-world.js"]Commands executed by the container runtime

Because my native Win10 does not install Docker, I will upload it to Linux packaging

Go to the directory where the Dockerfile resides

Docker build -t lexinhu/ Ubuntu_node :0.3Copy the code

Docker creates new images according to the Settings of Dockerfile.

Docker run lexinhu/ubuntu_node: 0.3Copy the code

Push the mirror

Here I choose to use the mirror service of Ali Cloud

Login, the user name and password will be prompted.

sudo docker login registry.cn-shanghai.aliyuncs.com
Copy the code

push

Sudo docker tag (ImageId) registry.cn-shanghai.aliyuncs.com/xn2001/study_xn2001: [the mirror version number] sudo docker push Registry.cn-shanghai.aliyuncs.com/xn2001/study_xn2001: [the mirror version number]Copy the code

Example:

Sudo docker tag d86fdef8356e registry.cn-shanghai.aliyuncs.com/lexinhu/ubuntu_node:0.3 sudo docker push registry.cn-shanghai.aliyuncs.com/lexinhu/ubuntu_node:0.3Copy the code

pull

Sudo docker pull registry.cn-shanghai.aliyuncs.com/lexinhu/ubuntu_node:0.3Copy the code