Docker - DhruvSingh861/Netcore_Assignment GitHub Wiki
Docker
- Docker is a software framework for building, running, and managing containers on servers and the cloud.
- Docker is a powerful tool used for developing, packaging, and deploying applications efficiently.
- It reduces your delay between writing code and running it in production. You can create self-contained environments known as containers. That can run consistently on different platforms.
- You need to install the Docker engine on your computer or device. The Docker engine allows you to create and manage docker containers, docker images, docker hub, docker desktop, etc.
Docker Installation on Ubuntu
sudo apt-get update
sudo apt install docker.io
sudo systemctl enable docker
start docker.sudo docker run hello-world
to test if installation is done correctly, "Hello from Docker" should be printed.docker --help
get commands used
Common Commands:
run
Create and run a new container from an image
exec
Execute a command in a running container
ps
List containers
build
Build an image from a Dockerfile
pull
Download an image from a registry
push
Upload an image to a registry
images
List images
login
Log in to a registry
logout
Log out from a registry
search
Search Docker Hub for images
version
Show the Docker version information
info
Display system-wide information
Running Postgres on Docker container
-
sudo docker login
Login with your Docker ID to push and pull images from Docker Hub. -
docker pull postgres:alpine
download image for postgres (alpine version). -
sudo docker images
list all images present. -
sudo docker run --name postgres-0 -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres:alpine
running postgres container on default port (with password 'password'). -
docker run --name postgres11 -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres
-
sudo docker ps
list containers (you can see postgres-0 running). -
sudo docker exec -it postgres-0 bash
this command will let you get inside the actual container. exec means interact inside postgres-0 using bash command. -
pwd
get inside root folder -
ls
get file structure -
psql -U postgres
-U for user name ('postgress' has root access). -
\du
to see user -
create database test;
create database -
\l
show all databases -
\c test
connect to 'test' database -
\d
get relations -
sudo docker stop CONTAINER_ID
stop the container by viewing container id fromdocker ps
. -
sudo docker rm CONTAINER_ID
remove that container (do it before creating a container with same name.) -
sudo docker start 3e27
start an exited (stopped) container. -
sudo service redis-server stop
command to stop locally installed redis.
Connect the container running postgres image to Local Machine
psql -h localhost -p 5432 -U
postgrers get you inside of that instance running postgres.\q
to quit out of this.
Running Redis on Docker container
sudo docker pull redis
pull redis image form docker hub. no version specified means default latest versionsudo docker run --name redis01 -d redis
-d means run it in demon mode (in background)sudo docker exec -it 3e27 sh
execute redis container. sh means shell, -it means run it in interactive mode, 3e27 is container ids 1st 4 letters.sudo docker run --name redis02 -v redis.conf:/usr/local/etc/redis/redis.conf -d redis
in order to use a specific config file, we have to map a value. the file in left hand side is our config file and the file in the right hand side is redis config file inside the container.
Explanation of run command
sudo docker run --name redis01 -p 6379:6379 -d redis
-
sudo: This command runs the following Docker command with superuser (administrator) privileges. It's often required to execute Docker commands, especially on systems where Docker needs elevated permissions to create and manage containers.
-
docker: This is the Docker command-line client that allows you to interact with Docker. It's the primary command to manage Docker containers, images, networks, and volumes.
-
run: This command creates and starts a new container from a specified Docker image. If the image is not available locally, Docker will pull it from the Docker Hub or another configured Docker registry.
-
--name redis01: This option assigns a custom name to the container, in this case, redis01. Naming containers makes it easier to manage and reference them later (e.g., for stopping, starting, or inspecting them). Without this option, Docker would assign a random name.
-
-p 6379:6379: This option publishes a container's port to the host. The format is hostPort:containerPort. Here's what each part means:
-
6379 (first part): The port number on the host machine. This is the port you will use to access the Redis server from outside the container (e.g., from your Java application or other tools).
-
6379 (second part): The port number inside the container where the Redis server is listening. By default, Redis listens on port 6379 inside the container. This option maps port 6379 on your host machine to port 6379 inside the container, allowing external access to Redis running inside the container.
-
-d: This option runs the container in detached mode, meaning the container runs in the background. If you omit this option, the container will run in the foreground, and you will see its logs and output in your terminal.
-
redis: This is the name of the Docker image to use for the container. If the redis image is not already available on your local system, Docker will automatically pull it from Docker Hub, the default public registry.
Connection options: -h, --host=HOSTNAME database server host or socket directory (default: "local socket") -p, --port=PORT database server port (default: "5432") -U, --username=USERNAME database user name (default: "root") -w, --no-password never prompt for password -W, --password force password prompt (should happen automatically)