This post is a set of personal question-based notes about Docker.
It contains essential things to know about Docker, which I wrote to remember 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 applications to be run in isolation from the machine that hosts them.
Two of the essential Docker's pars are containers and images.
What is a container?
A container is a minimal Linux machine with its filesystem, network and process tree. A Docker container is based on a Docker image, which defines how to implement the container.
In simple words, a container is a runtime instance of a Docker image.
A container's primary goal is to run applications within isolated environments.
What is a Docker image?
An image is a particular file with everything needed to run a specific application into a container.
When Docker executes an image, it instantiates a new container to run it. The container is completely isolated from the host environment by default.
To grasp the general idea. If you know about Object Oriented Programming, you can see the images like the classes and containers like objects instances of those classes.
To grasp the general idea. If you know about Object Oriented Programming, you can see the images like the classes and containers like objects instances of those classes.
How can I start and stop a container?
Containers can be created, started, stopped and restarted. However, you must create a container first, and then you can start and stop it.
To create a container, use the commands:
- create: it creates a container from an image name
- run: it works like
create
+start
For example:
To create a container from the postgres
image:
> docker create --name mypostgres -e POSTGRES_PASSWORD=xyz postgres
Note: "mypostgres" is the name we give to the new container, and "postgres" is the name of the image (which is stored on DockerHub).
After you create the container, you can start it with start
:
> Docker start mypostgres
The command run
replaces create
+ start
:
> docker run --name mypostgres2 -e POSTGRES_PASSWORD=mypw -d postgres
To stop the container:
> Docker stop mypostgres
In every command, you can use the container name or the id.
To list the containers id and other info, you can run the cmd ps
, which stands for Process Status:
`> docker ps --all'
The option -all
(`-a' for short) shows all the containers. Without it, only the running containers would be listed.
Note: another way to list the containers is:
> docker container ls
What happens when I stop a container?
As expected, Docker stops the container from running, but most importantly, it stores the container status in the local storage so it can be promptly restarted.
If you don't need it anymore, you can remove it with rm
:
> docker rm mypostgres
It deletes the container from the memory.
Note: a running container cannot be removed. You need to stop it before (see stop
).
From where are the images downloaded?
Images are pulled 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 you can pull and use.
A registry contains many repositories (public and private), and each repository can include one or more images.
How can I download an image from a Docker Registry?
Using the command pull
:
> docker image pull image_name
For example, if you need a PostgreSQL DB server, you'll want to use the official image called postgres
:
> docker image pull postgres
It will download on your computer the image called postgres from the Docker HUB registry.
How can I download a specific image version?
One or more tags can be attached to the images, and Docker uses them to identify the versions.
To pull a specific version of the image specifies the tag in this way:
> docker image pull image_name:tag_name
If no tag is specified, Docker uses the tag lastest. This means that
> docker image pull postgres
and
> docker image pull postgres:latest
are equivalent.
How can I create a new image?
There are two ways to create an image. The first is by using the command commit
:
> docker commit container_name
It commits the file changes of a container into a new image. This command is most of the time used for debugging.
The other way is using the command build
with a Dockerfile.
A Dockerfile is a text file containing the instructions to build the image.
The most common instructions available are: ADD
, CMD
, ENTRYPOINT
, ENV
, EXPOSE
, FROM
, MAINTAINER
, RUN
, USER
, VOLUME
and WORKDIR
.
To know more about the command build
, see the documentation: Dockerfile reference
How can I share the images I created?
The simplest way is just exporting them into a tar archive, using the commands Docker save
and docker load
:
> docker save
It saves an image into a tar archive that you can copy and share.
> docker load
It imports the image previously saved.
You should use a Docker registry if you need a complete solution with more features.
How can I remove an image?
> docker image rm image_name
In which network does a container run?
When you run a container, you can specify its network with the option --network
.
If no network is set, the container uses the bridge
network as default.
To list all the existing networks you can run:
> docker network ls
How can I make two containers to communicate with each other?
You need to set the same network for all the containers you want to connect by using the option --network
The old way used the option --link
, but it is now deprecated.
Are the data inside a container persistent?
Not as default, all data inside a container are lost when it is removed.
However, using the Volumes
is possible to make specified folders persistent.
How can I exec a command inside a container?
> docker exec container_name cmq
It runs the command cmd inside the container. Sometimes it is used to run a shell to access the container (see below).
How can I attach to a running container?
> docker attach container_id
It attaches your terminal's standard input, output, and error to a running container.
How can I gain access to a container?
Most containers do not run an SSH server, so you cannot SSH into them.
With the command attach
, you can attach to PID1 inside the container.
A safer way is by 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 it run in the background, just press:
CTRL + P, CTRL + Q
You can you detach from a running container only if you run it with the interactive mode option -it
:
> docker run -it redis
Alternatively, you can detach it from the start using the `-d' option:
> docker run -d redis
I hope this article helped you to brush up on your Docker skills.
Finally, the best way to deepen your knowledge is by studying the official doc docs.docker.com/get-started and always practising and experimenting with what you learn.
Updated on Jan 2023