/docker-fedora

Docker files for various microservices built on Fedora. Uses non-root user to run containers with shared volumes.

Primary LanguageShell

Docker Files for Fedora Microservices

This repo contains Docker files for running microservices on top of Fedora. It contains the following:

  • A Node.js container in the fedora-node dir
  • A Mongo database container in the fedora-mongodb dir
  • A PostgreSQL database container in teh fedora-postgresql dir
  • A Docker Compose file in the top level dir that can be used to run an integrated Node.js plus MongoDB microservice environment. It will run two separate Node.js and MongoDB containers in background.

An attempt was made to follow best practices:

  • Containers are always run by a non-root user who controls the microservice, like 'node', 'mongo', or 'postgres'.
  • The non-root user for Node.js will aut matically have the UID of the host user running the container. This will allow the host user to be able to edit any files copied over or created by the container user.

Usage

  • Make sure you have docker installed first.
  • Clone this repo and cd to one of the container dirs
 git clone https://github.com/bobbrady/docker-fedora.git
 cd fedora-node
  • Build the image with the given Dockerfile:
docker build -t <your_name>/fedora-node .
  • Run a container based on the built image. Pass in the -dt switch to run it in background with a pseudo-TTY terminal open. The -t switch will prevent the container from shutting down without having to apply a hack command like tail -f /dev/null to keep it running. You can point to a shared volume if you want to edit files live while the container is running. Note the use of the built-in linux shell command id for automatically finding the UID of the current host user.
docker run -p 3000:3000 -e HOST_USER_ID=$(id -u $USER) -v $PWD/src:/opt/node/src -dt <your_name>/fedora-node
  • View the running container's process status
docker ps -alias
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
d0ba3c1f29c2        bbrady/fedora-node   "/opt/node/entrypoint"   10 seconds ago      Up 8 seconds        0.0.0.0:3000->3000/tcp   small_jones
  • Connect to the container that is running in background via a bash shell. You can sudo as the container microservice user using the gosu command. Now you are ready to execute any of your commands!
docker exec -it small_jones bash
[root@d0ba3c1f29c2 node]# gosu node bash
[node@d0ba3c1f29c2 node]$ ls
entrypoint.sh  package.json  src
[node@d0ba3c1f29c2 node]$
  • If you passed-in your HOST_USER_ID, you can now edit files in the shared volume of the container. Your changes will be seen in the container. The changes will also be persisted in your local file system and survive a deletion of the container.
  • When you are done running a container, you can shut it down and delete its volume:
docker rm -fv <CONTAINER_ID>

or to clean-up all Containers by forcing quit and removing volumes (-fv switch)

docker rm -fv $(docker ps -aq)