5.2. Run a Docker Container#
In the 5.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)/data:/app/data
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.
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
$ chmod 777 ~/dokuwiki
$ docker run -it --rm \
-p 8080:8080 \
-e DOKUWIKI_PASSWORD=student \
-e DOKUWIKI_USERNAME=student \
-v $HOME/dokuwiki:/bitnami/dokuwiki \
bitnami/dokuwiki:latest
Error
This resuts in a permission denied error in Cloud Shell Editor. Fix the permissions like this:
$ chmod 777 $HOME/dokuwiki
Cleanup#
It’s not necessary but you you want to get rid of Dokuwiki’s data:
$ sudo rm -rf ~/dokuwiki/
The docker container runs as root
. Files it creates will be owned by root
.