Publish Docker Ports

By default, when you create or run a container using docker create or docker run, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

Here are some examples.

Flag valueDescription
-p 8080:80Map TCP port 80 in the container to port 8080 on the Docker host.
-p 192.168.1.100:8080:80Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100.
-p 8080:80/udpMap UDP port 80 in the container to port 8080 on the Docker host.
-p 8080:80/tcp -p 8080:80/udpMap TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

Commit Changes to Docker Image

You can create a new docker image by committing the changes in an existing using the following syntax:

sudo docker commit [CONTAINER_ID] [new_image_name]

Suppose there is a running docker with name “existing-image” (you can check with “docker ps -a”), the below common will commit the changes made in the running docker into the “new-image” image.

docker commit existing-image new-image

Auto Start NodeJS NPM React App after Reboot using PM2

To auto start node.js npm app after reboot, one way is to use pm2.

First instead pm2:
> npm install -g pm2

Configure pm2 to start as root
> pm2 startup -u root

Add the npm app to pm2 (example shown here is a react app, replace “yourapp” with the name of your react app)
> pm2 start node_modules/react-scripts/scripts/start.js –name “yourapp”

Save the configuration
> pm2 save

Now when you reboot the server, your npm app will be automatically run.