Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.
$ docker
// to get all the command available for docker
$ docker —version
// check docker version
Docker image is a template for creating and environment of your choice, i.e - database, app etc. and has everything we need to run our apps including OS, Software and of course our application code.
List of all docker images, run:
$ docker images
To delete a docker image, run:
$ docker image rm -f IMAGE_ID
Package Software into Standardized Units for Development, Shipment and Deployment. Container runs instance of of an docker image. Read more
Let's run a nginx image
$ docker pull nginx
to check the running images, run the following command, it will display all the images with repository, tag, image id, created date and size information
$ docker images
to run a docker image:
$ docker run nginx:latest
run a docker image in detouched mode
$ docker run -d nginx:latest
to check all running containers, type:
$ docker container ls
Detached mode, shown by the option --detach or -d, means that a Docker container runs in the background of your terminal. It does not receive input or display output.
$ docker run -d IMAGE
To stop the container, type the following command:
$ docker stop CONTAINER_ID
$ docker run -d -p 8080:80 nginx:latest
This will point the host port 8080 to container port 80
Exposing multiple ports
Add another port, this will map localhost:8080 to port 80 and also localhost:3000 to port 80, so with the both port you can access the container or app
$ docker run -d -p 8080:80 -p 3000:80 nginx:latest
Type the following command to see list of containers:
$ docker ps
Also you can run:
$ docker ps --help
or
To learn all the realted command of docker ps click here
Stop a docker container
$ docker stop CONTAINER_ID/NAME
To start the docker container again, run:
$ docker start CONTAINER_ID/NAME
To check all the container, run:
$ docker ps -a
To delete a container, run:
$ docker rm CONTAINER_ID
To delete all containers, run:
$ docker rm $(docker ps -aq)
Note: please keep in mind that if any container is running the above command will not work. So run the following command:
$ docker rm -f $(docker ps -aq)
If you do not specify any name to the container, it will be named automatically. To name of your container, run:
$ docker run --name myapp -d -p 8080:80 -p 3000:80 nginx:latest
To stop the container, run:
$ docker stop myapp
To start again the container, run:
$ docker start myapp
To list all running containers with their labels in a table format you can use:
$ docker ps --format "table {{.ID}}\t{{.Labels}}\t{{.Names}}\t{{.Ports}}"
Keep the formated variable as FORMAT
$ export FORMAT ="{{.ID}}\t{{.Labels}}\t{{.Names}}\t{{.Ports}}"
Now run the command
$ docker ps --format=$FORMAT
Volumes allows sharing data, files & folders between host and container [or between containers] Read more about docker volumes
$ docker run --name myapp -v $(pwd):/usr/share/nginx/html -d -p 8080:80 nginx
pwd = current directory, works dir in windows
$ docker exec -it myapp bash
this will give us an access to inside the container in interactive mode, here myapp is the running container.
To get all the docker run help command, run:
$ docker run --help
scrool down and at -v section there is --volumes-from list
Now lets copy the content from myapp, run:
$ docker run --name myapp-copy --volumes-from myapp -d -p 8081:80 nginx
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
Find the images, run:
$ docker image ls
Sample Dockerfile (keep this file in root)
FROM nginx:latest
ADD . /usr/share/nginx/html
Another exampple to built NodeJS app
FROM node:latest
WORKDIR /app
ADD . .
RUN npm install
CMD node index.js
$ docker build --tag myapp:latest .
here: myappp = name of the app, latest= tag/version of the app, . [dot] = Dockerfile location is current file
Now run our own image: $ docker run -d -p 8080:80 myapp:latest
create a file inside your app as .dockerignore and include the files/folders that you want docker to ignore, same as .gitignore
node_modules
Dockerfile
.git
we do not need to install dependencies if not required, so let's cahce it changing the Dockerfile as below:
FROM node:latest
WORKDIR /app
ADD package*.json .
RUN npm install
ADD . .
CMD node index.js
we can use alpine version of the images, i.e. $ docker pull node:alpine
to pull the latest alpine version. Almost every popular repository has alpine version.
Instead of using image:alpine lets use the version, so in future it can be change with our application requirements.
FROM node:1.19.3-alpine
$ docker tag myapp-api:latest myapp-api:1
This will create another image with tag 1 but both have the save funtionality. Now lets modify any code and build the image with 'latest' tag. then tag another image (same as before) as tag:2. Now we have 3 images, latest, 1 and 2. lets spin all three images. latest and 2 are the same but with tag 1 is the previous version.
The Registry is a stateless, highly scalable server side application that stores and lets you distribute Docker images. The Registry is open-source, under the permissive Apache license. read more
Here are few registry:
- Docker Hub
- quay.io
- Amazon ECR
docker hub is popular one, lets create an account and then login. Now go to Repositories and create new repository.
$ docker tag myapp:1 dockerusername/myapp:1
Once done, login to your docker hub with your username and password. You can do it by clicking on docker desktop logo or from terminal:
$ docker login
then type username and password
Now push your local image to docker hub, run:
$ docker push yourdockerusername/myapp:1
Check your repository, its listed there. Now you can pull your own image and run locally as we did earlier nginx image.
To inspect a running docker image, run:
$ docker inspect CONTAINER_ID/CONTAINER_NAME
To see the log, run:
$ docker logs CONTAINER_ID/NAME
To get the live logs or follow, run:
$ docker logs -f CONTAINER_ID
To see all the commands, run:
$ docker logs --help
to login to the container machine, run:
$ docker exec -it 2383adb04350 /bin/bash
or $ docker exec -it container_id /bin/sh
if you do not find the bash, inspect the container then find cmd
Congratulations! You have finished Docker handson tutorial successfully. Start Kubernetes Tutorial.