Start with Docker
Before I embark on the road to explain docker, I want to define a few concepts. In the docker world, we have two structures: images and containers. At the base level, you can download (pull) an image from the hub.docker.com, and use it to build a container.
Pull a Image:
docker pull postgres- You will see something like this on your command line:
Using default tag: latest
latest: Pulling from library/postgres
Digest: sha256:ce0f6c28b5869ff166714d5ff9702e38b00f81ad348c6
- This will provide an image on your local machine for use with building a container.
View Local Image:
- You can run the
docker imagecommand to see this image on your local system: docker image ls | grep postgres
Build a Container:
- From here we can create a container with the
docker runcommand: docker run --name postgres_cont --rm -e POSTGRES_PASSWORD=password -d postgres--namewill name my container.--rmtells my container to delete the container when it stops running.- Otherwise, my system will potentially build up a whole bunch of containers that use the same image.
-ewill pass environmental values to my container.- In this case, Docker Hub's explanation for the postgres container says that the
POSTGRES_PASSWORDis required for this image.
- In this case, Docker Hub's explanation for the postgres container says that the
-dtells the container to run in the background.postgrestells my container which image to use.
- Now, I have a container named 'postgres_cont' on my local machine.
docker container ls | grep postgres_cont
Start and Connect to a Container:
- I can start the container with a run command and then connect to a shell session by running a command similar to this:
docker run --name postgres_cont --rm -it -e POSTGRES_PASSWORD=password postgres bash-itsays to start an interactive terminal that stays open.bashtells my container to run the bash shell.- In some cases, the image will not be based off of an image that includes bash, so you would have to use
/bin/shinstead. - From inside this shell we could then interact with or database via
psql.
- In some cases, the image will not be based off of an image that includes bash, so you would have to use
- We could also start an interactive shell session inside the container directly with the postgres terminal:
docker run --name postgres_cont --rm -it -e POSTGRES_PASSWORD=password postgres psql -U postgrespostgres psql -U postgresis saying when you attach to this container runpsqland connect with thepostgresuser.
Connect to a Running Container:
- I can connect to a running container by running the
execcommand.- This container could have been started by using
runwith the-dflag or it could have been started by runningdocker-compose up.
- This container could have been started by using
docker exec -it postgres_cont bash-itsays to start an interactive terminal that stays open.postgres_contis my container name.bashis the process I want to use when I attach to my container.- This will give me an interactive terminal session within the container where I can run postgres commands.
Connect to the Container from the Host:
- If I want to connect to my container from my host machine, I will need to expose the container's port to my host.
docker run --name postgres_cont -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres- Now instead of having to connect to my running container via the command line, I can use any postgres GUI and connect to my database from my host machine on port 5432.