Build your own Ubuntu image under Docker

Since the Ubuntu image in the Docker image station is a minimum version, there is not even vi 🙁 Every time you create a container, you need to replace the domestic source and install tools such as gcc. Therefore, the author thinks that you need to make an Ubuntu image that suits you, reducing the number of follow-up The dirty work

Notes

  • The following commands are completed under the root user. If you are not in the root user, use the following command to switch to the root user.
su root
  • If the download is slow, please Baidu how to replace the Docker image with a domestic image, such as Alibaba Cloud’s container image site.

Get Ubuntu

Use the following command to download an Ubuntu 16.04 mirror from the mirror site

docker pull ubuntu 16.04

Will output the following information

root@ubuntu:~# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
4f53fa4d2cf0: Pull complete
6af7c939e38e: Pull complete
903d0ffd64f6: Pull complete
04feeed388b7: Pull complete
Digest: sha256:185fec2d6dbe9165f35e4a1136b4cf09363b328d4f850695393ca191aa1475fd
Status: Downloaded newer image for ubuntu: 16.04
docker.io/library/ubuntu:16.04

Use the command docker images to check whether the image is downloaded locally, and the following information will be output

root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 096efd74bb89 3 weeks ago 127MB

Run container

Create a container for the newly downloaded ubuntu:16.04 in the foreground and enter the shell

docker run -it ubuntu:16.04 /bin/bash

At this point, you have entered the shell of the container, as shown below

root@ubuntu:~# docker run -it ubuntu:16.04 /bin/bash
root@2ce6712ef339:/#

Container for source

Change the foreign source of the container to Ali source

Delete the original foreign source file sources.list

rm /etc/apt/sources.list

Create a new file with the same name as sources.list. Since the Ubuntu system under Docker does not have editors such as Vim and gedit, you need to use cat The code> command batch writes multiple lines of content to the specified file, as shown below.

cat>/etc/atp/sources.list<<EOF

The above command indicates that the input is completed after the three characters EOF are input.

After executing the above statement, copy the source information of Alibaba Cloud below, take the three characters EOF as the last line

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

Then use the command cat /etc/apt/sources.list to output the file information. The output information of the whole process is as follows.

root@2ce6712ef339:/# rm /etc/apt/sources.list
root@2ce6712ef339:/# cat>/etc/apt/sources.list<<EOF
> deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
> EOF
root@2ce6712ef339:/# cat /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

Update apt

Use the following command to update apt

apt update

Output the following information

root@2ce6712ef339:/# apt update
Get:1 http://mirrors.aliyun.com/ubuntu bionic InRelease [242 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease [242 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic/main Sources [1063 kB]
Get:7 http://mirrors.aliyun.com/ubuntu bionic/restricted Sources [5823 B]
Get:8 http://mirrors.aliyun.com/ubuntu bionic/universe Sources [11.5 MB]
Get:9 http://mirrors.aliyun.com/ubuntu bionic/multiverse Sources [216 kB]
Get:10 http://mirrors.aliyun.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:11 http://mirrors.aliyun.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:12 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:13 http://mirrors.aliyun.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:14 http://mirrors.aliyun.com/ubuntu bionic-security/main Sources [298 kB]
Get:15 http://mirrors.aliyun.com/ubuntu bionic-security/restricted Sources [13.0 kB]
Get:16 http://mirrors.aliyun.com/ubuntu bionic-security/universe Sources [331 kB]
Get:17 http://mirrors.aliyun.com/ubuntu bionic-security/multiverse Sources [4352 B]
Get:18 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 Packages [1733 kB]
Get:19 http://mirrors.aliyun.com/ubuntu bionic-security/restricted amd64 Packages [205 kB]
Get:20 http://mirrors.aliyun.com/ubuntu bionic-security/universe amd64 Packages [1348 kB]
Get:21 http://mirrors.aliyun.com/ubuntu bionic-security/multiverse amd64 Packages [15.0 kB]
Get:22 http://mirrors.aliyun.com/ubuntu bionic-updates/main Sources [621 kB]
Get:23 http://mirrors.aliyun.com/ubuntu bionic-updates/restricted Sources [18.4 kB]
Get:24 http://mirrors.aliyun.com/ubuntu bionic-updates/universe Sources [547 kB]
Get:25 http://mirrors.aliyun.com/ubuntu bionic-updates/multiverse Sources [11.6 kB]
Get:26 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 Packages [2150 kB]
Get:27 http://mirrors.aliyun.com/ubuntu bionic-updates/restricted amd64 Packages [231 kB]
Get:28 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 Packages [2112 kB]
Get:29 http://mirrors.aliyun.com/ubuntu bionic-updates/multiverse amd64 Packages [45.5 kB]
Get:30 http://mirrors.aliyun.com/ubuntu bionic-proposed/main Sources [111 kB]
Get:31 http://mirrors.aliyun.com/ubuntu bionic-proposed/restricted Sources [9288 B]
Get:32 http://mirrors.aliyun.com/ubuntu bionic-proposed/universe Sources [18.0 kB]
Get:33 http://mirrors.aliyun.com/ubuntu bionic-proposed/multiverse Sources [899 B]
Get:34 http://mirrors.aliyun.com/ubuntu bionic-proposed/main amd64 Packages [212 kB]
Get:35 http://mirrors.aliyun.com/ubuntu bionic-proposed/restricted amd64 Packages [69.3 kB]
Get:36 http://mirrors.aliyun.com/ubuntu bionic-proposed/universe amd64 Packages [44.8 kB]
Get:37 http://mirrors.aliyun.com/ubuntu bionic-proposed/multiverse amd64 Packages [11.3 kB]
Get:38 http://mirrors.aliyun.com/ubuntu bionic-backports/main Sources [5823 B]
Get:39 http://mirrors.aliyun.com/ubuntu bionic-backports/universe Sources [5759 B]
Get:40 http://mirrors.aliyun.com/ubuntu bionic-backports/main amd64 Packages [11.3 kB]
Get:41 http://mirrors.aliyun.com/ubuntu bionic-backports/universe amd64 Packages [11.4 kB]
Fetched 36.6 MB in 28s (1277 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
90 packages can be upgraded. Run 'apt list --upgradable' to see them.

Install build-essential

Use the following command to install build-essential

apt install build-essential

Check the following gcc version, if the output gcc version, the installation is successful

gcc --version

The output information is as follows

root@2ce6712ef339:/# gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Generate image

We need to generate images based on the currently configured containers to facilitate subsequent operations.

Do not exit the current container, but create a new terminal, and the following operations are all completed under the new terminal.

The command to generate the image

docker commit -m "description" -a "author" <container id> repository:tag
  • “description”: description information.
  • “author”: The name of the author.
  • : It can be seen in the terminal, in the form of root@2ce6712ef339:/#. You can see that is 2ce6712ef339.
  • reposiory: The name of the mirror warehouse, you can choose it at will.
  • tag: Mirror image tag name, you can choose any.

In this example, the following command is used to generate the image

docker commit -m "ubuntu 16.04 with aliyun and build-essential" -a "Nelson" 2ce6712ef339 nelson/ubuntu:16.04

After executing the above statement, use the following command to check whether the image is generated

docker images

You can see the image nelson/ubuntu:16.04 in the output.

root@ubuntu:/home/nelson/docker/hw2# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nelson/ubuntu 16.04 4192315dcfef 5 seconds ago 419MB
ubuntu 16.04 096efd74bb89 3 weeks ago 127MB

After successfully generating the image, you can directly use the image to create a container in the future, without changing the source, installing gcc or something. According to the reader’s needs, you can install more software such as vim and then generate the image.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry skill treeHomepageOverview 12912 people are studying systematically