Getting started with Docker
In this article I’ll take you through what docker is and a simple use case to demonstrate how to use it. What is docker and why have you been seeing it here and there ? Docker is one of the most popular open-sourced container runtime tools that help to build, test, and run containers. It is both a container system and a company. To use docker you need an understanding of what containers are. Containers make it easier for developers to create, run, and deploy applications on different hardware and platforms, very quickly and easily. An effective solution to the “it works on my computer” problem. In summary containers just bundle together an application with its dependencies.
A Docker image is the set of instructions for creating a container. The image typically includes a file system and the parameters that will be used for the container. Docker containers are created from Docker images — a container is a runnable instance of an image. Note that since the image is a set of instructions for creating a container, multiple containers can be created from the same image.
From here on, you will be using the Docker containers, and you will do that by first installing Docker Desktop (or simply Docker) on your own machine. You can find installer here, on the official Docker documentation page. Once your installation is done, you need to restart your pc and start the docker application.
We start by doing things the easy way (my favorite), which is to use dockerhub. DockerHub is the world’s largest registry of Docker images available. Think GitHub for docker images. DockerHub is the default registry for Docker. It contains images ready to run a great variety of applications. Lets start by testing out postgres which is a database.
Run the following command in your terminal (started as admin) to download the latest Postgres image to your machine:
docker pull postgres:latestRun the image using the docker run command, set a password for Postgres where you see “password!”:
docker run --name psql -e POSTGRES_PASSWORD=password! -p 5433:5432 -d postgres:latestNote: If you have Postgres already installed and running locally, then use a different local port other than 5432, such as 5433.
Understanding the command above:
- The
--nameflag allows you to specify a name for the container that can be used later to reference the container. If you don’t specify a name, Docker will assign a random string name to the container. - The
-eflag stands for “environment”. This sets the environment variablePOSTGRES_PASSWORDto the valuepassword!. - The
-pflag stands for “publish”. This allows you to bind your local machine’s port 5433 to the container port 5432. - The
-dstands for “detach”. This tells Docker run the indicated image in the background and print the container ID. When you use this command, you will still be able to use the terminal to run other commands, otherwise, you would need to open a new terminal.
Now that the Postgres client has been installed and is running in the container, you can connect to the database using the following command:
psql -h 127.0.0.1 -p 5433 -U postgresThis command allows you to access the database using the same port that you exposed earlier. Note that after running that command you will need to enter the same password that you set with the POSTGRES_PASSWORD when creating the container.
Get Stanley Ocran’s stories in your inbox
Join Medium for free to get updates from this writer.
Test the database with some psql commands. For example,
- List all existing databases using
\lcommand. - List all tables using the
\dtcommand. - More commands can be found in the Postgres documentation here.
- When you are finished testing Postgres, you can quit your connection to Postgres using
\q
To stop the running Docker container, you will first need to find it. You can list the running containers with the command docker ps. Copy the id of your Postgres container. Then, You can stop a particular container using its ID.
#List all containers
docker ps --all
# Stop a container
docker stop <container_ID>
# Remove
docker container rm <container_ID>You can also view the images in your local system, and remove any one:
# List all images
docker image ls
# Remove
docker image rm <image_ID>We have come to the end of this article, I hope you know more about docker now and are more confident about getting into its use.








