Not logged in. Login

Project Docker Deployment

The project in this course may be deployed on Docker containers managed by Docker Compose.

Getting Set Up: your computer

You should be able to use any OS on your computer to set up the Docker and Docker Compose.

  1. Install Docker. You can do this in Windows, OSX, or Linux.
  2. Install Docker Compose. You can also do this in Windows, OSX, or Linux.
  3. Get the template code for the project from the project template repository.

Working with Containers

The docker-compose command will do most of the management of Docker containers we use.

  • docker-compose build: Build any images from Dockerfiles referenced in your docker-compose.yml.
  • docker-compose up: Start all of the containers described in your docker-compose.yml. Press control-C to stop them.
  • docker-compose stop: Stop any containers described in your docker-compose.yml.
  • docker-compose rm: Destroy any containers described in your docker-compose.yml.
  • docker system prune: Remove any unused containers and images.
  • docker-compose run app bash: Run a command (bash) in a new instance of the container (app).

Building Container Images

Docker Compose is a tool for managing Docker container images. As an example, in this fragment of a docker-compose.yml:

      context: .
      dockerfile: ruby.Dockerfile

This creates a container with hostname ruby (the first line) that is built from the Dockerfile ruby.Dockerfile in the same directory (last lines).

Dockerfiles let you describe container images that contain whatever software you need to get things done. As a brief example to see what happens:

FROM ruby:2.7
COPY . .
RUN gem install bunny
CMD ruby rpc_server.rb

This Dockerfile describes a container image that starts with the Ruby container from Docker Hub (FROM); changes to the /app directory (WORKDIR, similar to the Unix command cd); copies the current directory (everything in the directory containing the Dockerfile) to /app in the container image (COPY); runs a gem command as part of building the image (RUN). Finally, it sets the command that will run when the container starts (CMD).

The template code provides a simple Dockerfile for many programming languages that are at least capable of running the "hello world" program that's also in the repo.

If you need extra Debian/Ubuntu packages installed in a container, there is an example RUN block in the c.Dockerfile provided. (The example installs C/C++ tools, but could be adapted to install any other packages.)

Compiling and Starting Services

If you have compilation steps that have to happen before a particular container can run, those should be done as part of the image build process.

You can add RUN lines to execute any Linux command as part of building the container image.

If you have a server process that needs to be started (and will, presumably run as long as the container exists), the command to start it should be the CMD command for the container image.

Ports and Servers

As part of defining the containers in the docker-compose.yml file, you have given each one a hostname (the first line in the docker-compose.yml service stanza: generally the programming language name in my template, but could be anything).

Within the Docker private network, all services you start will be accessible from other containers by default.

For example, if you have an HTTP service in a contained named "rust", listening on port 8080, other containers can access it at http://rust:8080/.

If you would like to access that server from your own computer (perhaps in a web browser), you can expose that port in the docker-compose.yml like this:

      context: .
      dockerfile: rust.Dockerfile
      - ""

Then that service will be accessible at http://localhost:8080/.

Updated Wed April 07 2021, 12:59 by ggbaker.