Docker helps us to manage the services of our applications (Databases or another external service). We can use to create isolated systems in our machine, this means a service doesn't interfere in other services or in our server. The services in our machine are called by images, images are services available to us, containers are instances of these services, are isolated of the rest of our server, containers expose one port to communicate with our machine, we communicate with a container mapping one port of our machine to one port of container, then we have a way to communicate with container.
Firstly we define which service we use on our application, in this case I will use Postgres. Searching for Postgres Image on google we find the site called by https://hub.docker.com/_/postgres , there you can find a more detailed tutorial, but basically is possible to download a docker image with the following command:
docker run --name database -e POSTGRES_PASSWORD=docker -p 5432:5432 -d postgres
Analizing each pice of the comand above, we got:
- docker run ⇒ Create and start a new container with the specify image.
- —name database ⇒ Setting a name to our container.
- -e POSTGRES_PASSWORD=docker ⇒ Setting environment variables, postgres_password is required for you to use the PostgreSQL image.
- -p 5432:5432 ⇒ Mapping [Host port] : [Container port]
- -d ⇒ Start container in background
- postgres ⇒ Name of the image
After creating our container, how if all is working as we expected? The command docker ps, show all containers running at the moment. If you want to see all containers instead only running you do it using docker ps -a.