Docker Basics beginner to pro

oatula

Atul Anand Oraon

Posted on April 10, 2024

Docker Basics beginner to pro

Docker

Use Docker without sudo

For this, we need to add the user to the docker mod.

Below is the terminal command:

sudo usermod -a -G docker userName
#or
sudo usermod -a -G docker $USER
Enter fullscreen mode Exit fullscreen mode

Now we need to logout and login again to see effects.

Pull and run a public docker image from the docker hub.

Here’s the command

version is optional

docker run image:version
Enter fullscreen mode Exit fullscreen mode

Docker advantages

1 Its light

2 Its fast

3 It allows to have different configuration layer

i.e. We can separate it form the actual OS configs.

4 We also get the benefit of running the same image

with different versions simultaneously on the same computer.

Basic Commands

docker pull imageName:version

This gets the public image from docker hub.

The version is optional

docker images

It shows the list of all images

REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
redis                    latest    e10bd12f0b2d   7 days ago      138MB
Enter fullscreen mode Exit fullscreen mode

docker run imageName:version

Here things get interesting.

If you mentioned the version while pulling, you must do so now too.

Else docker will pull the latest version of it and then run it.

💡 Well, you can stop it by just doing a Ctrl+C that's it.

docker ps

lists down all the currently running images.

💡 We can also use Ctrl+C to stop the image

docker run -d imageName

To run the image in detached mode i.e. now Ctrl+C won’t work on it.

It runs in detached mode

example

docker run -d -p3000:3000 --name node-app example-node
Enter fullscreen mode Exit fullscreen mode

Now to stop a detached image

We need to

💡 docker ps
Now we have the id
We can stop it simply using
docker stop id

docker ps -a

This prints the whole history.

From here we can start a container with its id.

by using the command

docker start id
Enter fullscreen mode Exit fullscreen mode

docker run -p:containerPort image:version

Now the command above looks a bit big.

Let me break it down.

Let’s first have an example

docker run -p9000:6379 -d redis
Enter fullscreen mode Exit fullscreen mode

Result

docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                       NAMES
5aeaf8ee3849   redis     "docker-entrypoint.s…"   14 seconds ago   Up 11 seconds   0.0.0.0:9000->6379/tcp, :::9000->6379/tcp   xenodochial_curran
Enter fullscreen mode Exit fullscreen mode

This helps us bind a host port to the container port.

This is really helpful when we want to run 2 versions of the same image.

We can bind them to 2 different ports.

docker logs id

This helps us see the logs of a container.

This helps us solve bugs and errors.

💡 we can also use names instead of id here

Naming the docker containers

docker run —name custom-name image:version

docker run -p9000:6379 -d --name redis_latest redis
docker run -d -p9001:6379 --name redis_old redis:6.2
Enter fullscreen mode Exit fullscreen mode

docker rm container_id_or_name

This command removes the container

ex

docker rm redis_old redis_latest
Enter fullscreen mode Exit fullscreen mode

Remove all containers

💡

docker ps -q | xargs docker stop
docker ps -q | xargs docker rm
Enter fullscreen mode Exit fullscreen mode

To get the interactive terminal of a container

docker exec -it container_id_or_name /bin/bash
Enter fullscreen mode Exit fullscreen mode

To exit the terminal simply Enter exit

Here -it stands for interactive mode.

Docker Volumes

Creating a Volume

You can create a volume explicitly using the docker volume create command:

docker volume create my-volume
Enter fullscreen mode Exit fullscreen mode

This creates a new volume named my-volume that Docker will manage.

Running a Container with a Volume

When running a container, you can mount the volume using the -v or --mount flag:

docker run -d --name my-app -v my-volume:/app myapp:v1
Enter fullscreen mode Exit fullscreen mode

This mounts the my-volume volume to the /app directory inside the container. Any data written to /app will be stored in the my-volume volume on the host.

Inspecting Volume Contents

You can inspect the contents of the volume on the host machine. The exact location depends on your Docker storage driver, but typically it will be under /var/lib/docker/volumes/. For example:

ls /var/lib/docker/volumes/my-volume/_data
Enter fullscreen mode Exit fullscreen mode

This will list the files stored in the volume.

Image description## Persisting Data

If the container crashes or is removed, the data written to the volume will persist on the host. You can then attach the volume to a new container to access the data:

docker run -d --name new-app -v my-volume:/app myapp:v2
Enter fullscreen mode Exit fullscreen mode

The new container will have access to the same data as the previous container.

Sharing Volumes Between Containers

Volumes can be mounted to multiple containers simultaneously. This allows containers to share data:

docker run -d --name app1 -v my-volume:/data app1:v1
docker run -d --name app2 -v my-volume:/data app2:v1
Enter fullscreen mode Exit fullscreen mode

Both app1 and app2 will have read-write access to the /data directory which points to the my-volume volume.

Docker volumes provide a way to persist data outside the container lifecycle. This ensures important data is not lost when containers are removed or crash. Volumes can be shared between multiple containers and their contents can be inspected directly on the host.

Thanks for making it to the very last. Since you read this do consider
leaving some comments.

Also you might find this post useful

https://dev.to/oatula/making-backup-of-a-docker-volume-1la9

💖 💪 🙅 🚩
oatula
Atul Anand Oraon

Posted on April 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related