# How to run Tryton using Docker

**URL:** https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200
**Category:** System Administrator
**Tags:** docker
**Created:** [September 9, 2020, 6:26pm UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200 "2020-09-09T18:26:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![dave](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://discuss.tryton.org/u/dave)
#### Post date: [September 9, 2020, 6:26pm UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200/1 "2020-09-09T18:26:30Z")

</div>

# Introduction

There are a few different ways of getting Tryton running on your system. One of these is by using [Docker](https://www.docker.com). The aim here is to help you get a basic Tryton system up and running using Docker, even if you have never used it before, and to teach you a bit about it along the way.

If you are familiar with Docker and know what you are doing, then the [instructions on the tryton/tryton docker image](https://hub.docker.com/r/tryton/tryton) are much shorter and will let you try out Tryton in seconds. These instructions are much longer, provide a slightly different setup, and try to explain each step in detail.

As Tryton has a [three tier architecture](https://en.wikipedia.org/wiki/Multitier_architecture#Three-tier_architecture) there are several different components that you need in order to get a functioning Tryton system. This turns out to be a good fit for Docker as Docker is designed to run applications that are made up of multiple different components.

# Install Docker

None of this is possible without having Docker installed on your system. So the very first thing you are going to need to do is install Docker. How you do this will depend on what operating system you are using. Fortunately the [official Docker Documentation](https://docs.docker.com/get-docker/) can help you here.

For linux you may also want to [run the Linux post-installation steps](https://docs.docker.com/engine/install/linux-postinstall/) so you don’t need to preface the `docker` commands with `sudo`.

Once you have got docker installed and running you will be ready for the next stage.

## Docker Concepts

If you have never used Docker before, then some of the terminology and ideas behind it may seem a bit strange. However, if you can grasp a few of the key concepts it should help you understand what is going on.

- Container  
Fundamentally a Docker container is just a running program. Once it has been created, you can use Docker to start, pause, unpause and stop it. Each container is isolated from the host it is running on, and all the other containers on the system. It also has its own private filesystem, which is initially provided by a Docker image. Containers are often treated as disposable. Extra setup is required to keep any data a container generates because when a container is removed all its data is lost. You can run as many containers as you like that are based on the same Docker image.

- Image  
A Docker image provides the filesystem that is used when starting a Docker container. It contains all the binaries, code, runtimes, dependencies and data needed by a program. Each image is made up from multiple readonly layers. The files in each layer add to or replace those in the layers below them. When a container is started using the image the container adds a final read/write layer on top of the image. Any changes done by the container only happen in this read/write layer.

# Create a Docker Bridge Network

As the Docker containers are isolated from each other, you need to setup a way for them to communicate so they can do useful things. Just like normal computers, the standard way this is done in Docker is over a network. Docker provides commands that create and manage what are called _user-defined bridge networks_. Any containers that are attached to the same user-defined bridge network will be able to talk to each other.

Create a user-defined bridge network called `tryton`, for use with your Tryton containers, by running:

```sh
docker network create tryton

```

# Run the Database Server

Conceptually in the three tier architecture this is the bottom most tier, and is where the majority of the data in your Tryton system is stored. For production systems Tryton currently supports the use of [PostgreSQL](https://www.postgresql.org/) as the database server.

As a side note, something that sometimes confuses people is the use of the term database to both mean:

- the related collection of structured data that is stored together, and
- the program that manipulates and manages access to this data.

In this document I will try to refer to the former as the database, and the later as the database server. So, a database server may contain multiple different databases.

## Create a Docker Volume for the Database

As described previously, Docker containers are often treated as disposable. To ensure your data doesn’t vanish if you if you replace your PostgreSQL container you can use a Docker volume.

```sh
docker volume create tryton-database

```

This command creates a Docker volume called `tryton-database` which can then be mounted inside a container. Any data stored in the volume by the container will still exist even after the container it was mounted in has gone.

## Generate a Secure Password

To secure your data ideally you should generate a strong password for use with your database. You will also need to remember this password, because you will need it to restart your Tryton system if it gets removed. Using a [password manager can help you do this](https://www.ncsc.gov.uk/collection/top-tips-for-staying-secure-online/password-managers).

You will then need to replace the _${POSTGRES\_PASSWORD}_ in the commands below with the password you generated. Alternatively you can set the password in your environment, and the commands will pick it up from there:

```sh
export POSTGRES_PASSWORD='your_top_secret_password'

```

## Start the PostgreSQL Container

In order to be able to run PostgreSQL in a container you need a PostgreSQL Docker image. This is provided by the officially supported [postgres](https://hub.docker.com/_/postgres) image on Docker Hub.

By default, if you try and use a Docker image, and you don’t have it on your system, then Docker will automatically attempt to download it (or _pull it_ in Docker terminology) from Docker Hub for you.

So, you should now be ready to start the database server using this command (remembering to replace _${POSTGRES\_PASSWORD}_, if required):

```sh
docker run \
    --name tryton-postgres \
    --env PGDATA=/var/lib/postgresql/data/pgdata \
    --env POSTGRES_DB=tryton \
    --env POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \
    --mount source=tryton-database,target=/var/lib/postgresql/data \
    --network tryton \
    --detach \
    postgres

```

What this command does is:

- starts a new Docker container (line 1) based on the latest postgres Docker image (line 9)
- names the container `tryton-postgres` (line 2), and
- attaches the container to the `tryton` network (line 7)
- tells PostgreSQL to create a database called `tryton` (line 4)
- it sets the username and password needed to connect to `postgres` (default) and `your_top_secret_password` (line 5)
- inside the container it mounts the `tryton-database` volume at `/var/lib/postgresql/data` (line 6), and
- tells PostgreSQL to store the database in `/var/lib/postgresql/data/pgdata` (line 3), so its data is stored in the Docker volume, and finally
- detaches your terminal’s standard input, output and error from the container so you get the command prompt back (line 8).

**Note:** If you want to run a specific version of PostgreSQL, so you can control when you upgrade, then you need to specify which one using a tag along with the image name, e.g.: for version 12 use `postgres:12` instead of `postgres`.

# Initialize the Database

Now that you have your database server running you can initialize the database so it is ready for use. This command can take a few seconds to run, and near the end you will be prompted to enter the email address and password you want to use for the `admin` user (once again don’t forget to replace _${POSTGRES\_PASSWORD}_, if required).

```sh
docker run \
    --env DB_HOSTNAME=tryton-postgres \
    --env DB_PASSWORD="${POSTGRES_PASSWORD}" \
    --network tryton \
    --interactive \
    --tty \
    --rm \
    tryton/tryton \
    trytond-admin -d tryton --all

```

What this command does is:

- starts a new docker container (line 1) based on the latest tryton Docker image (line 8), and
- attaches the container to the `tryton` network (line 4), and
- in the container runs the `trytond-admin` command against the `tryton` database and initializes and updates everything using the `--all` option (line 9)
- it ensures the correct database server is used (line 2) with the correct login username (default - `postgres`) and password (line 3)
- allocates a pseudo-TTY (line 6) and keeps the terminal’s standard input connected to the container (line 5), so you can enter in the `admin` user’s email address and password, and
- once done removes the container (line 7) because it isn’t needed anymore

**Note:** If you want to run Tryton from a specific series, so you can control when you upgrade, then you need to specify the series along with the image name. So for series 5.6 use `tryton/tryton:5.6` instead of `tryton/tryton`.

# Run the Tryton Server

The Tryton server forms the middle tier in the three tier architecture and provides the business logic. Tryton’s advanced modularity allows you to select the correct business logic for your requirements by activating only the modules that you intend to use.

## Create a Docker Volume for the Data

Although most of the data in your Tryton system is stored in the database, there may be some stored on the filesystem. Tryton normally stores attachments outside the database, and can be configured to store other things like copies of Sales Invoices there too. To make sure this data doesn’t disappear if you if you replace your Tryton container you can use a Docker volume.

```sh
docker volume create tryton-data

```

This volume needs to mounted inside any Tryton containers that get created.

## Start the Tryton Server Container

Everything should now be ready for you to start your Tryton server. This can be done with this command (ensure you replace _${POSTGRES\_PASSWORD}_, if required):

```sh
docker run \
    --name tryton \
    --env DB_HOSTNAME=tryton-postgres \
    --env DB_PASSWORD="${POSTGRES_PASSWORD}" \
    --mount source=tryton-data,target=/var/lib/trytond/db \
    --network tryton \
    --publish 127.0.0.1:8000:8000 \
    --detach \
    tryton/tryton

```

What this command does is:

- starts a new docker container (line 1) based on the latest tryton Docker image (line 9), and
- names the container `tryton` (line 2), and
- attaches the container to the `tryton` network (line 6)
- it ensures the correct database server is used (line 3) with the correct login username (default - `postgres`) and password (line 4)
- inside the container it mounts the `tryton-data` volume at `/var/lib/tryton/db` (line 5) so any documents Tryton saves to the filesystem won’t get lost, and
- allows you to connect to your Tryton server by forwarding connections to `127.0.0.1:8000` to the container’s port `8000` (line 7)
- detaches your terminal’s standard input, output and error from the container so you get the command prompt back (line 8).

**Note:** you should make sure that the series of Tryton that you are using is the same as the one you used when you initialized the database. So for series 5.6 use `tryton/tryton:5.6` instead of `tryton/tryton`.

**Note:** using this command you will only be able to connect to your Tryton server from the computer it is running on. If you want to be able to connect to it over the network you need to change `127.0.0.1:8000:8000` to `8000:8000`. However, if you do this, then you should also look at [configuring SSL](https://docs.tryton.org/projects/server/en/latest/topics/configuration.html#ssl) or better still running your Tryton server behind a reverse proxy such as [Nginx](https://nginx.org/en/) with SSL enabled, otherwise your private information (such as your password) will cross the network in plain text.

# Connect Using a Client

The client is the top tier in Tryton’s three tier architecture, and there are several clients to choose from.

The Tryton web client is provided in the Tryton Docker image and allows you to easily connect using your web browser. Your Tryton Server is accessed by visiting [http://localhost:8000/](http://localhost:8000/) in your browser. To login you should enter `admin` as the user name and then the password you entered when initializing the database.

If you have the desktop client installed, then you can use this as well. You need to enter `localhost:8000` as the host, `tryton` as the database, and `admin` as the user name.

---

<div class="post-metadata">

### Author: ![DocMors](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/779978/32.png) [@DocMors](https://discuss.tryton.org/u/DocMors)
#### Post date: [December 22, 2020, 1:17pm UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200/2 "2020-12-22T13:17:49Z")

</div>

Hi, this is my first attempt at tryton and my first attempt at docker. The guide above is easy enough to follow but could you please add an additional step? I would like to use an already running PostgreSQL server on our network and I seem to be unable to connect the docker tryton instance with that non docker PostgreSQL server. Thanks Mike

PS if there is a link that explains this in more detail already I haven’t found it.

---

<div class="post-metadata">

### Author: ![dave](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://discuss.tryton.org/u/dave)
#### Post date: [December 23, 2020, 12:09pm UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200/3 "2020-12-23T12:09:13Z")

</div>

It is not something that I have personally tried, but you should be able to change the

```
--env DB_HOSTNAME=tryton-postgres \

```

line to point to your PostgreSQL server by replacing the `tryton-postgres` part with your PostgreSQL server’s domainname or IP address.

You may also need to add `--env` lines to set the `DB_USER`, and `DB_PORT`, depending on how your PostgreSQL server is setup.

Once you can get the [Initialize the Database](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200#initialize-the-database) step to work with the right environment variables, the same environment variables should work when you [Run the Tryton Server](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200#run-the-tryton-server).

Also check you don’t have any firewall rules on either system that are preventing connections between the Docker container and the PostgreSQL server.

---

<div class="post-metadata">

### Author: ![DocMors](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/779978/32.png) [@DocMors](https://discuss.tryton.org/u/DocMors)
#### Post date: [December 23, 2020, 12:44pm UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200/4 "2020-12-23T12:44:56Z")

</div>

Thanks Dave, I’ll try to get it working 🙂

---

<div class="post-metadata">

### Author: ![w7hpwfxro00dcia9lc](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/w/3ab097/32.png) [@w7hpwfxro00dcia9lc](https://discuss.tryton.org/u/w7hpwfxro00dcia9lc)
#### Post date: [February 15, 2021, 8:34pm UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200/5 "2021-02-15T20:34:05Z")

</div>

Key thing with using Docker, this recipe has everything working on its internal software defined network (SDN) called “tryton” and then the user will use these services through the specified ports exposed externally to the SDN.

In order to connect to initialize the external Database Server you need to expose the docker container to the network that this database is hosted on to include the ports required.

This would likely require updating the line:  
`--env DB_HOSTNAME=tryton-postgres`  
TO:  
`--env DB_HOSTNAME=<hostname-of-external-database>`

This would likely require adding the line:  
`--publish <database-port-on-external-database-of-interest>:<database-port-on-internal-contaner-started(likely the same port)`

---

<div class="post-metadata">

### Author: ![dave](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://discuss.tryton.org/u/dave)
#### Post date: [April 19, 2021, 11:55am UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200/6 "2021-04-19T11:55:47Z")

</div>

> [@w7hpwfxro00dcia9lc](#):
>
> In order to connect to initialize the external Database Server you need to expose the docker container to the network that this database is hosted on to include the ports required.

I may have misunderstood what you are trying to explain, but I don’t think this is right.

If you are using a standard docker installation you don’t need to do any additional configuration in order to make outgoing connections from the container. This applies regardless of what network you have connected your container to.

There are some cases (for example, when running docker-machine) where you may have to do some extra work to make outgoing connections work. However this would not involve publishing ports, as published ports are used to allow access _in_ to your containers, not _out_ from them.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [September 23, 2023, 9:17am UTC](https://discuss.tryton.org/t/how-to-run-tryton-using-docker/3200/10 "2023-09-23T09:17:59Z")

</div>

A post was split to a new topic: [Access previous attachment after upgrade on Docker](https://discuss.tryton.org/t/access-previous-attachment-after-upgrade-on-docker/6535)
