More Examples
Usually the scripts of the containers are kept in git repositories,
not in a local directory (like /var/test/
that we have used so
far). Let's try some of the simple scripts that are located at
https://gitlab.com/docker-scripts
1. Get the scripts
Let's try to build a debian container. We can get the scripts for the
container with the command ds pull
:
ds pull debian
ls /opt/docker-scripts/debian/
As you can see from the output, ds pull
is basically a git clone of
https://gitlab.com/docker-scripts/debian to the directory
/opt/docker-scripts/
. The command ds
(without arguments) shows some
information about the general configuration and setup of
docker-scripts:
ds
The environment variable DSDIR
tells DS where to look for
configurations. It loads $DSDIR/config.sh
, which contains these
settings:
-
REPO
-- the base URL for the script repos -
SCRIPTS
-- the default directory where the scripts are saved -
CONTAINERS
-- the default directory where the containers are created
These can be customized if needed, but usually the defaults are OK.
Let's have a look at the scripts that we just downloaded:
cd /opt/docker-scripts/debian/
tree
nano Dockerfile
nano settings.sh
nano cmd/config.sh
nano inject/setup.sh
2. Initialize a directory for the container
cd
ds init debian @server1
Because the directory of the container (@server1
) does not start
with ./
or ../
or /
, it is interpreted as a subdirectory of
$CONTAINERS
, which is set on ~/.ds/config.sh
and has as default
value /var/ds/
.
cd /var/ds/server1/
ls
nano settings.sh
So, initialization just copies settings.sh
on the directory of the
container and sets some values for IMAGE
and CONTAINER
. The
default value for IMAGE
is the name of the scripts' directory, and
the default value for CONTAINER
is the name of the container's
directory. You may change them if you wish, but the default values are
usually OK.
3. Build and test the container
ds make
Besides starting the container, it also runs any configuration or setup scripts.
Docker uses cached layers of images and sometimes they may be stale and create some problems. To build the image from scratch (ignoring any caches) and then start the container do:
ds build --no-cache
ds make
ls
ssh -p 2201 -i sshkey root@localhost
The port 2201
on the localhost
is forwarded to the port 22
of the
container, so we end up inside the container. With this key we can
also login to the container remotely (from outside the server):
ssh -p 2201 -i sshkey root@server1.user1.fs.al
Isn't it a bit strange that you did not have to open the port 2201
on the firewall, in order to access it? As a matter of fact, Docker
bypasses the firewall, by default. So, any port that you forward from
the host to a container is immediately open and available to the whole
world, regardless of the firewall on your host. Keep this in mind and
be careful.
4. Build another container
ds init debian @server2
cd /var/ds/server2/
ls
nano settings.sh
Let's modify the forwarded port, so that it does not conflict with the port that is forwarded to the other container:
PORTS="2202:22"
ds make
ls
ssh -p 2202 -i sshkey root@localhost
5. Build an ubuntu container
The scripts for a simple ubuntu container are almost identical to
those for a simple debian container, except that the Dockerfile
has
the line include(noble)
instead of include(bookworm)
.
-
Get the scripts:
ds pull ubuntu
-
Initialize a container directory:
ds init ubuntu @server3
-
Change the port on
settings.sh
:cd /var/ds/server3/
sed -i settings.sh \
-e '/PORTS=/ c PORTS="2203:22"'
nano settings.sh -
Build the container:
ds make
-
Test it:
ls
ssh -p 2203 -i sshkey root@localhost
cat /etc/os-release
exit
6. Build other containers
6.1 Use a branch of the scripts
The command ds pull
can also have a branch argument:
ds pull
Let's use the branch bullseye
of the scripts' repo this time.
-
Get the scripts:
ds pull debian bullseye
-
Initialize the container:
ds init debian-bullseye @server4
-
Customize
settings.sh
:cd /var/ds/server4/
sed -i settings.sh \
-e '/PORTS=/ c PORTS="2204:22"'
nano settings.sh -
Build the container:
ds make
-
Test it:
ssh -p 2204 -i sshkey root@localhost ls
ssh -p 2204 -i sshkey root@localhost cat /etc/os-release
6.2 Get the scripts with git clone
This time let's get the scripts with git clone
instead of ds pull
.
-
Get the branch
jammy
of the ubuntu repo:git clone -b jammy \
https://gitlab.com/docker-scripts/ubuntu \
/opt/docker-scripts/test/jammy -
Initialize the container:
ds init test/jammy @server5
-
Customize
settings.sh
:cd /var/ds/server5/
sed -i settings.sh \
-e '/PORTS=/ c PORTS="2205:22"'
nano settings.sh -
Build the container:
ds make
-
Test it:
ssh -p 2205 -i sshkey root@localhost ls
ssh -p 2205 -i sshkey root@localhost \
cat /etc/os-release
7. Clean up
Let's remove all the test containers:
echo /var/ds/server[1-5]
for dir in /var/ds/server[1-5]; do
cd $dir
ds remove
done
rm -rf /var/ds/server[1-5]
cd /opt/docker-scripts/
rm -rf debian debian-bullseye test ubuntu
docker system prune