-
Notifications
You must be signed in to change notification settings - Fork 0
Docker cheat sheet
-
Docker containers will exit if no process is running inside it
-
If you are running something that needs input you have make the container
--interactiveand attach the--ttye.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)
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
Reference: https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag
- with
--mountsyntax:docker run -it --rm --mount type=bind,src=$(PWD),dst=/usr/src/app ruby:3.1.2-alpine sh - with
-vsyntax:docker run -it --rm -v $PWD:/usr/src/app ruby:3.1.2-alpine sh
-
docker image lsList images -
docker psShows only running containers -
docker ps -aShows all 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
- Create a Dockerfile
- Add the
FROMwith the base image you want to use (in our caseruby:3.1.2-alpine) - Set the
WORKDIRto/usr/src/app - 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 .
-
Start a container with volume make sure you use your newly built image
ruby-projectand that you run the commandshto start a new shell session inside the container. - Generate a
Gemfileby runningbundle init
- Edit the
Gemfileand 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/)
- Run
rails new --helpand check the description of all the available options
- Run
rails new . --api -d postgresql --minimalto 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 installagain. - 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?