Skip to content

Docker cheat sheet

Gabriel Araujo edited this page Jul 27, 2022 · 5 revisions

Docker cheat sheet

Remarks

  • Docker containers will exit if no process is running inside it

  • If you are running something that needs input you have make the container --interactive and attach the --tty

    e.g: docker run -it ruby:3.1.2-alpine vi

  • Containers are meant to be disposable and won't persist any changes made inside it (Reference: https://docs.docker.com/storage)

Running containers

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

e.g: docker run -it --rm ruby:3.1.2-alpine sh

You can use docker run --help to get the full OPTIONS list, but here are some important ones:

-i, --interactive          Keep STDIN open even if not attached
-t, --tty                  Allocate a pseudo-TTY
--rm                       Automatically remove the container when it exits
--mount                    Attach a filesystem mount to the container

-d, --detach               Run container in background and print container ID
-p, --publish list         Publish a container's port(s) to the host
--name string              Assign a name to the container
-w, --workdir string       Working directory inside the container

Starting a container with a volume

Reference: https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag

  • with --mount syntax: docker run -it --rm --mount type=bind,src=$(PWD),dst=/usr/src/app ruby:3.1.2-alpine sh
  • with -v syntax: docker run -it --rm -v $PWD:/usr/src/app ruby:3.1.2-alpine sh

Listing images and containers

  • docker image ls List images
  • docker ps Shows only running containers
  • docker ps -a Shows all containers

Stopping running containers

  • You can use docker stop CONTAINER [CONTAINER...] to stop one or more containers.
  • You can use their names or IDs to do so.

e.g:

CONTAINER ID   IMAGE                        COMMAND                  CREATED       STATUS         PORTS                NAMES
dc9cc882459c   docker/getting-started:pwd   "nginx -g 'daemon of…"   2 hours ago   Up 2 seconds   0.0.0.0:80->80/tcp   unruffled_saha

To stop it we can use either docker stop dc9cc882459c or docker stop unruffled_saha

Creating a dockerized Rails API

  1. Create a Dockerfile
  • Add the FROM with the base image you want to use (in our case ruby:3.1.2-alpine)
  • Set the WORKDIR to /usr/src/app
    • It can be anything you want, but I like to use the linux filesystem hierarchy Ref 1 | Ref 2
  • We will iterate over this Dockerfile and add what is needed step by step, for now we can just build the image with docker build -t ruby-project .
  1. Start a container with volume make sure you use your newly built image ruby-project and that you run the command sh to start a new shell session inside the container.
  2. Generate a Gemfile by running bundle init
  • Edit the Gemfile and uncomment the line with # gem rails
  • Run bundle install

You will probably get an error like this:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.

But this one we already know how to solve. (Reference: https://renehernandez.io/snippets/install-development-tools-in-alpine/)

  1. Run rails new --help and check the description of all the available options
  • Run rails new . --api -d postgresql --minimal to create the application files
-d, [--database=DATABASE]     # Preconfigure for selected database (options: mysql/postgresql/sqlite3/oracle/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
[--api], [--no-api]           # Preconfigure smaller stack for API only apps
[--minimal], [--no-minimal]   # Preconfigure a minimal rails app
  • You will get an error saying that git isn't installed, and that's correct because alpine doesn't have it out of the box, let's add it to our Dockerfile rebuild the image and try bundle install again.
  • You should get yet another error, and this one is about postgres. But know you now the drill, check the error message, update the Dockerfile with the required library, rebuild the image, and retry bundle install
Can't find the 'libpq-fe.h header
*****************************************************************************
Unable to find PostgreSQL client library.

Draft:

  • Update dockerfile with COPY commands, copy Gemfile and Gemfile.lock
  • RUN bundle install
  • COPY the source code into the container
  • Add the CMD to run the puma server
  • Fix issue
None of the paths included in TZInfo::DataSources::ZoneinfoDataSource.search_path are valid zoneinfo directories. (TZInfo::DataSources::ZoneinfoDirectoryNotFound)
  • Fix issue
Error loading shared library ld-linux-aarch64.so.1
  • Expose port 3000 when running container (check the run options with docker run --help)
  • Fix issue by creating our docker compose file
connection to server on socket "/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?