Why Should Learn Docker
What is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
Docker Architecture
Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface. Another Docker client is Docker Compose, that lets you work with applications consisting of a set of containers. More information please follow Docs Docker.
Start using Docker
Run Docker Command
The following command run a hello_world
container.
|
|
If you don’t have the
piinalpin/sample-node-web-app
image locally, docker pulls it from your configured registry, as though you had rundocker pull piinalpin/sample-node-web-app
manually.Docker create a new container
hello_world
from imagepiinalpin/sample-node-web-app
Let’s get into the container.
|
|
Run a simple command to get list folder in work directory by type the following command.
|
|
We will get an output.
|
|
Inside the container there is a folder
node_modules
andDockerfile package-lock.json package.json server.js yarn.lock
filesThe server is using
express.js
and running on port8080
Show linux distribution
The following command use to show linux distribution inside container.
|
|
We will get an output
|
|
This image use Linux Alpine
which the operating system is very light. And then let’s testing the server API by type the following command
|
|
We should get an output
|
|
Thats mean, on Linux Alpine
distribution didn’t have curl
command. We should install it by type the following command.
|
|
Then, test again the API server by type a command curl http://localhost:8080
. And we should get an output.
|
|
Expose Container PORT to Host PORT
If we want to access the container from host, we should expose the container port to host port by type a command docker run -p <HOST_PORT>:<CONTAINER_PORT> --name <CONTAINER_NAME> -d <IMAGE>
|
|
Docker create a new container
hello_world2
from imagepiinalpin/sample-node-web-app
Docker expose port
8080
from container to8001
host port and we can access from host
Check the container is already running by type following command
|
|
We should get an output
|
|
Let’s test the server API from the host by type following command
|
|
We should get an output
|
|
Build Image
We will build an image which running a Node.js
server. If you don’t have a Node.js
install from Node.js.
Create a simple node application. First we will install the dependencies, we will use an express.js
module by type a command npm install express
or yarn add express
Then create a server.js
like following script
|
|
Try run the server
|
|
On the new terminal session type command curl http://localhost:8080
. And we should get an output.
|
|
And let’s create an image using Dockerfile
like following script
|
|
- Dockerfile will pull
node:16-alpine3.11
images - Dockerfile create a working directory
/usr/src/app
- Dockerfile copy the
package*.json
into./
directory, that’s mean all of file where file with the prefixpackage
and postfix.json
will copied on working directory - Dockerfile execute command
npm install
to install all dependencies - Dockerfile copy all file inside project directory into working directory
- Dockerfile will expose port
8080
- Dockerfile will run command
node server.js
Then we can build an image from Dockerfile
by type the following command
|
|
Then makesure the image success created
|
|
We should get an output
|
|
And then try to run container from hello-world-image
and also expose port to host port
|
|
Docker create a new container
hello_world3
from imagehello-world-image
Docker expose port
8080
from container to8002
host port and we can access from host
We should get an output
|
|
Let’s try the server app by type following command curl http://localhost:8002
and we should get an output
|
|
Stop, Start, Delete Container and Delete Images
If you want to start, stop, delete container and delete image, type the following command
|
|
Thankyou
Docs Docker - Docker Overview