docker 使用

极简操作:

image相当于一个被打包了的用于发布的安装程序,container相当于image被安装完了可以使用的本机程序。

先有一个image,可以这样查看 docker images.

如果有你要的image了,可以查看下该image是否创建了container,尽量一个image只使用一个container,除非你想新起一个环境。 docker ps -a可以查看所有(包括没运行的container)的容器。如果有相应的容器,就不要创建了,直接使用docker start -i 去运行它。如果该容器本来就在运行,则使用 docker exec 去attach它,进入shell。

如果没有container,则可以如下先创建一个。

从image创建一个container: docker create -i -t --name container_name image_id

最后运行该container: docker start -i container_name

用完了stop:docker stop  container_name

注意,docker run 容器名或者容器ID,相当于docker pull + docker create + docker start,即每次都会创建一个新的容器(create)并进入容器(start)。如果参数为容器名,若docker里没有你要run的image,则它还会帮你下载该image。比如你使用这样的命令 docker run --rm -it ubuntu:16.04。   如果使用 --rm 参数则它会帮你自动清除使用完后没用的容器,一般不建议使用--rm。start 和 exec 都没有--rm参数。只有run才能使用-v来映射目录,使用了-v来映射目录后,下次就只能使用run -v来运行,并且最好带上--rm,每次用完就把容器删掉,为啥可以删掉容器呢,后面的隐喻就是你用了-v来映射目录,你所需要上传到docker容器的所有内容都必须放在映射目录里(host目录),这样你就可以放心的使用--rm参数,不然你上传到docker的内容随着--rm的执行会被删除(容器stop时)。当然你也可以不映射,那你要把run拆分成pull(只需运行一次),create(只需运行一次)和start(create之后每次运行start即可),这样你就可以不用每次删掉容器了,而且create的时候也不要使用-v来映射目录,你的所有个人数据都会随容器一起被放在/var/docker里。

docker  exec 是attach到一个正在运行的容器的意思。start是运行一个没运行的container。

docker exec 的典型命令为: docker exec -it 6e393e6d5051 /bin/bash,其中-i表示(--interactive)交互式标准输入,-t (--tty)表示Allocate a pseudo-TTY。6e393e6d5051是容器id,/bin/bash 表示进入容器的bash shell。

docker自动启动image的方法:

为啥不是自动运行container,一般你发布的image就是最终可运行的image,你不会发布container,因为container可以被修改,所以你会修改稳定后,将container再生成image快照,然后发布这个最终的image,客户运行这个image就可以了。

先删掉基于该最终image的所有container,然后再基于该image运行命令,比如:

docker run -i -t -d -p 90:80 --privileged --restart=always onlyoffice/documentserver:7.1

其中的--restart=always就是会让他自启动,-d是也需要的。Run Docker in privileged mode (--privileged) making it possible to access your host’s hardware. 其中的 -p 90:80是将docker容器的80端口与host主机的90端口相互进行映射,你访问host的90就是访问容器的80. docker运行容器的时候还有个 -v选项,就是把host主机的文件夹映射为docker容器的目录,即 -v host_dir:docker_dir 表示将host当前目录下的host_dir与容器的docker_dir进行映射,当然你也可以写绝对路径(容器的路径一定要写绝对路径,不然容器不知如何寻址),映射后互相拷贝文件就直接拷贝就是了,如 -v /home/hzh/aaa:/var/www。

 

 

docker image 和 docker container的区别:

A Docker image is an immutable (unchangeable) file that contains the source code, libraries, dependencies, tools, and other files needed for an application to run.    Docker image是一个保持固定不可更改的的文件,它包含运行应用所需的源码、库、依赖、工具及其它文件。

Due to their read-only quality, these images are sometimes referred to as snapshots. They represent an application and its virtual environment at a specific point in time. This consistency is one of the great features of Docker. It allows developers to test and experiment software in stable, uniform conditions.    由于docker image的只读特性,images常常被称为一个快照。它代表某时刻运行一个应用所需要的虚拟环境。这种一致性是docker的一大特点。它允许开发者在稳定统一的环境下测试与检验自己的软件。

Since images are, in a way, just templates, you cannot start or run them. What you can do is use that template as a base to build a container. A container is, ultimately, just a running image. Once you create a container, it adds a writable layer on top of the immutable image, meaning you can now modify it.    由于image是一个模板,因此你不能直接运行他们。你能做的就是使用该模板为基础建立一个容器。容器从根本上来说就是一个运行起来的image。当你创建了一个容器,它就在不可改变的image上创建了一个可读写的软件层级,意味着你可以在这个层级进行读写,但image依旧保持不变。

The image-base on which you create a container exists separately and cannot be altered. When you run a containerized environment, you essentially create a read-write copy of that filesystem (docker image) inside the container. This adds a container layer which allows modifications of the entire copy of the image.      你用来创建容器的image与你所创建的容器是独立开的,这些image不能被容器层更改,即在容器里进行改变不会影响到image。当你运行一个容器化环境,你仅仅是在容器里拷贝了一份基于docker image的可读写的文件系统环境。然后你就可以在该image的复刻层级环境中(即container中)进行读写操作了。

 

 

 

0, 这是一个操作 Docker 镜像的小技巧:

不冲突的情况下, Docker 可以使用 ID 的前面几个字符(如前2个)进行快速操作。

1.检查 Docker 是否安装:

 

docker info

 

docker version 查看版本
2.镜像操作:

1. 检查一下本机有多少 Docker 镜像:docker images

sudo docker images # 显示当前系统镜像,不包括过渡层镜像 
sudo docker images -a # 显示当前系统所有镜像,包括过渡层镜像 
sudo docker images ubuntu # 显示当前系统 docker ubuntu 库中的所有镜像

2.搜索镜像 并 下载镜像

docker search [-s] IMAGE   例子 docker search busybox
sudo docker search -s 100 ubuntu # 查找 star 数至少为 100 的镜像,找出只有官方镜像 start 数超过 100,默认不加 s 选项找出所有相关 ubuntu 镜像 NAME      DESCRIPTION
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
例子1: # docker pull busybox
例子2 : docker pull daocloud.io/library/ubuntu:16.04

下载镜像名称其实由三部分组成:其中其中daocloud.io是注册服务器地址,默认是 registry.hub.docker.com;library 是用户名,默认就是libraray, ubuntu是仓库名,16.04是标签名,默认是latest,第一个busybox 全名该是  registry.hub.docker.com/library/busybox:latest

 

3 给镜像添加标签

docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

[root@bogon ~]#  docker tag daocloud.io/library/ubuntu:16.04 ubuntu:latest

 

4 删除镜像

  • docker rmi [OPTIONS] IMAGE [IMAGE...]

 

 

  • # 按标签删除:多个标签,仅会删除当前标签,不会删除镜像
  • [root@bogon ~]# docker rmi ubuntu:latest
  • # 按ID删除:直接删除镜像
  • [root@bogon ~]# docker rmi 12543ced0f6f

 

选项:

  • -f, --force 强制删除镜像

  • --no-prune 不删除untagged parents

5 导出镜像

docker save [OPTIONS] IMAGE [IMAGE...]
[root@bogon ~]# docker save -o ubuntu_latest.tar ubuntu:latest
[root@bogon ~]# ls -l
-rw-r--r--. 1 root root 128086528 Jun 28 12:39 ubuntu_latest.tar

选项:

  • -o, --output 写入到文件

6 导入镜像

docker load --input ubuntu_latest.tar
# 或者
docker load < ubuntu_latest.tar

选项:

  • -i, --input 从压缩包载入镜像

7上传镜像

docker push [OPTIONS] NAME[:TAG|@DIGEST]

选项:

  • --disable-content-trust=true 跳过镜像签名

sudo docker push 192.168.0.100:5000/ubuntu # 推送镜像库到私有源[可注册 docker 官方账户,推送到官方自有账户] 
sudo docker push 192.168.0.100:5000/ubuntu:14.04 # 推送指定镜像到私有源 

8获取 Docker 镜像的更改历史

docker history <image-name>

9创建镜像 :build

在前面我们讲过 Dockerfile,不过一直没有告诉大家怎么用自己的 Dockerfile 生成镜像文件。
那么现在也不算晚:使用 build 命令生成 image。

docker build [OPTIONS] PATH | URL | - 

PATH / URL / - 这三个是必选选项,三选一。

OPTION 可以使用 docker build --help 进行查看,主要包括构建过程中的内存限制、CPU 限制、以及其他。有两个必须需要了解的 OPTION 选项:

  1. -f: 和make一样,如果PATH内找不到Dockerfile,则docker build必须要 -f 参数指定Dockerfile
  2. -t: 指定生成的镜像标签。例如: docker build -f Dockerfile -t tag .,使用 Dockerfile.bak 生成一个标签叫 tag的镜像文件。

另外,- 选项非常有意思:docker build -,然后可以交互式地输入 Dockerfile,输入完毕后按 Ctrl+D 结束输入即可。

OPTION 一定要放在 PATH / URL / - 前面。


10 load & save  :
使用 load 从 stdin 导入一个 tar 格式的镜像或者仓库,用 save 将 tar 镜像输出到 stdout。
docker save -o <output file> <image>或者docker save <image> > <output file>
docker load -i <file name>或者docker load < <file name>
这两个命令常用于多节点部署。
例如:
命令 # docker save -o a.tar suse
命令 # docker load -i a.tar

load import pull 的区别 ?save export push 的区别?
1 save 和export是 生成镜像的方式:
load 和save 操作的是镜像 ,import 和 export 操作的是容器,结果是 export 出的镜像文件 比 save 的文件 要少一些镜像层。commit 是生成镜像的命令可以指定用以上哪种方式生成。
2 pull push 是传输镜像的方式, 操作的也是镜像,做的只是上传下载。

stop 和 kill 的区别?
docker stop,支持“优雅退出”。先发送SIGTERM信号,在一段时间之后(10s)再发送SIGKILL信号。Docker内部的应用程序可以接收SIGTERM信号,然后做一些“退出前工作”,比如保存状态、处理当前请求等。
docker kill,发送SIGKILL信号,应用程序直接退出

 

3容器操作:

docker create   容器名或者容器ID    创建容器
docker start [-i] 容器名                     启动容器
docker run        容器名或者容器ID    运行容器,相当于docker create + docker start
docker attach   容器名或者容器ID    进入容器的命令行
docker restart   容器名或者容器ID                 重启容器
docker rm         容器名                               删除容器
      -----  Docker 的文件是放在 /var/lib/docker/containers/ 中的

docker pause 容器名    暂停容器

docker unpause 容器名   取消暂停容器
docker kill 容器名docker top 容器名
docker stop      容器名                             停止容器
docker stop,支持“优雅退出”。先发送SIGTERM信号,在一段时间之后(10s)再发送SIGKILL信号。Docker内部的应用程序可以接收SIGTERM信号,然后做一些“退出前工作”,比如保存状态、处理当前请求等。
docker kill,发送SIGKILL信号,应用程序直接退出。
docker inspect 容器名 查看Docker的底层信
docker logs 容器名
docker diff 容器名
docker commit 容器名 镜像名    : 保存容器为镜像

docker top <CONTAINER_ID>:查看容器中运行的进程

docker  port 容器名     # 查看映射端口对应的容器内部源端口


1. 运行镜像

Docker run IMAGE [COMMOND] [ARG...] 在新的容器中执行命令
docker run -t -i -c 100 -m 512MB -h test1 -d --name="docker_test1" ubuntu /bin/bash 
# 创建一个 cpu 优先级为 100,内存限制 512MB,主机名为 test1,名为 docker_test1 后台运行 bash 的容器

例子:
docker run busybox /bin/echo Hello Docker

这条命令是运行 busybox 镜像中的 /bin/echo 命令,参数是 Hello Docker

结果:Hello Docker

 

  • 一些常用的选项:

  • -i 交互式界面,默认是false

  • -t 伪终端,默认false

  • --name 容器别名,默认随机命名

exit 退出交互式界面,容器停止运行。Crtl+P 或者 Crtl+Q 退出交互式界面,容器在后台运行。(注意是大写P和Q)

1.1容器运行之后我们可以查看容器:
 
  1. docker ps 查看正在运行的容器
  2. docker ps -a 查看所有容器
  3. docker ps -l 查看最近一次运行的容器

删除容器时,容器必须是停止状态,否则会报错误。

 1.2运行守护进程

docker run -d IMAGE [COMMOND] [ARG...]
  1. docker run -d --name d1 ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done;"
  2. b89b9ce64d34bd202a642c8190428f4776f15e882f138949259722f22120201a
返回一个守护进程ID,在后台,以 busybox 镜像为基础创建的一个容器,每一秒运行一次 echo hello world命令

2. 查询容器的输出内容:

docker logs [-f] [-t] [--tail] 容器名或id 查看容器内WEB应用程序日志
  • -f --follow=true|false,默认false,一直跟随log变化

  • -t --timestamps=true|false,默认false,加上时间戳

  • --tail="all",返回最新多少条日志

  • 例子   :docker logs -f -t --tail 2 b89b9ce64d34

在运行的容器中启动新的进程:

docker exec [-d] [-i] [-t] 容器名 [COMMOND] [ARG...]

 

 

 

 

Docker容器进入的4种方式

在使用Docker创建了容器之后,大家比较关心的就是如何进入该容器了,其实进入Docker容器有好几多种方式,这里我们就讲一下常用的几种进入Docker容器的方法。

进入Docker容器比较常见的几种做法如下:

  • 使用docker attach
  • 使用SSH
  • 使用nsenter
  • 使用exec

一、使用docker attach进入Docker容器

  Docker提供了attach命令来进入Docker容器。

  接下来我们创建一个守护态的Docker容器,然后使用docker attach命令进入该容器。

  1. $ sudo docker run -itd ubuntu:14.04 /bin/bash  

  然后我们使用docker ps查看到该容器信息,接下来就使用docker attach进入该容器

  1. $ sudo docker attach 44fc0f0582d9 

  可以看到我们已经进入到该容器中了。 

  但在,使用该命令有一个问题。当多个窗口同时使用该命令进入该容器时,所有的窗口都会同步显示。如果有一个窗口阻塞了,那么其他窗口也无法再进行操作。

因为这个原因,所以docker attach命令不太适合于生产环境,平时自己开发应用时可以使用该命令。

二、使用SSH进入Docker容器

  在生产环境中排除了使用docker attach命令进入容器之后,相信大家第一个想到的就是ssh。在镜像(或容器)中安装SSH Server,这样就能保证多人进入

容器且相互之间不受干扰了,相信大家在当前的生产环境中(没有使用Docker的情况)也是这样做的。但是使用了Docker容器之后不建议使用ssh进入到Docker容

器内。关于为什么不建议使用,请参考如下文章:

为什么不需要在 Docker 容器中运行 sshd

三、使用nsenter进入Docker容器

  在上面两种方式都不适合的情况下,还有一种比较方便的方法,即使用nsenter进入Docker容器。关于什么是nsenter请参考如下文章:

https://github.com/jpetazzo/nsenter

在了解了什么是nsenter之后,系统默认将我们需要的nsenter安装到主机中

如果没有安装的话,按下面步骤安装即可(注意是主机而非容器或镜像)

具体的安装命令如下:

  1. $ wget https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz  
  2. $ tar -xzvf util-linux-2.24.tar.gz  
  3. $ cd util-linux-2.24/  
  4. $ ./configure --without-ncurses  
  5. $ make nsenter  
  6. $ sudo cp nsenter /usr/local/bin  

安装好nsenter之后可以查看一下该命令的使用。

 

  nsenter可以访问另一个进程的名称空间。所以为了连接到某个容器我们还需要获取该容器的第一个进程的PID。可以使用docker inspect命令来拿到该PID。

docker inspect命令使用如下:

  1. $ sudo docker inspect --help   

inspect命令可以分层级显示一个镜像或容器的信息。比如我们当前有一个正在运行的容器

可以使用docker inspect来查看该容器的详细信息。

  1. $ sudo docker inspect 44fc0f0582d9  

由其该信息非常多,此处只截取了其中一部分进行展示。如果要显示该容器第一个进行的PID可以使用如下方式

  1. $ sudo docker inspect -f {{.State.Pid}} 44fc0f0582d9  

 

在拿到该进程PID之后我们就可以使用nsenter命令访问该容器了。

  1. $ sudo nsenter --target 3326 --mount --uts --ipc --net --pid  
  1. $ sudo nsenter --target 3326 --mount --uts --ipc --net --pid  

其中的3326即刚才拿到的进程的PID

当然,如果你认为每次都输入那么多参数太麻烦的话,网上也有许多做好的脚本供大家使用。

地址如下:

http://yeasy.gitbooks.io/docker_practice/content/container/enter.html

http://www.tuicool.com/articles/eYnUBrR

 

四、使用docker exec进入Docker容器

  除了上面几种做法之外,docker在1.3.X版本之后还提供了一个新的命令exec用于进入容器,这种方式相对更简单一些,下面我们来看一下该命令的使用:

  1. $ sudo docker exec --help   

 

接下来我们使用该命令进入一个已经在运行的容器

  1. $ sudo docker ps  
  2. $ sudo docker exec -it 775c7c9ee1e1 /bin/bash  

4 删除镜像

 

在docker里运行ubuntu桌面版gui:

 

Run Ubuntu Linux in Docker with Desktop Environment and VNC

https://computingforgeeks.com/run-ubuntu-linux-in-docker-with-desktop-environment-and-vnc/

Using a Docker image, you can run Ubuntu with LXDE and LXQt desktop environments and access it via an HTML5 VNC interface. This is even more fascinating since you can make several other configurations using the docker image such as:

  • HTTP Base Authentication
  • SSL encryption
  • Default Desktop User setup
  • Screen Resolution adjustment
  • Generate Dockerfile from jinja template
  • Sound (Preview version and Linux only)
  • Deploy to a subdirectory (relative url root)

Furthermore, there are several Ubuntu versions and flavors that include:

 
  • focal: Ubuntu 20.04 (latest)
  • focal-lxqt: Ubuntu 20.04 LXQt
  • bionic: Ubuntu 18.04
  • bionic-lxqt: Ubuntu 18.04 LXQt
  • xenial: Ubuntu 16.04 (deprecated)
  • trusty: Ubuntu 14.04 (deprecated)

Let’s dive in!

Step 1 – Install Docker on your System

To be able to run the Ubuntu Docker image, of course, you need the Docker engine installed on your system. Below is a dedicated guide that can help you install the Docker engine on any Linux distribution.

Start and enable Docker to run automatically on system boot.

sudo systemctl start docker && sudo systemctl enable docker

Add your system user to the Docker group to be able to execute Docker commands without sudo.

sudo usermod -aG docker $USER
newgrp docker

Verify the installed Docker version.

$ docker version
Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:48 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
....

Step 2 – Pull the Ubuntu Docker Image

As said earlier, there are several images for Ubuntu that can be pulled. These images are available on the Docker page and can be pulled using Docker as shown.

##For Ubuntu 20.04 (latest)
docker pull dorowu/ubuntu-desktop-lxde-vnc:latest

##For Focal with LXDE
docker pull dorowu/ubuntu-desktop-lxde-vnc:focal

##For Focal with LXQt
docker pull dorowu/ubuntu-desktop-lxde-vnc:focal-lxqt

##For Bionic with LXDE
docker pull dorowu/ubuntu-desktop-lxde-vnc:bionic

##For Bionic with LXQt
docker pull dorowu/ubuntu-desktop-lxde-vnc:bionic-lxqt

##For Trusty 
docker pull dorowu/ubuntu-desktop-lxde-vnc:trusty

##For Xenial
docker pull dorowu/ubuntu-desktop-lxde-vnc:xenial

Once the desired Ubuntu image has been pulled, check if it is available in the local registry.

$ docker images
REPOSITORY                       TAG       IMAGE ID       CREATED         SIZE
dorowu/ubuntu-desktop-lxde-vnc   latest    1a89db715923   10 months ago   1.32GB

Step 3 – Run the Ubuntu Container with Desktop Environment

Here, we will go through several methods with configuration on how you can run the Ubuntu container to give you an intuitive experience.

Quick Start.

You can simply run the container and access it via port 6080 using the command below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

In the above command, we have set the container name to ubuntu_desktop and a persistent volume at /dev/shm Remember to replace ubuntu-desktop-lxde-vnc with the appropriate image pulled.

Check if the container is running with the port exposed.

$ docker ps
CONTAINER ID   IMAGE                            COMMAND         CREATED          STATUS                             PORTS                                   NAMES
f8fd69e3865e   dorowu/ubuntu-desktop-lxde-vnc   "/startup.sh"   17 seconds ago   Up 15 seconds (health: starting)   0.0.0.0:6080->80/tcp, :::6080->80/tcp   ubuntu_desktop

As seen from the output, we have port 6080 exposed. Allow it through the firewall and proceed to access the Ubuntu Desktop using the URL http://IP_Address:6080/.

Set HTTP Base Authentication

You can also set a password to be used when accessing the HTTP page on port 6080 by adding the HTTP_PASSWORD variable to the above command:

docker run -d \
  --name ubuntu_desktop \
  -e HTTP_PASSWORD=Passw0rd \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

Now accessing the page, you will be required to provide a password.

Enable VNC Viewer

The VNC viewer is accessed via a separate port 5900 that needs to be exposed in order to access it. The VNC Viewer port can be exposed as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  -p 5900:5900 \
  dorowu/ubuntu-desktop-lxde-vnc

Here, the VNC viewer should be available on port 5900.

You can as well set a password for the viewer by adding the VNC_PASSWORD variable to the above command:

docker run -d \
  --name ubuntu_desktop \
  -e VNC_PASSWORD=StrongPassword \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  -p 5900:5900 \
  dorowu/ubuntu-desktop-lxde-vnc

When accessing the VNC viewer you will be required to provide the set password to access the VNC.

Encrypt with SSL.

You can generate SSL certificates for encryption. In this guide, we will generate self-signed certificates as below.

Ensure that OpenSSL is installed on your system before you proceed:

mkdir -p ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ssl/nginx.key -out ssl/nginx.crt

Now when running the container, you need to specify the SSL_PORT and the path to the generated certificate as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6081:443 \
  -e SSL_PORT=443 \
  -v ${PWD}/ssl:/etc/nginx/ssl \
  dorowu/ubuntu-desktop-lxde-vnc

Here, you can access the Ubuntu Desktop using the URL https://IP_address:6081.

Set the Default Desktop User

When running the container, the default user is root. However, you can add another user with the USER and PASSWORD variables as below:

docker run -d \
  --name ubuntu_desktop \
  -e USER=thor \
  -e PASSWORD=Passw0rd \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

Now here, we will have another user thor with password as Passw0rd added.

Deploy into a subdirectory

The container can be deployed into a subdirectory and serve as a prefix when accessing the page. For example, here we will specify the URL subdirectory as new.

Now specify the subdirectory using the RELATIVE_URL_ROOT environment variable.

docker run -d \
  --name ubuntu_desktop \
  -e RELATIVE_URL_ROOT=new \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

Remember the directory should not have any leading and trailing slash (/). Now you can access the Ubuntu Desktop using the URL http://IP_address:6080/new/

Set the Screen Resolution.

Normally the virtual desktop will automatically adjust to the browser window size when connecting to the server. But you can set a fixed resolution using the variable RESOLUTION as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  -e RESOLUTION=1360x768 \
  dorowu/ubuntu-desktop-lxde-vnc

You will have the virtual desktop with 1360×768(16:9) resolution set.

Enable Sound

Begin by inserting the kernel module below on your Linux system

sudo modprobe snd-aloop index=2

Now run the container with sound enabled as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  --device /dev/snd \
  -e ALSADEV=hw:2,0 \
  dorowu/ubuntu-desktop-lxde-vnc

In the above command, –device /dev/snd -e ALSADEV=hw:2,0 grants sound to the container and sets the basic ASLA device config to use card 2. Now you will have your container with sound enabled. Test this by playing a sound/video.

Step 4 – Manage the Container

YOu can easily stop and start the Ubuntu container using the commands:

##To stop
docker stop ubuntu_desktop

##To start
docker start ubuntu_desktop

##To delete
docker rm ubuntu_desktop

Futhermore, you can manage the container as a system service by creating the service file below.

sudo vim /etc/systemd/system/ubuntu-container.service

In the file, add the lines:

[Unit]
Description=Ubuntu container

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a ubuntu_desktop
ExecStop=/usr/bin/docker stop -t 2 ubuntu_desktop

[Install]
WantedBy=local.target

Now reload the system daemon.

sudo systemctl daemon-reload

Start and enable the container to run automatically on boot.

sudo systemctl start ubuntu-container
sudo systemctl enable ubuntu-container

Check the status of the service.

 

posted @ 2018-07-21 14:51  微信公众号--共鸣圈  阅读(445)  评论(0)    收藏  举报