Docker is an open platform for developing, shipping, and running applications. With Docker, you can separate your applications from your infrastructure and treat your infrastructure like a managed application.
Docker helps you ship code faster, test faster, deploy faster, and shorten the cycle between writing code and running code.
Hello World
✔️ Run a hello world container
dockerrunhello-world
✔️ Take a look at the container image
dockerimages
✔️ Look at the runnung containers
dockerps
If you want to see all containers, including ones that have finished executing, run docker ps -a
Build
✔️ Build a Docker image based on a simple node application.
# Switch into a folder named testmkdirtest&&cdtest
# Create a Dockerfile# Use an official Node runtime as the parent imageFROM node:lts# Set the working directory in the container to /appWORKDIR /app# Copy the current directory contents into the container at /appADD . /app# Make the container's port 80 available to the outside worldEXPOSE 80# Run app.js using node when the container launchesCMD ["node", "app.js"]EOF
// Create the node appllicationconsthttp=require("http");consthostname="0.0.0.0";constport=80;constserver=http.createServer((req, res) => {res.statusCode =200;res.setHeader("Content-Type","text/plain");res.end("Hello World\n");});server.listen(port, hostname, () => {console.log("Server running at http://%s:%s/", hostname, port);});process.on("SIGINT",function () {console.log("Caught interrupt signal and will exit");process.exit();});
Now build the image.
docker build -t node-app:0.1 .
Run
✔️ Run containers based on the image you built
dockerrun-p4000:80--namemy-app:0.1
✔️ Stop and remove the container
dockerstopmy-app&&dockerrmmy-app
✔️ Start the container in the background
dockerrun-p4000:80--namemy-app-dnode-app:0.1
✔️ Run another container with the new image version
Edit app.js
....constserver=http.createServer((req, res) => {res.statusCode =200;res.setHeader('Content-Type','text/plain');res.end('Welcome to Cloud\n');});....
Build this new image and tag it with 0.2
dockerbuild-tnode-app:0.2.
Run another container
dockerrun-p8080:80--namemy-app-2-dnode-app:0.2
Debug
✔️ Look at the logs of a container
dockerlogs [container_id]
If you want to follow the log’s output as the container is running, use th -f option. docker logs -f [container_id]
✔️ Start an interactive Bash session inside the running container.
dockerexec-t [container_id] bash
✔️ Examine a container’s metadata in Docker by using Docker inspect