Docker Tutorial for Beginner
What is Docker?
Docker is a container management service. The keywords of Docker are developed, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.
Docker Features….
- Light-Weight
- Minimal overhead (CPU/io/network)
- Based on Linux containers
- Portable
- Run it Everywhere! — Linux, Mac OS or Windows operating system
- Self-sufficient
- A Docker container contains everything it needs to run
- Minimal Base OS
- Libraries and frameworks
- Application code
- A Docker container should be able to run anywhere that Docker can run.
Images vs containers
•Docker images are executable packages that include everything needed to run an application — the code, a runtime, libraries, environment variables, and configuration files.
•Docker containers are a runtime instance of an image — what the image becomes in memory when executed (that is, an image with the state, or a user process).
Docker Hub:
Docker Hub is a service provided by Docker for finding and sharing container images with your team. It provides the following major features: Repositories: Push and pull container images. … Webhooks: Trigger actions after a successful push to a repository to integrate Docker Hub with other services. More details click docker hub. Following examples, pulling and running docker containers.
Docker version — Show the docker version details in the operating system.
docker version
Client: Docker Engine - Community
Version: 19.03.8
...
Built: Wed Mar 11 01:21:11 2020
docker help — for more information on a command.
docker --help
Usage: docker [OPTIONS] COMMAND
Management Commands:
network Manage networks
volume Manage volumes
Commands:
kill Kill one or more running containers
login Log in to a Docker registry
run Run a command in a new container
docker pull — Pull an image or a repository from a registry on https://hub.docker.com/.
docker pull <image name + tag >
We are installing the latest version of MySQL following step.
docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
1811572b5ea5: Pull complete
Status: Downloaded newer image for mysql:latest
Getting Started with NGINX Installation and Basic Setup
STEP-1: Pulling latest version of nginx.
docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
STEP-2: Show image list.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 11 days ago 127MB
STEP-3: Run nginx image.
docker run --name nginx -d -p 8090:80 nginx
--name <Container name>
-d flag, to start a container in detached mode, you use -d
-p <Expose Port (expose port:current port )>
nginx is a image name
STEP-3: Show container.
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
8435dd4471b1 nginx "nginx -g 'daemon of…" 53 seconds ago
STATUS PORTS NAMES
Up 52 seconds 0.0.0.0:8090->80/tcp nginx
STEP -4: Check nginx server web browser.
Most Docker commands
+==========================+=======================================+
| Docker Commands | Explanation |
+==========================+=======================================+
| docker images | Image list |
+--------------------------+---------------------------------------+
| docker images -q | q − to return the Image ID’s only |
+--------------------------+---------------------------------------+
| docker images prune | Delete dangling images |
+--------------------------+---------------------------------------+
| docker images prune -a | Delete all unused images |
+--------------------------+---------------------------------------+
| docker run <images name> | Imaged run |
+--------------------------+---------------------------------------+
| docker ps -a | List of the container.(-a all list ) |
+--------------------------+---------------------------------------+
| docker start <ID> | Start stopped container |
+--------------------------+---------------------------------------+
| docker stop <ID> | Stop sunning container |
+--------------------------+---------------------------------------+
| docker rmi -f <ID> | Force image delete |
+--------------------------+---------------------------------------+
| docker container prune | Delete stoped containers |
+--------------------------+---------------------------------------+
| docker history <ID> | History of image |
+--------------------------+---------------------------------------+
| docker inspect <ID> | Details of the image |
+--------------------------+---------------------------------------+
| docker logs <ID> | Show the logs of a container |
+--------------------------+---------------------------------------+
Build a Docker File:
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. Dockerfile text file (recipe) used to create Docker images. Docker file name must be Dockerfile
Docker File Commands
ADD copies the files from a source on the host into the container’s own filesystem at the set destination.
CMD can be used for executing a specific command within the container.
ENTRYPOINT sets a default application to be used every time a container is created with the image.
ENV sets environment variables.
EXPOSE associates a specific port to enable networking between the container and the outside world.
FROM defines the base image used to start the build process.
MAINTAINER defines a full name and email address of the image creator.
RUN is the central executing directive for Dockerfiles.
USER sets the UID (or username) which is to run the container.
VOLUME is used to enable access from the container to a directory on the host machine.
WORKDIR sets the path where the command, defined with CMD, is to be executed.
LABEL allows you to add a label to your docker image.
Usage: The docker build command builds an image from a Dockerfile
and a context. The build’s context is the set of files at a specified location PATH
or URL
.
docker build .
Following example install ubuntu and then update ubuntu finally install Nginx on ubuntu.
# Ubuntu
FROM ubuntu
MAINTAINER demo@demo.com
RUN apt-get update
RUN apt-get install –y nginx
CMD [“echo”,”Image created”]• The first line "#This is a sample Image" is a comment.
• The next line has to start with the FROM keyword. It tells docker, from which base image you want to base your image from. In our example, we are creating an image from the ubuntu image.
• The next command is the person who is going to maintain this image. Here you specify the MAINTAINER keyword and just mention the email ID.
• The RUN command is used to run instructions against the image. In our case, we first update our Ubuntu system and then install the nginx server on our ubuntu image.
• The last command CMD is used to display a message to the user.
You can download Docker file, Dockerfile
Docker-compose:
Docker Compose is used to run multiple containers as a single service. For example, suppose you had an application which required NGNIX web server and My-SQL database, you could create one file.
Create Docker-Compose File at any location on your system.
- Tool for defining & running multi-container docker application
- All Docker Compose files are YAML
- The filename must be docker-compose.yml
- Can start all services with a single command
- Can stop all services with a single command
+=======================+=========================================+
| Docker-Compose | Explanation |
+=======================+=========================================+
| docker-compose config | Check the validity of file by command |
+-----------------------+-----------------------------------------+
| docker-compose up –d | Run docker-compose.yml file by command |
+-----------------------+-----------------------------------------+
| docker-compose down | Down Docker compose |
+-----------------------+-----------------------------------------+
| docker-compose –help | For more information on docker-compose. |
+-----------------------+-----------------------------------------+
| docker-compose -v | Docker compose version |
+-----------------------+-----------------------------------------+
Following example install Nginx, MYSQL and Adminer same as the docker-compose file. The first step creates a docker-compose.yml file and configures docker-compose. The docker-compose up –d command builds an image from a docker-compose.yml and a context. The build’s context is the set of files at a specified location PATH.
docker-compose.yml file.
version: '3'
services:
# Install Nginx web server
web:
image: nginx
container_name: nginxServer
# http://localhost:9090
ports:
- '9090:80'
# Intsall mysql latest version
db:
image: mysql
container_name: mySql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydb
ports:
- '3306:3306'
expose:
- '3306'
# Install adminer (Adminer is a tool for managing in MySQL)
adminer:
image: adminer
container_name: db_adminer
restart: always
# http://localhost:8090/adminer
ports:
- '8090:8080'
Save as above text into docker-compose.yml and then use following commands. The file must be the same path.
docker-compose up -d
Creating mySql ... done
Creating db_adminer ... done
Creating nginxServer ... done
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
b5df4a4af1ec nginx "nginx -g 'daemon of…" 8 seconds ago
435f8a900dc8 adminer "entrypoint.sh docke…" 8 seconds ago
0faf0e6e65a9 mysql "docker-entrypoint.s…" 8 seconds ago
STATUS PORTS NAMES
Up 7 seconds 0.0.0.0:9090->80/tcp nginxServer
Up 7 seconds 0.0.0.0:8090->8080/tcp db_adminer
Up 7 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mySql
You can download docker-compose file, docker-compose.yml
More examples and documents follow link https://github.com/mutluokuducu/Docker