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
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
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
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
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
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
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
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
docker rm container_id_or_name
This command removes the container
ex
docker rm redis_old redis_latest
Remove all containers
💡
docker ps -q | xargs docker stop
docker ps -q | xargs docker rm
To get the interactive terminal of a container
docker exec -it container_id_or_name /bin/bash
To exit the terminal simply Enter exit
Here -it
stands for interactive mode.
Docker Volume
Think of them as special folders that you can use to store data outside of your container. This is really helpful because it means that even if your container gets deleted or crashes, your data will still be safe and sound.
Example
docker run -p 27017:27017 -v atulDB:/data/db --name my-mongodb-container mongo:latest
Top comments (0)