The Basic Knowledge About Docker.
To find all containers and images:
[parallels@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[parallels@localhost ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mono latest 2ba93b1033ba 2 weeks ago 690MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
To read information about docker daemon.
[parallels@localhost ~]$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2019-12-12 09:52:35 CST; 38min ago
Docs: https://docs.docker.com
Main PID: 1405 (dockerd)
Memory: 132.9M
CGroup: /system.slice/docker.service
└─1405 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Dec 12 09:52:34 localhost.localdomain dockerd[1405]: time="2019-12-12T09:52:34.585770862+08:00" level=info msg="Default bridge (docker0) is assigne...address"
To remove container:
docker rm <container_id>
To remove image:
docker rmi <image_id>
To use container's shell and get interaction operations, for example, we go to mono's environment.
[parallels@localhost ~]$ docker run -t -i mono
root@b5e7dc5f546d:/#
root@b5e7dc5f546d:/#
We can add options -d
for docker run
to make program run in the background.
Compile CSharp File And Run Exe
The following csharp code comes from getting started with mono on docker
[parallels@localhost Downloads]$ ls sieve/
sieve.cs
sieve.cs
using System;
namespace sieve
{
class Program
{
const int HiPrime=1000;
static readonly bool[] Primes = new bool[HiPrime];//by default they're all false
private static void Main(string[] args)
{
for (var i = 2; i < HiPrime; i++)
{
Primes[i] = true;//set all potential primes true
}
for (var j = 2; j < HiPrime; j++)
{
if (!Primes[j]) continue;
for (long p = 2; (p * j) < HiPrime; p++)
{
Primes[p * j] = false;
}
}
for (var index = 2; index < Primes.Length; index++)
{
if (Primes[index]) Console.WriteLine(index);
}
}
}
}
We create Dockerfile to build and run it.
FROM mono:latest
Add ./sieve /root
RUN mcs /root/sieve.cs
CMD [ "mono", "/root/sieve.exe" ]
[parallels@localhost Downloads]$ docker build -t sieve .
Sending build context to Docker daemon 73.61MB
Step 1/4 : FROM mono:latest
---> 2ba93b1033ba
Step 2/4 : Add ./sieve /root
---> 5dc811407db4
Step 3/4 : RUN mcs /root/sieve.cs
---> Running in f937a7285615
Removing intermediate container f937a7285615
---> 5b1d31ad54c4
Step 4/4 : CMD [ "mono", "/root/sieve.exe" ]
---> Running in 1d3f6b206e95
Removing intermediate container 1d3f6b206e95
---> e28c4c1b513f
Successfully built e28c4c1b513f
Successfully tagged sieve:latest
[parallels@localhost Downloads]$ sudo docker run sieve
[sudo] password for parallels:
2
3
5
7
11
13
17
19
23
29
31
37
The image sieve can be executed multiple times and generated different container.