Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Docker container after Docker installation in Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use the Docker container after Docker installation in Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Start a Docker container

[root@localhost] # docker run-I-t Ubuntu / bin/bash

Unable to find image 'ubuntu' locally

Pulling repository ubuntu

04c5d3b7b065: Download complete

511136ea3c5a: Download complete

C7b7c6419568: Download complete

70c8faa62a44: Download complete

D735006ad9c1: Download complete

Status: Downloaded newer image for ubuntu:latest

Note:

-I: open the STDIN in the container

-t: assign a pseudo tty terminal to the container

As we can see from the above, first of all, Docker will check whether the ubuntu image exists locally. If the image is not found locally, then Docker will go to the official Docker Hub Registry to see if it exists in the Docker Hub. Once Docker finds the image, it downloads the image and saves it to the local host.

Docker then uses this image to create a new container inside the file system. The container has its own network, IP address, and a bridging network interface that can communicate with the host. Finally, we tell Docker what commands to run in the new container.

When the container is created, Docker executes the / bin/bash command in the container. At this time, we can see the shell in the container.

Root@8c342c0c275c:/#

Note: 8c342c0c275c represents the ID of the container

two。 Use containers

View the hostname of the container

Root@8c342c0c275c:/# hostname

8c342c0c275c

As you can see, the hostname of the container is the ID of the container

View the hosts file of the host

Root@8c342c0c275c:/# cat / etc/hosts

172.17.0.2 8c342c0c275c

Ff00::0 ip6-mcastprefix

Ff02::1 ip6-allnodes

Ff02::2 ip6-allrouters

127.0.0.1 localhost

:: 1 localhost ip6-localhost ip6-loopback

Fe00::0 ip6-localnet

You can see that Docker adds a host configuration entry to the IP address of the container.

View the IP address of the container

Root@8c342c0c275c:/# ip a

1: lo: "LOOPBACK,UP,LOWER_UP" mtu 65536 qdisc noqueue state UNKNOWN group default

Link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

Inet 127.0.0.1/8 scope host lo

Valid_lft forever preferred_lft forever

Inet6:: 1/128 scope host

Valid_lft forever preferred_lft forever

4: eth0: "BROADCAST,UP,LOWER_UP" mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

Link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff

Inet 172.17.0.2/16 scope global eth0

Valid_lft forever preferred_lft forever

Inet6 fe80::42:acff:fe11:2/64 scope link

Valid_lft forever preferred_lft forever

View the processes running in the container

Root@8c342c0c275c:/# ps-aux

USER PID CPU MEM VSZ RSS TTY STAT START TIME COMMAND

Root 1 0.0 0.1 18168 1976? Ss 09:38 0:00 / bin/bash

Root 19 0.0 0.0 15568 1144? R + 09:55 0:00 ps-aux

Install a software package in the container

Root@8c342c0c275c:/# apt-get update & & apt-get install vim

Ign http://archive.ubuntu.com trusty InRelease

Ign http://archive.ubuntu.com trusty-updates InRelease

Ign http://archive.ubuntu.com trusty-security InRelease

Hit http://archive.ubuntu.com trusty Release.gpg

Get:1 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]

Get:2 http://archive.ubuntu.com trusty-security Release.gpg [933 B]

Hit http://archive.ubuntu.com trusty Release

Get:3 http://archive.ubuntu.com trusty-updates Release [62.0 kB]

Get:4 http://archive.ubuntu.com trusty-security Release [62.0 kB]

Get:5 http://archive.ubuntu.com trusty/main Sources [1335 kB]

Get:6 http://archive.ubuntu.com trusty/restricted Sources [5335 B]

Get:7 http://archive.ubuntu.com trusty/universe Sources [7926 kB]

Get:8 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]

Get:9 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]

Get:10 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]

Get:11 http://archive.ubuntu.com trusty-updates/main Sources [193 kB]

Get:12 http://archive.ubuntu.com trusty-updates/restricted Sources [1874 B]

Get:13 http://archive.ubuntu.com trusty-updates/universe Sources [119 kB]

Get:14 http://archive.ubuntu.com trusty-updates/main amd64 Packages [493 kB]

Get:15 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [14.8 kB]

Get:16 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [298 kB]

Get:17 http://archive.ubuntu.com trusty-security/main Sources [70.1 kB]

Get:18 http://archive.ubuntu.com trusty-security/restricted Sources [1874 B]

Get:19 http://archive.ubuntu.com trusty-security/universe Sources [19.1 kB]

Get:20 http://archive.ubuntu.com trusty-security/main amd64 Packages [229 kB]

Get:21 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB]

Get:22 http://archive.ubuntu.com trusty-security/universe amd64 Packages [98.1 kB]

Fetched 20.3 MB in 41s (490 kB/s)

Reading package lists. . . Done

Reading package lists. . . Done

Building dependency tree

Reading state information. . . Done

The following extra packages will be installed:

Libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim-runtime

Suggested packages:

Gpm ctags vim-doc vim-scripts

The following NEW packages will be installed:

Libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim

Vim-runtime

0 upgraded, 6 newly installed, 0 to remove and 2 not upgraded.

Need to get 9083 kB of archives.

After this operation, 42.9 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgpm2 amd64 1.20.4-6.1 [16.5 kB]

Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7-minimal amd64 2.7.6-8 [307 kB]

Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7-stdlib amd64 2.7.6-8 [1872 kB]

Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7 amd64 2.7.6-8 [1044 kB]

Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main vim-runtime all 2 trusty/main vim-runtime all 7.4.052-1ubuntu3 [4888 kB]

Get:6 http://archive.ubuntu.com/ubuntu/ trusty/main vim amd64 2 trusty/main vim amd64 7.4.052-1ubuntu3 [956 kB]

Fetched 9083 kB in 51s (175kB/s)

Selecting previously unselected package libgpm2:amd64.

(Reading database. 11527 files and directories currently installed.)

Preparing to unpack . / libgpm2_1.20.4-6.1_amd64.deb.

Unpacking libgpm2:amd64 (1.20.4-6.1).

Selecting previously unselected package libpython2.7-minimal:amd64.

Preparing to unpack . / libpython2.7-minimal_2.7.6-8_amd64.deb.

Unpacking libpython2.7-minimal:amd64 (2.7.6-8).

Selecting previously unselected package libpython2.7-stdlib:amd64.

Preparing to unpack . / libpython2.7-stdlib_2.7.6-8_amd64.deb.

Unpacking libpython2.7-stdlib:amd64 (2.7.6-8).

Selecting previously unselected package libpython2.7:amd64.

Preparing to unpack . / libpython2.7_2.7.6-8_amd64.deb.

Unpacking libpython2.7:amd64 (2.7.6-8).

Selecting previously unselected package vim-runtime.

Preparing to unpack . / vim-runtime_2%3a7.4.052-1ubuntu3_all.deb.

Adding 'diversion of / usr/share/vim/vim74/doc/help.txt to / usr/share/vim/vim74/doc/help.txt.vim-tiny by vim-runtime'

Adding 'diversion of / usr/share/vim/vim74/doc/tags to / usr/share/vim/vim74/doc/tags.vim-tiny by vim-runtime'

Unpacking vim-runtime (2 7.4.052-1ubuntu3).

Selecting previously unselected package vim.

Preparing to unpack . / vim_2%3a7.4.052-1ubuntu3_amd64.deb.

Unpacking vim (2 7.4.052-1ubuntu3).

Setting up libgpm2:amd64 (1.20.4-6.1).

Setting up libpython2.7-minimal:amd64 (2.7.6-8).

Setting up libpython2.7-stdlib:amd64 (2.7.6-8).

Setting up libpython2.7:amd64 (2.7.6-8).

Setting up vim-runtime (2 7.4.052-1ubuntu3).

Processing / usr/share/vim/addons/doc

Setting up vim (2 7.4.052-1ubuntu3).

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/vim (vim) in auto mode

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/vimdiff (vimdiff) in auto mode

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/rvim (rvim) in auto mode

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/rview (rview) in auto mode

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/vi (vi) in auto mode

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/view (view) in auto mode

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/ex (ex) in auto mode

Update-alternatives: using / usr/bin/vim.basic to provide / usr/bin/editor (editor) in auto mode

Processing triggers for libc-bin (2.19-0ubuntu6.4).

Exit the container

Root@8c342c0c275c:/# exit

Exit

When we type exit, the container stops working. The container is running accordingly only when the specified / bin/bash command is running. Once you exit the container, the / bin/bash command ends, and the container stops.

View a list of containers in the system

[root@localhost] # docker ps-a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

8c342c0c275c ubuntu:latest "/ bin/bash" 26 minutes ago Exited (0) 5 minutes ago sharp_bohr

8c342c0c275c: represents the ID of the container

Ubuntu:latest: create an image of the container

"/ bin/bash": the last command executed by the container

26 minutes ago: creation time

Exited (0) 5 minutes: status of container exit

Sharp_bohr: name of the container

Naming of containers in Docker

Docker automatically generates a random name for the container when it is created. So if we want to specify the name of a container when we create it, we can use the following command:

[root@localhost] # docker run-- name ovcer_the_container-I-t ubuntu / bin/bash

Root@1ce9f640478d:/#

The above command will create a container called ovcer_the_container. Only the following characters can be included for the name of a legitimate container: lowercase letter aquiz, uppercase letter Amurz, number 0per9, underscore, dot, dash.

This is the end of the content of "how to use the Docker container after Docker installation in Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report