I suggest you get machine to follow along this part. So here’s a little plug of our own: Cloud VPS, for as little as €2.95 a month.
This article is a part of our complete series of articles on Docker. Click here to access the Free Series.
Back to the article now…
We have to use “docker” on the CLI to run commands pertaining to docker.
The best way to get started is to use “docker –help” , you will immediately be bombarded with all the commands you can run using docker.
It can feel over whelming at first, but not to sweat by the end of these tutorials you will be familiar with a lot of them and on your way to deep dive even further.
At the end of this you can see: docker COMMAND –help
This command in particular will help us understand other commands.
Let’s say we wanted to understand more about “docker image”, we will run “docker image –help”
You will see this output:
Basics of working with Images and Containers
Now we will download and work with an image for postgres database.
Checking if we have the required image:
When you ran the docker command “docker image –help”, you should’ve seen the ls command.
So lets use it and check if we have a postgres image with us.
This is what your output must be looking like, if you have followed the previous article for Docker Installation.
However we do not see the postgres image.
Pulling the required Image:
Since we are trying to install postgres, this is the command you will run:
docker image pull postgres
General Command : docker image pull < REQUIRED IMAGE NAME>
If you are thinking that the image is being is pulled out of thin air, well first, congratulations for not loosing your innocence and sense of wonder. Secondly, please go through the Docker Architecture article to understand the workings.
Lets check if the image is now in our system:
Gathering more information related to the image:
So you just downloaded an image, and you ofcourse need to understand more about it before running it.
For this purpose we have the inspect command:
docker image inspect <IMAGE ID>
Note: Image ID is available by running docker image ls, like previous step.
You will be greeted with a “Novel” about the image in json format.
Not everything is important, only pay attention to the main characters for now.
Environment Variable that are defined are present as “Env”
Command that is performed when we start the container
In the Config section you can find the info like Exposed Port where you can find the service provided by this image.
In this case Postgres will be running on 5432 port.
Starting a Container from Image
Now that we have our image on our box, let us create a container out of it.
Starting a container requires you to use the run command.
Running the command with “–help” will fetch you the Options that you can use.
For now let’s try to run the container by using the run command:
docker container run postgres
We get an error.
Now this error is specific to postgres, as we are trying to run a database service, we need to specify a password for the superuser, trust all connections.
So lets give a password:
docker container run -e POSTGRES_PASSWORD=postgres postgres
Press ‘Ctrl+C’ to exit the container, you will see this:
Doing a “docker container ls -a”, you will notice that the container is in exited state.
So how do we run a container and not let it hinder our usual process, or run a container in the background?
If you had noticed the “docker container run” command has an option “-d” this runs a detached container.
So lets try that:
docker container run -e POSTGRES_PASSWORD=postgres -d postgres
Now you have a container running in the background!
But the fun doesn’t end here, we will now discuss, how to access a container in the next tutorial!