Time Scale DB
Time Scale DB
com/2018/04/19/docker-introduction-tutorial/
Docker is kind of like git in a sense that there are only a few commands
to learn to for it be useful. This tutorial focuses on the docker commands
that I find myself most commonly use. This article assumes that you have
a general idea of what docker is and have it installed and set up already.
Hopefully, it serves as handy reference or cheatsheet for useful common
docker commands.
Summary
First, we’ll run through the lifecycle commands:
The command above starts a docker container and puts you into a bash
command. I then explored around a bit. When I’m jumping into bash shell
to explore, I find myself using these docker run options: --rm -ti. Let’s
cover the options and their purposes:
Option Purpose
–rm Once you exit from the container the docker container will automatically be removed.
Without this option, what ends up happening after playing with a few containers is that
you’ll end up with a bunch of “Exited” containers that show up with docker ps -a.
It’s easy to remove the Exited containers but the --rm will automatically clean house for
you.
To exit out of the docker container bash shell. Just run exit or hit ctrl-
D like you normally would.
Option Purpose
-P -P is short for the --publish-all option. This means docker published any EXPOSE
port with the docker image automatically so you can access the port from the host. If
apache is listening to port 80, docker will map it to a random port in the ephemeral
range.
-d -d is short for the --detach option. Docker detaches the container process and run it in
the background.
Notice that the run command with the -d flag returned the docker
container id and runs the container in the background. We’ll see the short
version of the container in the next command.
docker ps
Leave the docker container running above in the background. You can
check for running docker containers with the docker ps command:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
a2b93575b021 boltops/docker-tutorial "/usr/sbin/httpd -DF…" 14 seconds ago
Up 14 seconds 0.0.0.0:32768->80/tcp mystifying_yonath
Notice, that the first column contains a short version of the container id:
a2b93575b021 A user-friendly version of the container is also provided.
It’s mystifying_yonath in this case. The container id or name is used for
the next command.
docker exec
The next command is a cool command: docker exec. Docker exec is sort
of like ‘ssh-ing’ into the container. Let’s “jump” into the dettached
container running in the background and explore again.
docker stop
For background containers, you usually must stop them before you can
remove them. Here’s how you stop them.
$ docker rm mystifying_yonath
Error response from daemon: You cannot remove a running container
a2b93575b0215405dce221d6004ed34d46530fa6d956600241de0fe38f5f7a55. Stop the container
before attempting removal or force remove
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
a2b93575b021 boltops/docker-tutorial "/usr/sbin/httpd -DF…" 8 minutes ago
Up 8 minutes 0.0.0.0:32768->80/tcp mystifying_yonath
$ docker stop mystifying_yonath
mystifying_yonath
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
$
docker rm
Thus far, we started our containers with the --rm option. This was nice
because then we do not have to manually clean up stopped containers. If
you do not use the --rm option, we would have to clean up the stopped
or Exited containers.
docker start
You can start back up stopped containers with docker start. You likely
won’t be using this as much as docker run:
That covers the basics of the docker lifecycle commands. A really cool
thing once you know these basics is now you can pretty much use any
docker container on DockerHub or other registries and play with them.
This is useful if you want to quickly test different Linux distros like
ubuntu, centos, or amazonlinux:2 and throw them away. Welcome to the
power of Docker 😁 Remember the words of wisdom from Spiderman:
“With Great Power Comes Great Responsibility.”
docker build
docker images
docker push
To build a docker image, we’ll grab the same example we’ve been using.
Here are the summarized commands:
Wrap Up
Okay, I hope that this basic introduction to Docker has helped ease the
learning curve. Like git it takes only knowing a few of common
commands to add another powerful tool to your toolbelt.
docker --help
docker run --help
https://betterprogramming.pub/connect-from-local-machine-to-postgresql-docker-container-
f785f00461a7
This command will start a PostgreSQL database and map ports using
the following pattern: -p <host_port>:<container_port>.
Port 5432 of our container will be mapped on port 5432 of our host or
server.
Now you are ‘inside’ your container. We can access postgres and create
the database.
root@cb9222b1f718:/# psql -U postgres
psql (10.3 (Debian 10.3-1.pgdg90+1))
Type "help" for help.postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=#\q
We are finished. You can exit your container (\q) and go to your local
machine. Here you need some PostgreSQL Client tool installed:
PSQL (CLI)
PgAdmin
…
After authenticating you will see the mytestdb is in the list of available
databases. Now you can connect with your database using \c .
For a tool like PgAdmin you can define your connection. Also, here you
have to replace localhost with your server IP if your container is
running elsewhere.
Save the connection and you can connect to the database which is
running in your PostgreSQL Docker container!
This post was based on a popular SO answer I gave. I hope it helps!
===========================
Tutorial TimescaleDB - Sources
===========================
https://techexpert.tips/timescaledb/timescaledb-docker-installation/
===========================
Tutorial TimescaleDB - Docker Installation
===========================
#Download the TimescaleDB docker image from the online repository.
docker pull timescale/timescaledb:latest-pg14
===========================
Tutorial TimescaleDB - New Docker container start
===========================
#Start a new TimescaleDB container using this Docker image.
#When you create a new container using the docker run command, by default you also create a new
data volume.
docker run -d --name timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=password
timescale/timescaledb:latest-pg14
or
#If you want to access the container from the host but avoid exposing it to the outside world, you can
bind to 127.0.0.1 instead of the public interface
docker run -d --name timescaledb -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=password
timescale/timescaledb:latest-pg14
or
docker run -d --name timescaledb-server -p 5432:5432 \
-e "POSTGRES_PASSWORD=kamisama123" timescale/timescaledb:latest-pg12
# If you want to store the data from your Docker container in a host directory, or you want to run the
Docker image on top of an existing data directory, you can specify the directory to mount a data
volume using the -v flag. For example:
docker run -d --name timescaledb -p 5432:5432 -v /your/data/dir:/var/lib/postgresql/data -e
POSTGRES_PASSWORD=password timescale/timescaledb:latest-pg14
#When you remove a Docker container with docker rm the data volume persists on disk until you
explicitly delete it.
docker run --rm -P --name pg_test eg_postgresql
===========================
Tutorial TimescaleDB - connect to TimescaleDB container using psql
===========================
#connect using the version of psql that is bundled within the container
docker exec -it timescaledb psql -U postgres
===========================
Tutorial TimescaleDB - Docker container management
===========================
#List the Docker images installed on your system.
docker images
#You can use the docker volume ls command to list existing docker volumes.
docker volume ls
#Verify the status of all Docker conteiners using the following command:
docker ps -a
#Verify the status of the TimescaleDB container using the ID or its name.
docker ps -a -f id=db51dbe186b1d63c72d1cfc38464467279bf99df456435528e8a2462e6d02578
docker ps -a -f name=timescaledb-server
docker ps -a -f name=timescaledb
#To stop the TimescaleDB container service, use the following command:
docker container stop db51dbe186b1d63c72d1cfc38464467279bf99df456435528e8a2462e6d02578
docker container stop timescaledb-server
docker container stop timescaledb
#To start the TimescaleDB container service, use the following command:
docker container start db51dbe186b1d63c72d1cfc38464467279bf99df456435528e8a2462e6d02578
docker container start timescaledb-server
#To restart the TimescaleDB container service, use the following command:
docker container restart db51dbe186b1d63c72d1cfc38464467279bf99df456435528e8a2462e6d02578
docker container restart timescaledb-server
#In case of error, use the following command to verify the TimescaleDB container logs.
docker logs db51dbe186b1d63c72d1cfc38464467279bf99df456435528e8a2462e6d02578
docker logs timescaledb-server
#Run the following command to remove Docker container: Where Container_ID is the Docker
container ID.
docker stop <Container_ID>
docker rm <Container_ID>
#Run the following command to remove the Docker image:Where Image_name is the Docker image
name.
docker rmi -f <Image_name>
===========================
Tutorial TimescaleDB - TimescaleDB
===========================
#Access the container on your host or server using bash
docker exec -it timescaledb bash
#to exit bash
exit
#access the postgresql container from your local computer/you can replace "localhost " with the ip
address 192.168.99.100
psql -h localhost -p 5432 -U postgres -W
# create a db
CREATE DATABASE mytestdb;
===========================
Tutorial TimescaleDB -
===========================
===========================
Tutorial TimescaleDB -
===========================
Existing containers can be started again while retaining their volumes and data
docker start