Deploying an application with Docker involves several steps. Here is a simplified guide to using Docker for deployment, assuming that Docker is already installed on your machine:
1. Create a Dockerfile: This file contains instructions for Docker to build your application. Start with specifying a base image that includes the platform requirements (like Python, Java, .NET). Then outline the instructions to copy your code into the image and set correct file permissions. Specify the command to run the application.
Here’s a simple example of a Dockerfile for a Node.js application:
```
1. Build the Docker Image: Run the following command in the same directory as Dockerfile.
```
docker build -t your-app-name .
```
The `-t` flag is for you to tag the image with a name so you can refer the image later.
1. Test your Image Locally: Before deploying your image onto a server, you may want to test the image on your local machine. Use this command to run the image:
```
docker run -p 4000:8080 your-app-name
```
The `-p` flag is for port mapping. It maps the port 4000 on your local machine to port 8080 on the Docker container.
1. Push the Image to a Registry: Before deployment, you must first push your image onto a Docker registry. The Docker registry could be Docker’s own DockerHub or your private Docker registry.
First, tag your image with the registry’s address:
```
docker tag your-app-name your-registry-username/server-address:tag-name
```
Then, push the image onto the Docker registry:
```
docker push your-registry-username/server-address:tag-name
```
1. Deploy the Application on Server: SSH into the server where you’d like to run your app, install Docker if it’s not installed, and run:
```
docker run -d -p 4000:8080 your-registry-username/server-address:tag-name
```
This command will pull the image from the Docker registry and run the image on the server.
1. Update the Application: To update the application, you must rebuild the Docker image with the new version of the application, push the new image to Docker registry, and then rerun the Docker image on the server. You might need to stop and remove the old container before you can rerun the new image on the same port.
In practice, DevOps tools and orchestration like Kubernetes or Docker Swarm can make deployment and update process much simpler and more manageable.