The image you provided outlines a general workflow for using Docker to develop, test, and deploy an application. Here's an explanation of each step with an example:
-
Develop
- Write your application code: This is the initial step where you write the code for your application. For example, let's say you're building a simple Node.js web application.
// app.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Docker!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
-
Dockerfile
- Create a Dockerfile that defines the environment and dependencies for your application: A Dockerfile is a text document that contains the commands to assemble an image. Here’s an example Dockerfile for the above Node.js application.
# Use an official Node.js runtime as a parent image
FROM node:14
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the package.json and package-lock.json files
COPY package*.json ./
# Install the dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Command to run the application
CMD ["node", "app.js"]
-
Build Image
-
Use
docker build
to create a Docker image from your Dockerfile: This command creates an image from your Dockerfile.
-
Use
docker build -t my-node-app .
This command builds an image named my-node-app
.
-
Run Container
-
Use
docker run
to launch a container from your image: This command creates a container from your image and runs it.
-
Use
docker run -p 3000:3000 my-node-app
This command runs the container and maps port 3000 of the container to port 3000 of the host machine. Now, if you navigate to http://localhost:3000
in your browser, you should see "Hello, Docker!"
-
Test
- Test your application within the container: Ensure your application works as expected within the container. If you make changes to your code, rebuild the image and recreate the container.
docker build -t my-node-app .
docker run -p 3000:3000 my-node-app
-
Push (Optional)
-
Use
docker push
to share your image on a registry (e.g., Docker Hub): If you want others to use your Docker image, you can push it to a registry like Docker Hub.
-
Use
docker tag my-node-app mydockerhubusername/my-node-app
docker push mydockerhubusername/my-node-app
-
Pull (Optional)
-
Others can use
docker pull
to download your image and run your application in their own environments: If someone else wants to use your application, they can pull your image from the Docker registry and run it on their own machine.
-
Others can use
docker pull mydockerhubusername/my-node-app
docker run -p 3000:3000 mydockerhubusername/my-node-app
This process ensures that your application is containerized, making it easy to deploy and run consistently across different environments.