This post is a set of personal notes that I took studying Docker.
It contains essential things to know about Docker, that I wrote to help me not to forget the important stuff.
Is it worth learning Docker?
Brilliant question! :) This is the topic of my previous post: Why bother to learn Docker
What is Docker?
Docker is a tool that allows to run applications in isolation from the machine that host them.
To do so it uses concepts like containers, images, volumes and networks.
What is a container?
In short, a container is a minimal linux machine, with its own filesystem, network and process tree.
Its main purpose is to run applications within isolated environments.
What is an image?
An image is a special file that has got everything needed to run a certain application.
When an image is executed, a new container is instantiated to run the image. The container is completely isolated from the host environment by default.
If you know a bit about OOP, just to grasp the general idea, you can see the images like the classes and containers like objects instances of those classes.
How I can start and stop a container?
Containers can be start/stop/restarted.
To start a container, it must have been created.
To create it you can use the commands
run (see later).
To start a container:
> docker start container_name
to stop it:
> docker stop container_name
In every commands, you can use the containername as well as the containerid.
To know the containers id and other info you can run:
> docker ps -a
What happens when I stop a container?
As expected the container stops running and also Docker stores its status somewhere in the local storage, so it can be easily restarted.
If you don't need the container anymore, you can remove it with
> docker rm container_id
This deletes the container. Note that to be deleted it needs to be stopped.
How can I run an image?
Let's say you want to run the image for the PostgreSQL DB server. First you need to know the name of the image, in this example it is
postgres, then you just need to execute this in your shell:
> docker image run postgres
You don't even need to previously download the image, it will be pulled from the Docker HUB repository.
Where are the images pulled from?
From a service called Docker registry. There are many of these services, the default and most common one is the Docker HUB registry, which contains thousands of images that you can pull and use.
A registry is made up of many repositories, and each repository can contain one or more images.
In Docker HUB registry a repo can be public (free) or private.
How can I get an image from a Docker Registry?
By the use of the command
> docker image pull image_name
For example, if you need a PostgreSQL DB server, you'll want to use the official image called
> docker image pull postgres
This will pull the image called postgres from the Docker HUB registry.
Every image is labeled with one or more tags, that usually tell about its version. To pull a certain version of the image, specifies a tag in this way:
> docker image pull image_name:tag_name
if no tag is specified, the tag called lastest will be used, therefore the following two commands are equivalent:
> docker image pull postgres
is equivalent to
> docker image pull postgres:latest
How can I create a new image?
There are two ways to create an image:
- by using
> docker commit container_name
It commits the file changes of a container into a new image. This command is usually used for debugging.
If you want a reliable and replicable way to create an image, you must use
build with a Dockerfile.
- by using
build allows to create a new image from a
Dockerfile. A Dockerfile is a text file containing the instructions to build the image.
The instructions available are:
To know more about
build study the documentation: Define a container with a dockerfile
How can I share the images I created?
The simplest way is just exporting them using the command pair
> docker save
allows to save an image into a tar archive that you can copy and share
> docker load is the command to import the image previously saved.
If you need a complete solution with more features you should use a Docker registry.
How can I remove an image?
> docker image rm image_name
In which network a container run?
When you run a container you can specify the network to use with
--network, if no network is specified the container uses the
bridge network as default.
To list all the existing networks you can run:
> docker network ls
Can I make two different containers comunicate to each other?
Yes, the old way to do it was by using
--link, but that it is now deprecated.
The proper way is to specify the same network for all the containers you want to connect by using the command
Are data inside the container persistent?
No, as default when a container is removed all data inside it are lost.
However using the
Volumes is possible to make specified folders persistent.
How can I exec cmq inside a container
> docker exec container_name cmq
It allows to run any commands inside the container. Sometimes it is used to run a shell to access the container (read below).
How can I gain access to a container?
Most containers do not run a SSH server, so you cannot SSH into them.
Maybe you are thinking to attach to the container with
docker attach but it attaches to PID1 inside the container, which is probably not a shell.
A safer way is executing a bash shell inside the container:
> docker exec -it container_id /bin/bash
How can I detach a running container?
To detach a container and let him run on background just press
CTRL + P, CTRL + Q
Note: you have to run the container in interactive mode with
-it to be able to detach from it, for example:
> docker run -it redis
Alternatively you can run it detached since it starts, using the
> docker run -d redis
How can I attach to a running container?
> docker attach container_id
if you instead need a shell, use the
exec command to run a shell (read above).
These are some basic notes about Docker. I hope this has been an helpful reading.
I suggest you to go on learning Docker from it's detailed tutorial docs.docker.com/get-started and from practicing and experimenting.