John Siu Blog

Tech - Business Tool, Personal Toys

Docker Gogs - How To

☰ Table of Content

Setup Gogs using official docker image.

Image

Docker Hub: gogs/gogs1

Mappings

HostInside ContainerUsage
$MY_GOGS_DIR/dataGogs persistent storage(configuration, data, log)
$MY_GOGS_SSH_PORT22/tcpGogs ssh listening port
$MY_GOGS_WEB_PORT3000/tcpGogs http listening port

$MY_GOGS_DIR, $MY_GOGS_SSH_PORT and $MY_GOGS_WEB_PORT will be used in compose section below.

Preparation

$MY_GOGS_DIR

Create a directory for Gogs persistent storage. We will use /var/lib/my_gogs as our Gogs persistent storage.

1
mkdir -p /var/lib/my_gogs

In this case $MY_GOGS_DIR=/var/lib/my_gogs, you can create the directory in other location.

$MY_GOGS_SSH_PORT

Gogs uses standard ssh port 22 inside container. We will map it to 22222 in the host for this example.

In this case $MY_GOGS_SSH_PORT=22222, you can use other port as long as it does not conflict with other services running.

NOTE: Do not map to port 22 on your host. That will likely conflict with your host ssh.

$MY_GOGS_WEB_PORT

Gogs web interface uses port 3000 inside container. We will map it to port 3000 on the host.

In this case $MY_GOGS_WEB_PORT=3000, again, you can use other port as long as it does not conflict with other services running.

Preparation Summary

VariableValue
$MY_GOGS_DIR/var/lib/my_gogs
$MY_GOGS_SSH_PORT22222
$MY_GOGS_WEB_PORT3000

Setup

Testing

Following is the format using variables mentioned above:

1
2
3
4
5
6
docker run \
--rm \
-v ${MY_GOGS_DIR}:/data \
-p ${MY_GOGS_SSH}:22 \
-p ${MY_GOGS_WEB}:3000 \
gogs/gogs
docker run optionUsage
–rmAutomatically remove the container when it exits
-v <source path in host>:<target path in container>Map a path(file/dir) from host to a path in container
-p <host port>:<container port>Map a port from host to a port in container

Let just plug in all the values manually for now:

1
docker run --rm -v /var/lib/my_gogs:/data -p 3000:3000 -p 22222:22 gogs/gogs

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
usermod: no changes
Aug 31 17:52:27 syslogd started: BusyBox v1.30.1
Aug 31 17:52:27 sshd[28]: Server listening on :: port 22.
Aug 31 17:52:27 sshd[28]: Server listening on 0.0.0.0 port 22.
2019/08/31 17:52:27 [ WARN] Custom config '/data/gogs/conf/app.ini' not found, ignore this if you're running first time
2019/08/31 17:52:27 [TRACE] Custom path: /data/gogs
2019/08/31 17:52:27 [TRACE] Log path: /app/gogs/log
2019/08/31 17:52:27 [TRACE] Build Time: 2019-08-12 02:17:33 UTC
2019/08/31 17:52:27 [TRACE] Build Git Hash: c154721f4a8f3e24d2f6fb61e74b4b64529255c2
2019/08/31 17:52:27 [TRACE] Log Mode: Console (Trace)
2019/08/31 17:52:27 [ INFO] Gogs 0.11.91.0811
2019/08/31 17:52:27 [ INFO] Cache Service Enabled
2019/08/31 17:52:27 [ INFO] Session Service Enabled
2019/08/31 17:52:27 [ INFO] SQLite3 Supported
2019/08/31 17:52:27 [ INFO] Run Mode: Development
2019/08/31 17:52:27 [ INFO] Listen: http://0.0.0.0:3000

If you check /var/lib/my_gogs, sub-directories are created:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/var/lib/my_gogs/
├── git/
├── gogs/
│   ├── conf/
│   ├── data/
│   └── log/
└── ssh/
    ├── ssh_host_dsa_key
    ├── ssh_host_dsa_key.pub
    ├── ssh_host_ecdsa_key
    ├── ssh_host_ecdsa_key.pub
    ├── ssh_host_ed25519_key
    ├── ssh_host_ed25519_key.pub
    ├── ssh_host_rsa_key
    └── ssh_host_rsa_key.pub

Keep it running for now and go to next step.

Configuration

Open your browser to http://<hostname/ip>:3000 and fill out the form as follow:

Install

Once you click Install Gogs:

Done

The config file is located at ${MY_GOGS_DIR}/gogs/conf/app.ini. In this example the path will be /var/lib/my_gogs/gogs/conf/app.ini.

Most of the values should not be changed except ROOT_URL in [server] section:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
...

[server]
DOMAIN           = localhost
HTTP_PORT        = 3000
ROOT_URL         = http://localhost:3000/ # Change this according to your environment.
DISABLE_SSH      = false
SSH_PORT         = 22
START_SSH_SERVER = false
OFFLINE_MODE     = false

...

The ROOT_URL should be the URL used in your browser. For example, if you put a nginx front proxy with https at standard port 443, then ROOT_URL will be https://<hostname>/.

After updating app.ini, stop the container with ctrl-c and run it again. The output will be shorter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
usermod: no changes
Aug 31 18:46:24 syslogd started: BusyBox v1.30.1
Aug 31 18:46:24 sshd[28]: Server listening on :: port 22.
Aug 31 18:46:24 sshd[28]: Server listening on 0.0.0.0 port 22.
2019/08/31 18:46:24 [TRACE] Custom path: /data/gogs
2019/08/31 18:46:24 [TRACE] Log path: /app/gogs/log
2019/08/31 18:46:24 [TRACE] Build Time: 2019-08-12 02:17:33 UTC
2019/08/31 18:46:24 [TRACE] Build Git Hash: c154721f4a8f3e24d2f6fb61e74b4b64529255c2
2019/08/31 18:46:24 [TRACE] Log Mode: File (Info)
2019/08/31 18:46:24 [ INFO] Gogs 0.11.91.0811

Stop it with ctrl-c.


Compose

Gogs configuration is done. But we don’t want to enter the long train of options every time restarting Gogs or after server reboot. This is where docker-compose comes in.

Let create the two necessary files below.

.env

Create /var/lib/my_gogs/.env:

1
2
3
4
MY_GOGS_TAG=latest
MY_GOGS_DIR=/var/lib/my_gogs
MY_GOGS_SSH_PORT=22222
MY_GOGS_WEB_PORT=3000

MY_GOGS_TAG: You can use specific version other than latest. List of valid version is in gogs/gogs docker tags page2.

docker-compose.yml

Create /var/lib/my_gogs/docker-compose.yml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
version: '3'
services:
  gogs:
    image: gogs/gogs:${MY_GOGS_TAG}
    ports:
      - "${MY_GOGS_SSH_PORT}:22"
      - "${MY_GOGS_WEB_PORT}:3000"
    volumes:
      - ${MY_GOGS_DIR}:/data
    restart: always

Start

1
2
cd /var/lib/my_gogs
docker-compose up -d
docker-compose command/OptionUsage
upcreate and start container
-ddaemon/run in background

When docker-compose3 is executed, it automatically look for two default files in current directory: docker-compose-yml4 and .env5.

It will use variables in .env as environment variables. The .env file must be present in the current working folder.

Currently there is no command line option to specify an alternative .env file.

Variables already set in shell and command line will override .env.

It will create containers(s) base on docker-compose.yml.

-f can specify one or more compose file, other than the default.

Output:

1
2
Starting gogs_gogs_1 ...
Starting gogs_gogs_1 ... done

Status

Check status with ps

1
2
cd /var/lib/my_gogs/
docker-compose ps

Output:

1
2
3
   Name                  Command               State                       Ports
----------------------------------------------------------------------------------------------------
gogs_gogs_1   /app/gogs/docker/start.sh  ...   Up      0.0.0.0:22222->22/tcp, 0.0.0.0:3000->3000/tcp

Stop

1
2
cd /var/lib/my_gogs/
docker-compose stop

Output:

1
Stopping gogs_gogs_1 ... done

Now Gogs is ready!

John Siu

Update: 2020-08-28
comments powered by Disqus