Home Explore Blog Models CI



docker

3rd chunk of `content/guides/nodejs/develop.md`
34533150309a650a2f64083767aea38234a1af3075914f4b000000010000103b
   # If you need more help, visit the Docker Compose reference guide at
   # https://docs.docker.com/go/compose-spec-reference/

   # Here the instructions define your application as a service called "server".
   # This service is built from the Dockerfile in the current directory.
   # You can add other services your application may depend on here, such as a
   # database or a cache. For examples, see the Awesome Compose repository:
   # https://github.com/docker/awesome-compose
   services:
     server:
       build:
         context: .
       environment:
         NODE_ENV: production
         POSTGRES_HOST: db
         POSTGRES_USER: postgres
         POSTGRES_PASSWORD_FILE: /run/secrets/db-password
         POSTGRES_DB: example
       ports:
         - 3000:3000

       # The commented out section below is an example of how to define a PostgreSQL
       # database that your application can use. `depends_on` tells Docker Compose to
       # start the database before your application. The `db-data` volume persists the
       # database data between container restarts. The `db-password` secret is used
       # to set the database password. You must create `db/password.txt` and add
       # a password of your choosing to it before running `docker compose up`.

       depends_on:
         db:
           condition: service_healthy
       secrets:
         - db-password
     db:
       image: postgres
       restart: always
       user: postgres
       secrets:
         - db-password
       volumes:
         - db-data:/var/lib/postgresql/data
       environment:
         - POSTGRES_DB=example
         - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
       expose:
         - 5432
       healthcheck:
         test: ["CMD", "pg_isready"]
         interval: 10s
         timeout: 5s
         retries: 5
   volumes:
     db-data:
   secrets:
     db-password:
       file: db/password.txt
   ```

6. In the `docker-nodejs-sample` directory, create a directory named `db`.
7. In the `db` directory, create a file named `password.txt`. This file will
   contain your database password.

   You should now have at least the following contents in your
   `docker-nodejs-sample` directory.

   ```text
   ├── docker-nodejs-sample/
   │ ├── db/
   │ │ └── password.txt
   │ ├── spec/
   │ ├── src/
   │ ├── .dockerignore
   │ ├── .gitignore
   │ ├── compose.yaml
   │ ├── Dockerfile
   │ ├── package-lock.json
   │ ├── package.json
   │ └── README.md
   ```

8. Open the `password.txt` file in an IDE or text editor, and specify a password
   of your choice. Your password must be on a single line with no additional
   lines. Ensure that the file doesn't contain any newline characters or other
   hidden characters.
9. Ensure that you save your changes to all the files that you have modified.
10. Run the following command to start your application.

    ```console
    $ docker compose up --build
    ```

11. Open a browser and verify that the application is running at
    [http://localhost:3000](http://localhost:3000).
12. Add some items to the todo list to test data persistence.
13. After adding some items to the todo list, press `ctrl+c` in the terminal to
    stop your application.
14. In the terminal, run `docker compose rm` to remove your containers.

    ```console
    $ docker compose rm
    ```

15. Run `docker compose up` to run your application again.

    ```console
    $ docker compose up --build
    ```

16. Refresh [http://localhost:3000](http://localhost:3000) in your browser and verify that the todo items persisted, even after the containers were removed and ran again.

## Configure and run a development container

You can use a bind mount to mount your source code into the container. The container can then see the changes you make to the code immediately, as soon as you save a file. This means that you can run processes, like nodemon, in the container that watch for filesystem changes and respond to them. To learn more about bind mounts, see [Storage overview](/manuals/engine/storage/_index.md).

Title: Creating Database Password and Running the Application
Summary
This section guides you through creating the `db/password.txt` file, specifying a password, and running the application using `docker compose up --build`. It also covers verifying the application is running at `http://localhost:3000`, testing data persistence, stopping the application, removing the containers, and running the application again to ensure the todo items persist. Finally, it touches on using bind mounts for development containers.