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

Docker Light-weight virtualization-for class 2

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

Share

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

Docker class 2: run the docker image and introduce the docker volume and web server

(1)

We will introduce how to use docker to run in backdround!

Docker run-it-d chusiang/takojs1607 / bin/bash-c "while true; do echo 'hi'; sleep 1; done"

-d means daemon, run in the background! You can use docker ps-a see the docker process which run in the background!

-c means "while true; do echo 'hi'; sleep 1; done" is string

For example, the container ID is f0e

You can use this command to see the logs.

Docker logs f0e

(2)

You can use this command to give container name which you run

Docker run-it-d-- name test1 chusiang/takojs1607

-- name means the container name

(3)

The data haven't seen when you closed the docker if you close directly

You can use docker voulme to save the data in docker!

On the basis of the first class, we create the environment to show that!

First, you need to change the user for root!

Cd /

Mkdir / mydata

Cd mydata/

Echo "hello world" > hello.txt

Docker run-it-v / mydata:/data chusiang/takojs1607 / bin/bash

-v means volume

/ mydata is local host

/ data is docker folder

(4)

If you want to open a new terminal in docker containers which is the same container ID, you can use this command

Docker exec-it CONTAINER ID bash

(5)

If you want to remove docker containers automatic after finished the docker container, you can add-rm when you start the docker container!

Docker run-it-name test1-rm chusiang/takojs1607 / bin/bash

(6)

If you want to switch terminal to host, you can tap the control + P + Q in your keyboard. If you want to use docker container, just attach it!

(7)

Docker is the Light-weight virtualization, Many packages are not installed in it! For example: ifconfig, ping etc.

If your docker is ubuntu, you can use apt-get update, and apt-get install net-tools.

But!! Most important is it will disappear if you restart the docker.

So, you can create a new docker images with the package installed

For example, the container ID is ccea9d4e9655

Docker commit cce ubuntu:mv1

Then, you can start the docker new images with ifconfig tools!

If you want to remove the docker images, you can use this command

Docker image rm IMAGE ID

How to use Web server in docker!?

We want to create the web page in the host virtual machine and map to the docker for starting the web server

For the first, we need to pull the docker images with httpd from docker hub

Docker pull httpd

And the document root is / usr/local/apache2/htdocs/

Now, we will create the webpage.

Cd / mydata/echo "this is an index webpage" > index.htmlecho "hi" > hi.htm

Start the docker container with apache

Docker run-itd-- name mywebserver-p 8080 name mywebserver 80-v / mydata:/usr/local/apache/htdocs/ httpd

-p means port

8080 is local port

80 is docker port

You can use curl 127.0.0.1:8080 for test!

For important, you can edit the webpage in local!

After edited, the docker web server will update!

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