cbsch.no


Docker CLI notes



Notes on Docker CLI

Creating a container

docker run -d -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=testPassword --name postgres-test postgres
Parameter Description
-d Deamonize (run in background)
-p 127.0.0.1:5432:5432 Forward port to container local-port:container-port or local-ip:local-port:container-port
-e POSTGRES_PASSWORD=testPassword Set an environment variable
--name postgres-test Name the container
postgres The image to create the container from, always the last argument

Remove a container

Multiple container names can be specified separated by spaces

docker rm --force postgres-test another-container

Pipe input into container

Parameter Description
-i Makes exec accept stdin (-i for interactive)
--user postgres Specific to the postgres container, but is required to connect to the database
postgres-test The container name
psql The cli program to execute inside the container

Powershell:

@"
CREATE TABLE test (
    id SERIAL PRIMARY KEY
    ,name TEXT NOT NULL
);
"@ | docker exec -i --user postgres postgres-test psql

Bash:

docker exec -i --user postgres postgres-test psql <<-EOSQL
CREATE TABLE test (
    id SERIAL PRIMARY KEY
    ,name TEXT NOT NULL
);
EOSQL