4.2. Run a Docker Container#
In the 4.1. Docker Quick Start lab you created and destroyed a container
for Dokuwiki. In this lab we will change the docker run command so that we
can:
Configure environment variables in DokuWiki
Store DokuWiki data permanently on the filesystem
Open network ports to the container
Store Data#
The most common way to store data in Docker is to create a volume mount that
shares a directory in the container with a directory on the host. You can have
as many shared directories as you like using the -v option:
docker run -v /path/on/host:/path/in/container
The /path/on/host MUST be an absolute path. So it’s common to see this
shell trick:
docker run -v $(pwd)/host-path:/app/container-path
Set Environment Variables#
Containers are configured with the environment. Setting variables is simple:
docker run -e ENV_VAR="value"
Network Ports#
In the quickstart we created a container that had exposed ports. The format of
the -p option is:
-p host-port:container-port
So this example connect’s the host’s port 8080 to the container port 80:
-p 8080:80
There can be multiple ports.
User and Group#
Docker container run as root. It might seem counterintuitive that a
container have high privileges, but it’s important to remember that a container
is not a security boundary, it’s for convenience. It’s best to reduce the
privileges of a container with the -u or --user argument. It specifies the
user and group ID of the container. For example:
# Run as user 1000 and group 1000
$ docker run -u 1000:1000 ...
There’s a nice shell trick to make the container run as you:
$ docker run -u $(id -u):$(id -g) ...
Run Configuration for Dokuwiki#
Containers are expendable. When run with the --rm option, the container will
be automatically deleted when it stops. That’s a nice way to not have to clean
up. To run Dokuwiki with durable storage, a pre-set password and an exposed port
run:
$ mkdir ~/dokuwiki
$ docker run -it --rm \
-u $(id -u):$(id -g) \
-p 8080:8080 \
-e DOKUWIKI_PASSWORD=student \
-e DOKUWIKI_USERNAME=student \
-v $HOME/dokuwiki:/storage \
dokuwiki/dokuwiki:stable
Cleanup#
You can delete the container image, but keep the data with docker rm. It’s not
necessary but you you want to get rid of Dokuwiki’s data:
$ sudo rm -rf ~/dokuwiki/