Skip to content

Instantly share code, notes, and snippets.

@fdocr
Last active December 17, 2018 17:47
Show Gist options
  • Select an option

  • Save fdocr/5388c55a223d1cddfcfccfdd2532c9a8 to your computer and use it in GitHub Desktop.

Select an option

Save fdocr/5388c55a223d1cddfcfccfdd2532c9a8 to your computer and use it in GitHub Desktop.
Common Docker commands & images. Includes Swagger, PostgreSQL & Redis

Common Docker parameters explained

  • -it
    • Execute using "interactive tty", i.e. execute the command and hold the session until the command ends (with possible keyboard input).
  • -p 8080:3000
    • Bind the port 3000 from the container to the local network interface on port 8080. The port that is closer to the container image name (see below) is the one that refers to the container port, the other one refers to the local network port.
  • -d
    • Run the container as daemon (instead of interactively with -it).
  • -v /opt/postgres/data:/var/lib/postgresql/data
    • Attach a local volume to a container for data persistance. Follow the same logic as with the ports to know which one refers to the container and which one to the local filesystem.
  • -e DATABASE_URL=postgres://user:[email protected]:5432/database
    • Sets an ENV variable so it's available inside the container

SWAGGER CONTAINER

Run swagger editor locally on port 8080

docker run -it --rm --name swagger-editor -p 8080:8080 swaggerapi/swagger-editor

REDIS CONTAINER

Run redis locally on port 6379

docker run --name my-redis -d -p 6379:6379 redis

POSTGRES CONTAINER

Create volume for persistance

docker volume create pgdata

Create the container

docker run --name pg -e POSTGRES_DB=test -e POSTGRES_USER=test -e POSTGRES_PASSWORD=test -d -v pgdata:/var/lib/postgresql/data -p 5432:5432 postgres

Access using psql

docker exec -it pg psql -U postgres

PSQL COMMANDS

List databases

\l

Connect to database

\c db_name

Describe all relations (tables) in the DB

\dt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment